Reputation: 16268
Long story short:
I want to undo the merge but keeping the new changes. I'm aware git reset --hard <hash>
will undo a commit, and if I do it 2 times I'll undo the merge. But then what about the new changes? Thanks in advance.
Upvotes: 0
Views: 36
Reputation: 23
You can use git revert <<sha hash of merge commit>> -m 1
The 1 indicates the mainline (or Parent) of the merge commit object that you want to keep. Any file changes brought in by parent 2 will be removed as a result of the revert. Any changes that were made by other commits on the mainline branch will not be affected.
git cat-file -p <<sha hash of merge commit>>
This will show you the parents. The first parent listed in the output will be used if the 1 option is used in the git revert command.
Taking this approach will not delete/rewrite your history, as is the case with a rebase approach.
You should take this approach if you have already shared your code. Otherwise you run the risk of other developers reintroducing the unwanted code in future commits.
Rebasing on shared code will also introduce duplicate commit objects for other developers. This is extremely frowned upon.
The caveat to reverting is that, should you like to introduce the changes from the original merge at a later point, you will need to revert the revert.
Upvotes: 1
Reputation: 23793
You can use rebase
for that :
git rebase -i HEAD~3
You'll then have something like
pick #hash3 Your commit 1 title
pick #hash2 Your commit 1 title
pick #hash1 Your commit 1 title
If you want to remove for example the 2 commits #hash1
and #hash2
, simply do :
pick #hash3 Your commit 1 title
drop #hash2 Your commit 1 title
drop #hash1 Your commit 1 title
Save.
WARNING :
If you rebase before the commit #hash3
(which is the oldest here) and if you've already pushed, you'll have to push
with the force flag : git push -f
. This can be quite dangerous if other people are working on the same remote branch. If you're alone on the repo or at least on the branch, go ahead ;)
Upvotes: 0