Reputation: 7924
I would like to amend a merge commit which failed a unit test on our continuous integration server, but I don't want to leave a commit which fails tests in the history.
Since this commit was only used by the CI server (and would not have been pulled down by anyone else) I would like to fix up the merge and then --force
push it to replace the existing failed merge commit.
My problem is, I can't work out how to easily get back to the state immediately before the original commit was made, so that I can fix the failing test, recommit the merge and then force push that change up.
I don't really want to have to redo the whole merge, since there are quite a few files with conflicts which had to be resolved and all but one was resolved successfully.
git reset
My first attempt was to do a mixed reset back to the commit before the merge commit, fix the bug introduced by the merge and then recommit.
git reset HEAD^
# Fix the failing test
git commit
Unfortunately this results in a non-merge commit which silently incorporates all of the branch changes - not really what I want. *8')
reset
, stash
, merge
& apply
I then attempted to use stash to save the changes I made during my first attempt to merge:
git reset HEAD^
git add . # Since there were untracked files
git stash
git merge branch
git stash apply # Fails
git add .
git stash apply # Merged but with conflicts
Unfortunately when git stash apply
attempts to merge my stashed changes with the existing conflicted merge files it fails with the error
Cannot apply to a dirty working tree, please stage your changes
If I dutifully make my working directory clean by using git add .
then the git stash apply
runs but now not only do I have conflicts, I have conflicts with conflicts in them and any files which should have been removed have been added back in.
amend
and/or rebase
.I tried the nice simple suggestion by qqx to just fix the problem and git commit --amend
the original commit, but without actually going back to the mid-merge state you lose access to the merge tools.
Once I've committed, I lose the ability to run git mergetool {file}
as it just returns with the error:
{file}: file does not need merging
When trying to fix up a merge conflict, I really like being able to see my base, left, right and current revisions all together using tools like kdiff3
.
using rebase
suffers the same issue.
Any hints on a workflow which could allow me to achieve my desired result would be appreciated.
Upvotes: 1
Views: 4443
Reputation: 19465
git branch old_merge
git checkout -b new_merge HEAD~
git merge branch
Would get you back into pending merge state, where you can use the normal merge tools. Since the old merge is saved as the old_merge
branch you can also use git checkout old_merge {file}
to use the choices that you made for a particular file on your previous attempt. Once you're done you can commit that as normal, then checkout the branch that you were previously working on and use git reset --hard new_merge
to switch to using the new merge commit.
Upvotes: 1
Reputation: 19465
Since you're just resetting the most recent commit it seems that you haven't done any other commits since the one that is problematic. Because of this you should be able to just fix the code, use git add
on the changed files, then use git commit --amend
to replace the existing merge commit. That will preserve the merge information.
Upvotes: 4