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?
How do I cancel a git merge? Use git-reset or git merge --abort to cancel a merge that had conflicts. Please note that all the changes will be reset, and this operation cannot be reverted, so make sure to commit or git-stash all your changes before you start a merge.
You can undo a Git merge using the git reset –merge command. This command changes all files that are different between your current repository and a particular commit. There is no “git undo merge” command but the git reset command works well to undo a merge.
So the “git merge –abort” operation essentially terminates the merger that you have just carried out and separated the two versions of your file, i.e., the current version and the older version.
You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.
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
As per this answer to a similar question, since Git 2.23 (Q3 2019) you can use:
git merge --quit
This will:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With