Git HowTo - Revert A Commit Already Pushed To A Remote Repository - Christoph Rüegg
Git HowTo - Revert A Commit Already Pushed To A Remote Repository - Christoph Rüegg
ABOUT
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 ;)
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.
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.
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:
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:
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.
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!
Log in with
Name
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
−
+
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
−
+
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
−
+
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
−
+
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
@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
−
+
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
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