Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse a merge commit (Git)?

So my colleague was editing some code on a repo, however she had not pulled some recent changes and so the software would not allow her to commit her changes.

We went into the shell and pulled the commits that she had omitted and she was able to push, the only problem is that it drew those changes into her diverged branch and attributed those commits to her.

It looks like this:

A-B-C-D-E (say D and E were by someone else)

My colleague had this:

A-B-C, and wanted to add some more on.

So she pulled D and E and pushed those changes and now it looks like this:

A-B-C-D-E-F, except that the changes D, E, and F are now from her account.

Is there away to keep her changes (F) but have the timeline revert to when D and E were attributed to someone else?

Thanks!

Graphic representation of problem:

A-B-C-D-E (most recent version of master)

A-B-C (colleague has this)

A-B-C-F (colleague makes edits) (branches hath diverged)

!ERROR! (colleague tries to commit and push)

A-B-C-D-E-F (colleague merges master into her branch)

A-B-C-D-E-F (colleague pushes)

GitHub UI now does not display changes D-E, they are lumped into a "Merge branch 'master' ..."

like image 865
James Yen Avatar asked Jul 13 '16 22:07

James Yen


People also ask

Can a git merge be reversed?

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 cancel a 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.

How do you reverse a merge conflict?

In case you've made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with "git reset --hard " and start over again.


1 Answers

With git log check which commit is the one before the merge. Note the sha.

Then you can reset it using: git reset --hard commit_sha

Also if you want to, using your example, remove D and E then do the following. Except it will also remove F. That is, the last 3. git reset --hard HEAD~3

like image 76
Manil Avatar answered Sep 28 '22 04:09

Manil