0% found this document useful (0 votes)
40 views

Git HowTo - Revert A Commit Already Pushed To A Remote Repository - Christoph Rüegg

The document discusses how to revert a commit that has already been pushed to a remote repository in Git. It provides three alternatives: correcting the mistake in a new commit, reverting the full commit, or rewriting history by rebasing. It gives examples for deleting the last commit, deleting the second last commit, and fixing a typo in one commit.

Uploaded by

max
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Git HowTo - Revert A Commit Already Pushed To A Remote Repository - Christoph Rüegg

The document discusses how to revert a commit that has already been pushed to a remote repository in Git. It provides three alternatives: correcting the mistake in a new commit, reverting the full commit, or rewriting history by rebasing. It gives examples for deleting the last commit, deleting the second last commit, and fixing a typo in one commit.

Uploaded by

max
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

8/15/2020 Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg

ABOUT

Git HowTo: revert a


commit already pushed to
a remote repository
MAY 2010 on Git

So you've just pushed your local branch to a remote branch, but then
realized that one of the commits should not be there, or that there was
some unacceptable typo in it. No problem, you can fix it. But you
should do it rather fast before anyone fetches the bad commits, or you
won't be very popular with them for a while ;)

First two alternatives that will keep the history intact:

Alternative: Correct the mistake in


a new commit
Simply remove or fix the bad file in a new commit and push it to the
remote repository. This is the most natural way to fix an error, always
safe and totally non-destructive, and how you should do it % of the
time. The bad commit remains there and accessible, but this is usually
not a big deal, unless the file contains sensitive information.

Alternative: Revert the full commit


Sometimes you may want to undo a whole commit with all changes.
Instead of going through all the changes manually, you can simply tell

file:///C:/Users/test/Dropbox/IT/Help/05-GIT/Git HowTo_ revert a commit already pushed to a remote repository _ Christoph Rüegg.html 1/10
8/15/2020 Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg

git to revert a commit, which does not even have to be the last one.
Reverting a commit means to create a new commit that undoes all
changes that were made in the bad commit. Just like above, the bad
commit remains there, but it no longer affects the the current master
and any future commits on top of it.

1: $ git revert dd61ab32

About History Rewriting


People generally avoid history rewiriting, for a good reason: it will
fundamentally diverge your repository from anyone who cloned or
forked it. People cannot just pull your rewritten history as usual. If they
have local changes, they have to do some work to get in sync again;
work which requires a bit more knowledge on how Git works to do it
properly.

However, sometimes you do want to rewrite the history. Be it because


of leaked sensitive information, to get rid of some very large files that
should not have been there in the first place, or just because you want
a clean history (I certainly do).

I usually also do a lot of very heavy history rewriting when converting


some repository from Subversion or Mercurial over to Git, be it to
enforce internal LF line endings, fixing committer names and email
addresses or to completely delete some large folders from all
revisions. I recently also had to rewrite a large git repository to get rid
of some corruption in an early commit that started causing more and
more problems.

Yes, you should avoid rewriting history which already passed into other
forks if possible, but the world does not end if you do nevertheless. For
file:///C:/Users/test/Dropbox/IT/Help/05-GIT/Git HowTo_ revert a commit already pushed to a remote repository _ Christoph Rüegg.html 2/10
8/15/2020 Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg

example you can still cherry-pick commits between the histories, e.g.
to fetch some pull requests on top of the old history.

In opensource projects, always contact the repository maintainer first


before doing any history rewriting. There are maintainers that do not
allow any rewriting in general and block any non-fastforward pushes.
Others prefer doing such rewritings themselves.

Case 1: Delete the last commit


Deleting the last commit is the easiest case. Let's say we have a
remote mathnet with branch master that currently points to commit
dd ab . We want to remove the top commit. Translated to git
terminology, we want to force the master branch of the mathnet remote
repository to the parent of dd ab :

1: $ git push mathnet +dd61ab32^:master

Where git interprets x^ as the parent of x and + as a forced non-


fastforward push. If you have the master branch checked out locally,
you can also do it in two simpler steps: First reset the branch to the
parent of the current commit, then force-push it to the remote.

1: $ git reset HEAD^ --hard


2: $ git push mathnet -f

Case 2: Delete the second last


commit

file:///C:/Users/test/Dropbox/IT/Help/05-GIT/Git HowTo_ revert a commit already pushed to a remote repository _ Christoph Rüegg.html 3/10
8/15/2020 Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg

Let's say the bad commit dd ab is not the top commit, but a slightly
older one, e.g. the second last one. We want to remove it, but keep all
commits that followed it. In other words, we want to rewrite the history
and force the result back to mathnet/master. The easiest way to rewrite
history is to do an interactive rebase down to the parent of the
offending commit:

1: $ git rebase -i dd61ab32^

This will open an editor and show a list of all commits since the commit
we want to get rid of:

1: pick dd61ab32
2: pick dsadhj278
3: ...

Simply remove the line with the offending commit, likely that will be the
first line (vi: delete current line = dd ). Save and close the editor (vi:
press :wq and return). Resolve any conflicts if there are any, and your
local branch should be fixed. Force it to the remote and you're done:

1: $ git push mathnet -f

Case 3: Fix a typo in one of the


commits
This works almost exactly the same way as case , but instead of
removing the line with the bad commit, simply replace its pick with

file:///C:/Users/test/Dropbox/IT/Help/05-GIT/Git HowTo_ revert a commit already pushed to a remote repository _ Christoph Rüegg.html 4/10
8/15/2020 Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg

edit and save/exit. Rebase will then stop at that commit, put the

changes into the index and then let you change it as you like. Commit
the change and continue the rebase (git will tell you how to keep the
commit message and author if you want). Then push the changes as
described above. The same way you can even split commits into
smaller ones, or merge commits together.

Christoph Rüegg Share this post

file:///C:/Users/test/Dropbox/IT/Help/05-GIT/Git HowTo_ revert a commit already pushed to a remote repository _ Christoph Rüegg.html 5/10
8/15/2020 Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg

35 commentsComments
Christoph RüeggCommunity
Login
Disqus
Facebook
Twitter
Google
1

Recommend Recommended 18

Discussion Recommended!
Recommending means this is a discussion worth sharing. It gets shared to
your followers' Disqus feeds, and gives the creator kudos!

Find More Discussions


Share
Share this discussion on
Twitter
Facebook
Sort by Best
Best
Newest
Oldest

Join the discussion…

Log in with

or sign up with Disqus or pick a name

Disqus is a discussion network


Disqus never moderates or censors. The rules on this community are its own.
Don't be a jerk or do anything illegal. Everything is easier that way.

Read full terms and conditions

Name

Email

file:///C:/Users/test/Dropbox/IT/Help/05-GIT/Git HowTo_ revert a commit already pushed to a remote repository _ Christoph Rüegg.html 6/10
8/15/2020 Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg

Password

By signing up, you agree to the Disqus Basic Rules, Terms of Service, and Privacy
Policy.
By posting, you agree to the Disqus Basic Rules, Terms of Service, and Privacy
Policy.
I'd rather post as a guest


+

mltsy • 6 years ago

I would recommend looking into `git revert` before using any of these strategies
on a remote repo. Rebasing and force pushing on a shared repo can cause issues
for people downwind of your changes. Revert was designed specifically for this
purpose! :) (I'm just researching it myself, not having used it before)

see more

35

Reply

Share ›
Twitter
Facebook


+

user z mltsy • 7 days ago

revert leaves changes in files. Some people don't want changes to remain
in files, especially if it was a mistake.

see more

0

Reply

Share ›
Twitter
file:///C:/Users/test/Dropbox/IT/Help/05-GIT/Git HowTo_ revert a commit already pushed to a remote repository _ Christoph Rüegg.html 7/10
8/15/2020 Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg
Facebook


+

Κώστας Λουπασάκης mltsy • 10 months ago

True. My google search results for "git revert commit" were giving me
tutorials for git rebase up to the middle of the page.

see more

0

Reply

Share ›
Twitter
Facebook


+

Christoph Rüegg Mod • 6 years ago

Thanks for your feedback. I've extended the post to explain the issue a bit, and
now fist mention the non-destructive approach with `git revert`. I hope that
prevents some pain caused by unnecessary history rewrites.

see more

15

Reply

Share ›
Twitter
Facebook


+
file:///C:/Users/test/Dropbox/IT/Help/05-GIT/Git HowTo_ revert a commit already pushed to a remote repository _ Christoph Rüegg.html 8/10
8/15/2020 Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg

Citizen Forker • 6 years ago

@mltsy is 100% right, I've just experienced the horror of the leaked malicious
commit being pushed again into remote repo.

see more

5

Reply

Share ›
Twitter
Facebook


+

lorcha • 5 years ago

So in the case of pushing rewritten history to a project repository, and it breaks


everybody's clones and working copies and local changes and whatnot, how do
those users with the broken local repositories fix the damage?

see more

3

Reply

Share ›
Twitter
Facebook


+

Ch i t h Rü M dl h 5
file:///C:/Users/test/Dropbox/IT/Help/05-GIT/Git HowTo_ revert a commit already pushed to a remote repository _ Christoph Rüegg.html 9/10
8/15/2020 Git HowTo: revert a commit already pushed to a remote repository | Christoph Rüegg
Christoph Rüegg Mod lorcha • 5 years ago

Once you fetch the new tree you'll end up with two trees in parallel: the
old one and the new rewritten one. You need to get rid of the old one
entirely in your local repository. So you'll want to migrate all your local
work over to sit on top of the new tree. The easiest ways to do this is
cherry-picking or rebasing (rebasing might require some conflict
resolution though). Just be sure *not* to merge between the two trees,
ever! Once everything is over there, drop all local remaining refs (e.g.
branches) to the old tree and gc/prune your repository it to get rid of it
entirely.

see more

1

Reply

Share ›
Twitter
Facebook

Christoph Rüegg © 2016 Proudly published with FsBlog

file:///C:/Users/test/Dropbox/IT/Help/05-GIT/Git HowTo_ revert a commit already pushed to a remote repository _ Christoph Rüegg.html 10/10

You might also like