Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmerge a git branch, keeping post-merge commits

Tags:

git

git-merge

Say there are two branches master and branchA.

I work on master, a friend on branchA. Once things are finalized, we merge branchA with master.

After several commits on the merged master, the client wants the work done on branchA removed, but NOT the commits done after the merge.

How can we do it?

like image 322
whatf Avatar asked Jan 31 '12 00:01

whatf


People also ask

Can we Unmerge a merged branch?

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.

How do I Unmerge a branch?

In case you are using the Tower Git client, undoing a merge is really simple: just press CMD+Z afterwards and Tower will undo the merge for you!

Should you keep merge commits?

Some people keep a merge commit, even for a fast forward, because it keeps a very good record of when a branch was completed. These people prefer "complete and accurate" history over a "clean" history. Some people avoid merge commits, even rebasing their branches, because it keeps the history easier to read.

Does branch disappear after merging?

In a good workflow, the feature branch is deleted once its merged back into master. New branches should be created for each new feature(s) that you work on.


1 Answers

Read through Pro Git - Undoing Merges.

Basically, you git revert the merge commit:

git revert -m 1 hash_of_merge_commit 

You may end up with some conflicts that you'll have to manually unmerge, just like when merging normally.

Additional links:

  1. Git SCM - Undoing Merges

  2. Git Ready - Rolling back changes with revert

like image 81
Justin ᚅᚔᚈᚄᚒᚔ Avatar answered Sep 23 '22 19:09

Justin ᚅᚔᚈᚄᚒᚔ