Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel git merge but keep local changes

Tags:

git

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?

like image 689
Matt Joiner Avatar asked Jan 21 '16 01:01

Matt Joiner


People also ask

How do I cancel a git merge?

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.

Can a git merge be undone?

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.

What happens if I abort 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.

How do I revert merge changes in local?

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.


3 Answers

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
like image 120
Michael Sorens Avatar answered Sep 21 '22 07:09

Michael Sorens


As per this answer to a similar question, since Git 2.23 (Q3 2019) you can use:

git merge --quit

This will:

  • Forget about the current merge in progress.
  • Leave the index and the working tree as-is.
like image 29
Jeremy Baron Avatar answered Sep 19 '22 07:09

Jeremy Baron


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.

like image 28
ddsnowboard Avatar answered Sep 22 '22 07:09

ddsnowboard