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?
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
Language is notoriously ambiguous. :-) When you say "keep local changes" you could mean either:
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
Found a tested solution for:
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 :)
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.