Git Undo Redo A Commit - ashish9342/FreeCodeCamp GitHub Wiki
You would typically want to UNDO/REDO when you commit some changes to git, and realize that the changes need to be removed/reverted. This very common in scenarios for example, when you did a temporary change to some files, and forgot to revert them, then proceeded to adding them to commit accidentally.
Assuming you did some changes and made commits like:
git commit -m "Commit 1 - Some changes to the code"
git commit -m "Commit 2 - Some MORE changes to the code"
- (UNDO-ing): Revert back the last commit
git reset --soft HEAD~
- Do the changes.
- Add your files to the staging area
git add <filenames or paths>
orgit add --all
- (REDO-ing): Do the commit.
git commit -c ORIG_HEAD
orgit commit -C ORIG_HEAD
Now that you know the flow lets understand how this works behind the scenes.
-
Step 1
resets the last commit i.e."Commit 2 - Some MORE..."
back to the"Commit 1 - Some..."
commit. - In
Step 2
, you do changes you deem fit to the files. - In
Step 3
, you add the changed files to the staging area either selectively withgit add <filenames>
or all files withgit add --all
. - In the final step you commit the changes in the staging area.
Note: you can either use -c
or -C
. The small -c
will open an editor for modifying the commit message, in this case it will be Commit 2 - Some MORE...
. You can edit the commit message as you want.
Or alternatively you can use caps -C
, where git will skip the editor window, and reuse the LAST commit message which again in this case is Commit 2 - Some MORE...
.
Re-using the "Same" commit message is also known as redoing/recommiting.
To undo a change staged before a commit simply run git reset <file>
or git reset
to unstage all changes before a commit.
Note: In older versions of git, the commands were git reset HEAD <file>
and git reset HEAD
respectively. This was changed in Git 1.8.2
You can go back any number of commits by using git reset --soft HEAD~n
where you want to undo last n
commits.
This article is based on a Stack Overflow question here and here.