We make mistakes when developing before or after committing in git. We may revert changes and fix our mistake by using git.
Revert entire working tree before committing
- 1 Revert entire working tree before committing
- 2 Revert changes to particular file before committing
- 3 Revert changes to specific file after committing
- 4 Revert a commit
- 5 Revert file deletion
- 6 Discard newly added files
- 7 Remove new added file from index
- 8 Checkout old vision
- 9 Check out a particular version of a file
We can return the entire working tree to the last committed state if we mess up the working tree:
$ git reset --hard HEAD
Revert changes to particular file before committing
We can directly revert the changes to particular file before committing the changes.
We made some stupid changes to file working_file
echo "stupid changes" > working_file
And we haven’t added the changes to the staging index. We can just checkout the old version:
git checkout working_file
Revert changes to specific file after committing
We made another stupid changes to file working_file:
echo "another stupid change" > working_file
The we commit these changes:
git commit -a -m 'another stupid changes'
Checkout and restore the file with the previous version:
git checkout HEAD^ -- working_file
Revert a commit
We made not-wanted changes:
echo "not-wanted change" > working_file
Then we commit these changes:
git commit -a -m 'not-wanted changes'
Then find out the commit name by:
We may find
commit 85191fada91f2d2b1fbb997607309dd8050c07e8 Author: Zhiqiang Ma <firstname.lastname@example.org> Date: Wed Dec 22 12:06:06 2010 +0800 not-wanted changes
Then we can revert the commit ‘not-wanted changes’:
git revert 85191fada91f2d2b1fbb997607309dd8050c07e8
Revert file deletion
We can checkout a deleted file again if we haven’t committed the change .
Delete a file by mistake:
We can revert the deletion by checking out the deleted file:
git checkout deleted_file
Discard newly added files
If we create new files in the working tree but we don’t want to keep them, we can discard and clean up these files.
We can find out new added files in working tree:
Before deleting, we can make a dry-run to see what will happen:
git clean -n
-n is the same as –dry-run
Now we can clean up these file if nothing is wrong
git clean -f
Remove new added file from index
If you added a file to the index but don’t want to commit the file you can remove it from the index via git reset file
We create a file no_use_file:
And add it to the index by mistake:
git add .
We can remove it from the index
git reset no_use_file
Then we can delete the file from the working tree
Checkout old vision
We can also checkout old visions of the working tree.
First, get the log:
Find out the commit name like:
The checkout the older revision by commit name
git checkout 85191fada91f2d2b1fbb997607309dd8050c07e8
Check out a particular version of a file
We can also check out a particular version of a file:
git checkout v1.2.3 -- filename # tag v1.2.3 git checkout stable -- filename # stable branch git checkout origin/master -- filename # upstream master git checkout HEAD -- filename # the version from the most recent commit git checkout HEAD^ -- filename # the version before the most recent commit