f960dd3
was a mistake and we wish to undo it:$ git log --oneline
f960dd3 (HEAD -> master) not sure this is a good idea
40fbb90 draft a readme
dd4472c we should not forget to enjoy
2bb9bb4 add half an onion
2d79e7e adding ingredients and instructions
A safe way to undo the commit is to revert the commit with git revert
:
$ git revert f960dd3
This creates a new commit that does the opposite of the reverted commit. The old commit remains in the history:
$ git log --oneline
d62ad3e (HEAD -> master) Revert "not sure this is a good idea"
f960dd3 not sure this is a good idea
40fbb90 draft a readme
dd4472c we should not forget to enjoy
2bb9bb4 add half an onion
2d79e7e adding ingredients and instructions
git revert
.git log --oneline
.git show
on both the reverted and the newly created commit.Sometimes we commit but realize we forgot something. We can amend to the last commit:
$ git commit --amend
This can also be used to modify the last commit message.
Note that this will change the commit hash. This command modifies the history. This means that we never use this command on commits that we have shared with others.
This is a command that permanently deletes changes that were unstaged/uncommitted!
git status
and git diff
.git checkout <file>
.git status
and git diff
.git status
, git diff
, git diff --staged
, and git diff HEAD
.git checkout <file>
.git status
, git diff
, git diff --staged
, and git diff HEAD
.git rm
, is it gone forever?