Undoing and recovering
One of the main points of version control is that you can go back in time to recover. Let's have look at safe commands that do not modify the commit history.
Undoing uncommitted changes#
Unstaged file#
- Let's edit
config.yml
and remove the linesample_ids: ["SRR935090","SRR935091","SRR935092"]
. Rungit status
, it will tell you that there are modifications in one file (config.yml
) compared to the previous commit.
For unstaged file it is possible to have details about the modifications made.
git diff config.yml
You now realize that this modification was wrong. Use git restore
to remove all modifications made since last commit.
git restore config.yml
git status
Staged file#
- Let's edit
config.yml
, remove the linesample_ids: ["SRR935090","SRR935091","SRR935092"]
, stage the file and rungit status
.
Stage and commit the first file
git add config.yml
git status
Git tells us that there is a staged file. You now realize that this modification was wrong. You have to first unstage the file before to clean the modifications:
git restore --stage config.yml
git status
git restore config.yml
git status
Tip
In case you want to throws away everything that is not in last commit (HEAD) you can use git reset --hard HEAD
. It is apply on all files, staged and unstaged.
Undoing committed changes#
- Let's edit
config.yml
, remove the linesample_ids: ["SRR935090","SRR935091","SRR935092"]
, stage the file, commit, and rungit status
.git add config.yml git commit -m "remove sample_ids line" git status
The git status
is not really helpfull here as we already commited our modifications
git log
The git log
command show us the message we entered in previous commits.
To see modification made in the previous commit you can use:
git diff HEAD^
Tip
Too see the modifications between the last commit and a particular commit you can use git diff <commit_id>
. It is also possible to compare the differences between two commits: git diff <commit_id1> <commit_id2>
.
You now realize that the latest commit was a mistake and you wish to undo it:
git log --online
git revert <commit>
This creates a new commit that does the opposite of the reverted commit. The old commit remains in the history.
Note
You can revert any commit, no matter how old it is. It doesn’t affect other commits you have done since then - but if they touch the same code, you may get a conflict (which we’ll learn about later).
Quick recap
We added four important Git commands to our repertoire:
git diff <file>
list all modifications made on a file since last commitgit restore <file>
cancels the modifications of an unstaged filegit restore --staged <file>
unstage a staged filegit revert <commit>
does the opposite of the reverted commit