So I made a pretty big mistake. I made a commit, pulled, merged (but messed up the code while doing so) and then pushed. I'd like to redo that merge and get the code right. Is there any way to do this?
I use bitbucket.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communitySo I made a pretty big mistake. I made a commit, pulled, merged (but messed up the code while doing so) and then pushed. I'd like to redo that merge and get the code right. Is there any way to do this?
I use bitbucket.
Not sure if this is the "blessed" way to do it, but here's what I did to resolve the same problem without having to "force push" or anything gross like that.
Let's assume your history looks something like this (and M is the flubbed merge):
-A--B--C--M (master points here)
\ /
D----E
Running git checkout -b merge_fix <commit ID E>
creates a branch before we made any mistakes:
-A--B--C--M (master points here)
\ /
D----E (HEAD and merge_fix point here)
Now, let's re-do the merge on our new branch. We can't just merge in master
, so we need to manually pick the commit before our bad merge: git merge <commit ID C>
Don't make the same mistakes you did last time!
-A--B--C--M (master points here)
\ X
D----E-G (HEAD and merge_fix point here)
Assuming that commit G
looks good, now we want to sync up with the top of the master
branch. This command tells git to ignore the changes that were made to master, and force our changes to become the merge result: git merge -s ours master
-A--B--C--M (master points here)
\ X \
D----E-G--H (HEAD and merge_fix point here)
Finally, (again assuming that commit H
looks good, we want to fast-forward master
to include our fixed merge:
git checkout master
git merge merge_fix
This really just moves the master
branch pointer to H
, but I'll take the opportunity to clean up my ASCII art a bit:
-A--B--C--M--H (HEAD, master, and merge_fix all point here)
\ X /
D----E--G
And there you have it! you've successfully re-done the merge without invalidating any history!
M
before beginning to fix, their changes might be overwritten by this method. I've had success in preserving these changes by skipping git merge -s ours master
, instead immediately doing git checkout master
, then finally git merge -s resolve merge_fix
. This alternate method works best if those further commits are not in conflict with merge_fix
.
M
, and git merge -s ours M
is done with the "fubbed" commit M
first. This step is basically just discarding M
changes. And then it can be merged with master's HEAD.
Feb 1, 2021 at 22:51
You can do it, like this:
That is:
git reset --hard SHA1
git merge branchname
git commit
git push --force remotename branchname
Just keep in mind that git push --force
will rewrite whatever you had at the remote branch, and other people using that branch may be affected by this too. (Normally you should not do this.)
git push --force
is git push --force-with-lease
; e.g. in the last step use git push --force-with-lease remotename branchname
Nov 8, 2021 at 14:48
git merge --abort
and then you can merge again
try
git checkout .
git merge --abort
this will undo any local changes during merge, and then revert back to pre-merge state. if you already did a commit or merge continue, you'll need to do the reset HEAD~ steps
I had a different, somewhat more complicated setup.
there's basically three starting points: (and the other solutions are all assuming either 1 or 2)
redo-branch
should be completely merged AND redo-branch
there have been no other mergesredo-branch
should be completely merged AND redo-branch
there have already been other merges, that should be keptredo-branch
is a long-living branch to be repeatedly merged ANDa setup for (3) would be
---A--B--C--M (master points here)
/ /
-X--D-----E--Y (that's the long-living redo-branch)
# let's start on the last commit before the failed merge
git checkout redo-branch
git checkout -b D^1 new-branch-for-merge
# now let's pick all the revisions D through E
git cherry-pick D^1..E
# go to the target branch (master) and merge
git checkout master
git merge new-branch-for-merge
Please check this hope it's useful for you
https://www.kernel.org/pub/software/scm/git/docs/howto/revert-branch-rebase.html