Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: how to go back before merge

Tags:

git

Thanks for your help. I am really confused...

I merged a pull request for a team mate.

Now I wish to go back to the situation before the merge, because I have realized that bugs are introduced. The pull request was done by a friend on the 24, I did the merge on the 24.

  • I need to go back to the situation of the 20.
  • what will happen to the changes done by the team mate? Will I be able to see/use them again?

Thanks!!!

commit ff1e9ecfa13c5050957b77bd5b5695d802415867
Merge: 053c894 437e11c
Author: me
Date:   Tue Mar 24 09:06:11 2015 +0100

Merged ' changes on xxx

commit 437e11c23ae7da68f8bb3698557bdc2aa3d9a614
Merge: a129401 adfd8c5
Author: someone else
Date:   Tue Mar 24 00:00:18 2015 +1000

Merge pull request #17 from 

xxx

commit adfd8c5c30541095ee48ce64da14740f92594ec8
Author: someone else
Date:   Mon Mar 23 23:54:06 2015 +1000

xx

commit 053c89426460ffaa86873bed342bc38e24a13096
Merge: 849ec63 35fdbc6
Author: me
Date:   Fri Mar 20 07:57:13 2015 +0100
like image 227
Lisa Anne Avatar asked Mar 24 '15 13:03

Lisa Anne


People also ask

How do you go back before a merge?

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.

How do I get out of git merge?

You can use the git reset --merge command. You can also use the git merge --abort command.

How do I go back in git?

Use git checkout & the ID (in the same way you would checkout a branch) to go back: $ git checkout <commit-id> .

How do I undo a last merge request?

After the merge request has been merged, use the Revert button to revert the changes introduced by that merge request. After you click that button, a modal appears where you can choose to revert the changes directly into the selected branch or you can opt to create a new merge request with the revert changes.


1 Answers

Assuming you didn't delete the branch on which the original changes appear, then the work from your teammate is still sitting on that branch, so I wouldn't worry about losing those changes so long as you still have that branch where the changes were made.

You can do a git reset --hard [commit hash] to go back to the commit before the merge, and it's like traveling back in time. However, you'll also undo any changes that were added after the commit you specify in the reset command, if you have any.

Ex:

On my current branch I have the following commits:

A
|
B    <- commit *before* the merge
|
|
|  --- some other branch
| /
|/
C    <- commit where "some other branch" was merged into current branch
|
D
|
E

If I do git reset --hard B then I'll go back to the commit before the merge at C. But I'll also lose commits D and E in the process. Once you do that, just check the state of the branch and make sure it looks the way you need it to, then do a git push --force, which will update the remote repo to this state. The reason you need --force is because you'll be deleting commits D and E from your current branch on the remote repo, and git won't let you do that without the --force option, since you're asking it to delete work.


Depending on how substantial these changes are, sometimes it's easier to just roll forward and fix the bugs, than to try to undo the changes. Where that line is (to go forward or back) is a case-by-case decision.

like image 65
jefflunt Avatar answered Sep 20 '22 20:09

jefflunt