There are a couple of ways to take out / Reset / Remove commits from branch
RESET
The reset will basically delete this log history.So basically it's overwriting the history. It will also help you delete added files. So these two files will also get deleted.
- Hard Reset
- first log- there are 3 commits and 3 files
- Lets check new files
- Let reset the Latest commit-
- this delete last added file and latest commit
- Use 2 ^ to remove 2 commit history 3 ^ for 3 and so on
- Check log and files on derive again
- We can see
- Test.txt file removed
- commit related to test.txt file is removed
- Soft Reset (Particular commit)
If you do a git reset soft it will only delete the history. It will not remove the files for you. check first 6 characters of commit by running git log- We can remove --soft flag as soft is by default
- Soft Reset (Last commit)
Recommended: Undoes commit, keeps changes staged- It undoes the last commit, but it keeps all the changes that were part of that commit in your staging area (index). Your working directory remains untouched.
REVERT
- Same as resent, but in revert a new commit history will be created, old history will remain as it is, Wherein in reset the history is rewritten.
- The git revert command creates a new commit that reverses the changes of a preceding commit, thereby undoing it. This method is the most secure
- Safe method of undoing modifications in a shared repository and maintains your commit history.
- git revert focuses on the changes made by a commit, not the existence of files.
- If a commit added a file, the reverting commit will remove it.
- If a commit modified a file, the reverting commit will undo those modifications.
- If a commit deleted a file, the reverting commit will re-add it (in the state it was in the commit before the deletion).
- Make sure on revert last commit not last changes at working directory without any commit
- this open an VI editor to add comment for revert. other wise it add own default comment. to skip VI editor
- Revert Last commit
- Revert a specific commit
- Revert without opening VI editor for comment
- Revert a commit back in history
- Revert Last commit