`git stash` during a merge conflict

前端 未结 5 940
猫巷女王i
猫巷女王i 2021-02-02 08:46

We\'ve done something bad.

We ran git stash save during a merge conflict, and now we can\'t restore our work.

Things we\'ve tried:

g         


        
5条回答
  •  无人及你
    2021-02-02 08:54

    I did the same thing today, and took a different approach (after trial and error) to get back to the state just prior to stashing so I could continue resolving conflicts and complete the merge.

    First, after unstashing the partial merge in the destination branch, I captured a list of the files with remaining conflicts (text file or editor tab). This is just the list of unstaged files after unstashing, as the files with conflicts already resolved would have been staged prior to stashing.

    $ git status
    # Changes not staged for commit:
    #   (use "git add ..." to update what will be committed)
    #   (use "git checkout -- ..." to discard changes in working directory)
    #
    #   modified:   myproject/src/main/java/com/acme/package3/Class3.java
    #   modified:   myproject/src/main/java/com/acme/package3/Class4.java
    #
    

    Next, I created a patch and reset the branch back to the pre-merge state:

    $ git diff HEAD > ~/merge-with-resolved-conflicts.patch
    $ git reset --hard HEAD
    

    Then I created a temporary branch (derived from the merge destination branch), and applied the patch:

    $ git checkout -b my-temp-branch
    $ git apply ~/merge-with-resolved-conflicts.patch
    $ git commit -a -m "Merge with resolved conflicts"
    

    So the HEAD of my-temp-branch now contains everything that was merged, including files with conflicts resolved, and files with remaining conflicts.

    Then I switched back to original branch, merged again, and looked at the git status

    $ git checkout my-branch
    $ git merge other-branch
    $ git status
    

    The status shows the full list of files with conflicts:

    # Unmerged paths:
    #   (use "git add ..." to mark resolution)
    #
    #   both modified:      myproject/src/main/java/com/acme/package1/Class1.java
    #   both modified:      myproject/src/main/java/com/acme/package2/Class2.java
    #   both modified:      myproject/src/main/java/com/acme/package3/Class3.java
    #   both modified:      myproject/src/main/java/com/acme/package3/Class4.java
    #
    

    Now I needed to compare these two lists of files. Any files in the second list but not the first had already been resolved (in this example, Class1.java and Class2.java). So for each of those files, I pulled in the version with conflicts resolved from the temporary branch (like cherry-pick, but for individual files rather than an entire commit):

    $ git checkout my-temp-branch myproject/src/main/java/com/acme/package1/Class1.java
    $ git checkout my-temp-branch myproject/src/main/java/com/acme/package2/Class2.java
    

    Having done this, I was back to the state before the stash, so I could resume resolving the remaining conflicts and commit the merge.

提交回复
热议问题