How to Revert Changes in Git

Posted on In Linux

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

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:

git log

We may find

commit 85191fada91f2d2b1fbb997607309dd8050c07e8
Author: Zhiqiang Ma <eric.zq.ma@gmail.com>
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:

rm deleted_file

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:

git status

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:

touch 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

rm no_use_file

Checkout old vision

We can also checkout old visions of the working tree.

First, get the log:

git log

Find out the commit name like:

commit 85191fada91f2d2b1fbb997607309dd8050c07e8

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

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

One comment

  1. To delete a local commit (not revert, a revert is a commit) that is not pushed to the remote server yet, such as the HEAD:

    $ git reset –hard HEAD^

    The reset the working repository to one commit before HEAD. Hence, HEAD is “deleted”.

Leave a Reply

Your email address will not be published. Required fields are marked *