Checkpoint 4.5.1. Read External Git Undoing Summary.
For a humorous summary of undoing in Git, go to Oh Shit, Git!?!
1
ohshitgit.com/
--amend
option:$ git commit --amend
$ git commit -m 'Initial commit' $ git add forgotten_file $ git commit --amend
git add *
and stage them both. How can you unstage one of the two? The git status
command reminds you:$ git add * $ git status On branch main Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: README.md -> README modified: CONTRIBUTING.md
git reset HEAD <file>…
to unstage. So, let’s use that advice to unstage the CONTRIBUTING.md
file:$ git reset HEAD CONTRIBUTING.md Unstaged changes after reset: M CONTRIBUTING.md $ git status On branch main Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: README.md -> README Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: CONTRIBUTING.md
CONTRIBUTING.md
file is modified but once again unstaged.git reset
can be a dangerous command, especially if you provide the --hard
flag. However, in the scenario described above, the file in your working directory is not touched, so it’s relatively safe.git reset
command.CONTRIBUTING.md
file? How can you easily unmodify it — revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? Luckily, git status
tells you how to do that, too. In the last example output, the unstaged area looks like this:Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: CONTRIBUTING.md
$ git checkout -- CONTRIBUTING.md $ git status On branch main Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: README.md -> README
git checkout -- <file>
is a dangerous command. Any local changes you made to that file are gone — Git just replaced that file with the last staged or committed version. Don’t ever use this command unless you absolutely know that you don’t want those unsaved local changes.--amend
commit can be recovered. However, anything you lose that was never committed is likely never to be seen again.git restore
. It’s basically an alternative to git reset
which we just covered. From Git version 2.23.0 onwards, Git will use git restore
instead of git reset
for many undo operations.git restore
instead of git reset
.git restore
. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, let’s say you’ve changed two files and want to commit them as two separate changes, but you accidentally type git add *
and stage them both. How can you unstage one of the two? The git status
command reminds you:$ git add * $ git status On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: CONTRIBUTING.md renamed: README.md -> README
git restore --staged <file>…
to unstage. So, let’s use that advice to unstage the CONTRIBUTING.md
file:$ git restore --staged CONTRIBUTING.md $ git status On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) renamed: README.md -> README Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: CONTRIBUTING.md
CONTRIBUTING.md
file is modified but once again unstaged.CONTRIBUTING.md
file? How can you easily unmodify it — revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? Luckily, git status
tells you how to do that, too. In the last example output, the unstaged area looks like this:Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: CONTRIBUTING.md
$ git restore CONTRIBUTING.md $ git status On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) renamed: README.md -> README
git restore <file>
is a dangerous command. Any local changes you made to that file are gone — Git just replaced that file with the last staged or committed version. Don’t ever use this command unless you absolutely know that you don’t want those unsaved local changes.ohshitgit.com/
git checkout -- <file>
git reset --hard HEAD
git restore <file>
git revert HEAD
git --unamend <file>
git reset <file>