When using git for version control, there is frequent need on undoing commits due to some unexpected changes. This post will introduce how to undo changes with git command in different cases.
Undo commit
A common undo case is that some commit needs to be reverted as the commit contains error. In this case, the code is already committed. The command to revert the commit is
git revert HEAD
This command will add new a commit to the existing head to undo the previous commit. It will not change the previous commit history, hence there is no risk of losing changes.
If need to revert multiple commits, run revert command on multiple commits.
git revert [commit1] [commit2]
git revert
command can have additional options as well.
- --no-edit: it will not open editor when executing and will use the commit message generated by git
- --no-commit: it will revert the changes in staging area but will not create new commit.
Reset commit
Sometimes one may want to remove the commit from the git history instead of just undoing the commit, command git reset
can be used in this case. Syntax is:
git reset [last good SHA]
git reset
will move the history pointer to the specified commit and drops the rest of commits after the specified commit. By default, git reset
will not change the files in working directory but only change the files in staging area. If running with --hard
option, the working directory files would be changed as well.
git reset --hard [last good SHA]
In case one wants to find the dropped commits back after running git reset, git reflog
can be ran. But this command needs to be ran within a certain period of time, otherwise the dropped changes may be lost forever.
Replace last commit
Sometimes one may realize that the commit message needs to be updated after a commit is done, --amend
option can be used to change the commit message.
git commit --amend -m "Fixes bug #42"
How it works is that it will generate a new commit object and replace the previous commit object. If there are changes in staging area when above command is ran, those changes would also be committed. Hnece this option not only changes the commit message but also replaces the commit object with new commit object.
Undo file change in working directory
If some file change in working directory is wrong, git checkout
command can be used to bring back the original file.
git checkout -- [filename]
This command will first find the file in staging area, it will revert to that file if it exists, otherwise it would revert to the working file in previous commit.
Once the file change is undone, it cannot be found back anymore.
Undo file change in staging area
Assume some file change has been added to staging area
git add --all
Now they need to be dropped, below command can be used.
git rm --cached [filename]
This command will not affect those committed changes.
Undo changes in current branch
In case one finds that some commits are in wrong branch, below commands can be ran.
// create a new branch, but the current branch is unchanged
git branch feature
// reset to the last working commit before the change
git reset --hard [LAST COMMIT]
// check out to new feature
git checkout feature
Above command will undo changes in current branch and move them to a new branch.
Reference: http://www.ruanyifeng.com/blog/2019/12/git-undo.html