Cancel git merge but keep local changes

后端 未结 4 1046
灰色年华
灰色年华 2021-02-20 05:40

I started a git merge, but made local changes that I want to keep. I no longer want to merge, and instead continue to work on the local changes. How do I do this?

相关标签:
4条回答
  • 2021-02-20 05:42

    It happened today to me, I need to do a merge in a new branch created from develop with certain changes from another branch...

    I tried the solution from Michael Sorens but it need an additional step... git add . if you have some conflict

    git checkout -b mynewbranchfromdevelop
    git merge AnotherBranchToMergeIntoMynewbranchfromdevelop
    

    it have conflicts, I tried to make a git stash but "needs merge" messages appears, simply what I have to do is to use

    git add .
    

    to take those conflicts as resolved and staged (but still need git commit to conclude merge), then

    git stash
    

    will save those changes in a stash, and if you check with "git status" there is nothing to commit and working tree clean

    git stash apply
    

    and you will have those changes in local directory of current branch without commit

    0 讨论(0)
  • 2021-02-20 05:52

    Language is notoriously ambiguous. :-) When you say "keep local changes" you could mean either:

    1. keep all current changes in the working directory, whether you manually edited those files OR the merge that you are in the middle of brought them in; or
    2. discard any changes brought in by the merge and retain any changes that you have introduced.

    This simple solution addresses point (1):

    $ git stash save
    $ git stash pop
    

    Here's a transcript showing the affect:

    $ git status
    On branch stuff-217/apply-new-config-details
    All conflicts fixed but you are still merging.  <<<<<< notice this line!
    
    Changes to be committed:
            modified:   package.json
            modified:   src/wallabyTest.ts
            modified:   wallaby.js
    
    $ git stash save
    Saved working directory and index state WIP on stuff-217/apply-new-config-details...
    
    $ git status
    On branch stuff-217/apply-new-config-details
    nothing to commit, working tree clean
    
    $ git stash pop
    On branch stuff-217/apply-new-config-details
                                                 <<<<<< no longer in the merge!
    Changes not staged for commit:
            modified:   package.json
            modified:   src/wallabyTest.ts
            modified:   wallaby.js
    
    0 讨论(0)
  • 2021-02-20 05:58

    Found a tested solution for:

    1. keep all current changes in the working directory, whether you manually edited those files OR the merge that you are in the middle of brought them in
    rm .git/MERGE*
    rm .git/ORIG_HEAD
    

    Might be a bit dangerous as you're basically directly interfering with git's internal working, so maybe back up your folder first :)

    0 讨论(0)
  • 2021-02-20 06:06

    First, copy the folder you're working in in case something bad happens. Git is usually pretty bulletproof, but if you start using git reset --hard, it's possible for bad things to happen.

    Then, do a git commit --patch, picking only the changes that you want to keep and leaving everything that the merge did. Once you've committed those changes, do a git reset --hard, and the merge should be gone, but your changes should still be there.

    0 讨论(0)
提交回复
热议问题