threads / discuss / 29104

Undo a commit that is already pushed to central server and merged to several branches

Subject: Undo a commit that is already pushed to central server and merged to several branches

## tl;dr

6 messages between Dec 7, 2011 and Dec 8, 2011.

replies: 5people: 3as markdown or json

Matthias Fechner· Dec 7, 2011, 14:15 UTC · lore
Dear git list,

I commited two files into the master branch (later I figured out that the files included a lot bugs).

I continued to work then on different branches and merged the bad master branch to all my other branches.

I pushed the master branch then to the server including the bogus commit.

What I would like to do is move this bogus commit into a different branch and remove all changes from this bogus commit from every branch.

Is this possible and how?

Bye Matthias

-- 
"Programming today is a race between software engineers striving to 
build bigger and better idiot-proof programs, and the universe trying to 
produce bigger and better idiots. So far, the universe is winning." -- 
Rich Cook
Ramkumar Ramachandra· Dec 7, 2011, 14:42 UTC · re: Matthias Fechner · lore

Re: Undo a commit that is already pushed to central server and merged to several branches

Hi Matthias,
Matthias Fechner wrote:
Show 7 quoted lines
> [...]
> I continued to work then on different branches and merged the bad master
> branch to all my other branches.
> [...]
>
> What I would like to do is move this bogus commit into a different branch
> and remove all changes from this bogus commit from every branch.
If I understand correctly, each of your branches looks like*:
o  <--- HEAD of branch; merge commit referencing bogus commit
| \
o  \   <--- This is where you want to move the HEAD to
|   \
o    o  <-- Bogus commit from master branch
|
o
|
o  <-- Branch born

Assuming that you actually want to rewrite the history, the situation calls for a git-reset(1). Just "git reset --hard HEAD~1" on each of your branches (Caution: first understand what it does!) and you'll rewind the HEAD to "undo" the bad merge. After that you can just "git push +foo:foo" to overwrite the foo branch on your server. If you don't want to rewrite anything and simply commit the inverse of the bad commit, see git-revert(1).

* If you're having difficulty understanding the diagram, please read:
http://eagain.net/articles/git-for-computer-scientists/
Cheers.
-- Ram
Matthias Fechner· Dec 7, 2011, 15:35 UTC · re: Ramkumar Ramachandra · lore

Re: Undo a commit that is already pushed to central server and merged to several branches

Am 07.12.11 15:42, schrieb Ramkumar Ramachandra:
> Assuming that you actually want to rewrite the history, the situation
> calls for a git-reset(1).  Just "git reset --hard HEAD~1" on each of
> your branches (Caution: first understand what it does!) and you'll

that will not work, because in the master branch I already have around 15 commits after the bogus commit and I want to keep these commits.

And in some other branches there are tons of commits (over 100) after the bogus commit with the merge and I only want to remove that bogus commit but keep all other commits after the bogus commit (all commits are not related to the bogus one, they are all in different files).

It is even fine if I could remove the bogus commit from all branches.

Bye Matthias

-- 
"Programming today is a race between software engineers striving to 
build bigger and better idiot-proof programs, and the universe trying to 
produce bigger and better idiots. So far, the universe is winning." -- 
Rich Cook
Ramkumar Ramachandra· Dec 7, 2011, 16:01 UTC · re: Matthias Fechner · lore

Re: Undo a commit that is already pushed to central server and merged to several branches

Hi Matthias,
Matthias Fechner wrote:
Show 14 quoted lines
> Am 07.12.11 15:42, schrieb Ramkumar Ramachandra:
>
>> Assuming that you actually want to rewrite the history, the situation
>> calls for a git-reset(1).  Just "git reset --hard HEAD~1" on each of
>> your branches (Caution: first understand what it does!) and you'll
>
>
> that will not work, because in the master branch I already have around 15
> commits after the bogus commit and I want to keep these commits.
>
> And in some other branches there are tons of commits (over 100) after the
> bogus commit with the merge and I only want to remove that bogus commit but
> keep all other commits after the bogus commit (all commits are not related
> to the bogus one, they are all in different files).

I see. I won't attempt to repeat what Junio and Linus have explained in this document then [1].

Cheers.
[1]: http://schacon.github.com/git/howto/revert-a-faulty-merge.txt
-- Ram
Johan Herland· Dec 7, 2011, 16:01 UTC · re: Matthias Fechner · lore

Re: Undo a commit that is already pushed to central server and merged to several branches

On Wed, Dec 7, 2011 at 16:35, Matthias Fechner <idefix@fechner.net> wrote:
Show 16 quoted lines
> Am 07.12.11 15:42, schrieb Ramkumar Ramachandra:
>>
>> Assuming that you actually want to rewrite the history, the situation
>> calls for a git-reset(1).  Just "git reset --hard HEAD~1" on each of
>> your branches (Caution: first understand what it does!) and you'll
>
>
> that will not work, because in the master branch I already have around 15
> commits after the bogus commit and I want to keep these commits.
>
> And in some other branches there are tons of commits (over 100) after the
> bogus commit with the merge and I only want to remove that bogus commit but
> keep all other commits after the bogus commit (all commits are not related
> to the bogus one, they are all in different files).
>
> It is even fine if I could remove the bogus commit from all branches.

Use "git revert $commit" to undo the effects of the given $commit. This must be applied to all affected branches (either by reverting in the master branch and remerging master to the other branches, or by using "git revert" in each individual branch).

Note that the "git revert" command will NOT remove the original bogus commit. Instead, it will create a new commit that undoes the changes introduced by the bogus commit. If the same lines/files have been touched by other commits in the meantime, you might get conflicts that you'll have to resolve, just like a regular merge.

If you'd rather want the bogus commit to disappear entirely (i.e. rewrite the history so that it looks like the bogus comit never happened in the first place), then things get more complicated. You will need to recreate the history of your master branch, skipping the bogus commit, and then recreate all the other branches in a similar manner. The "git rebase" tool will be helpful here, but I recommend that you understand and get used to how it works before applying it to this problem. Note that rewriting history is only a good idea when you haven't shared the history with anyone else. If other people are already using any of the branches with the bogus commit, then rewriting the history is a BIG NO-NO. In that case, "git revert" is the only sane approach.

Have fun! :)
...Johan
-- 
Johan Herland, <johan@herland.net>
www.herland.net
Matthias Fechner· Dec 8, 2011, 09:46 UTC · re: Johan Herland · lore

Re: Undo a commit that is already pushed to central server and merged to several branches

Dear Ramkumar and Johan,
Am 07.12.11 17:01, schrieb Johan Herland:
> Use "git revert $commit" to undo the effects of the given $commit.
> This must be applied to all affected branches (either by reverting in
> the master branch and remerging master to the other branches, or by
> using "git revert" in each individual branch).

thanks a lot for your help. The steps I did now was at first undo everything i did locally with: git reset --hard origin/master

Then undo the bad commit: git revert commit-id git commit

Now the bad commit was undone and I merged the master branch in all other branches.

I create a new branch and cherry-picked the bad commit into it so I can correct the problem there and later merge this branch in all the other ones.

Hopefully this short summary will help other users having the same problem.

Bye Matthias

-- 
"Programming today is a race between software engineers striving to 
build bigger and better idiot-proof programs, and the universe trying to 
produce bigger and better idiots. So far, the universe is winning." -- 
Rich Cook

← back to recent threads