Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to reset after merging?

Tags:

git

git-revert

I've merged a master branch from a friend's repository into my working directory into branch_a using:

git pull my_friend master

I've discovered that the merged version has errors. To continue development I would like to revert to my last commit before the merge.
I tried:

git reset --hard HEAD

But that brought me back to the state right after the merge. (does pull command commit?!)
I also tried:

git revert HEAD

but received the following error:

fatal: Commit 2d72d8f367b987d8c16f5cb1a543a6886acdcf83 is a merge but no -m option was given.

What should I do?

like image 837
Jonathan Livni Avatar asked Aug 24 '10 12:08

Jonathan Livni


People also ask

How do I reset a commit before a merge in Git?

$ git reset --hard <commit-before-merge> If you don't have the hash of the commit before the merge at hand, you can also use the following variation of the command: $ git reset --hard HEAD~1 This way, using "HEAD~1", you're telling Git to go back to the commit before the current HEAD revision — which should be the commit before the merge!

How to undo a merge in Git?

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 to Undo a Git Merge

What is Git merging and how does it work?

Merging combines two branches into one. You can merge a branch into another branch whenever you are ready. This means that you can develop a feature or a bug fix on a separate branch. Then, you can make it part of your main project later. To undo a git merge, you need to find the commit ID of your last commit.

What is the difference between GIT revert and Git reset?

Another difference between these two commands is that git revert is configured to undo public commits, and git reset is configured to undo local changes to the working directory and staging index. Don’t use git reset <commit>, when there are snapshots after <commit>, which are moved to a public repository.


1 Answers

HEAD refers to the current commit (generally the tip of the currently checked-out branch). You've already committed your merge, so HEAD is pointing to the merge commit. If you want to get back to the commit before it, use:

git reset --hard HEAD^

The ^ means "first parent of"; for a regular commit it's the only parent, and for a merge commit it's the commit you had checked out when you merged (i.e. the prior tip of the branch you merged into).

And of course, if you ever get really lost, just open up gitk, and either copy/paste the SHA1 of the commit you want to reset to (git reset --hard SHA1) or just right click on it and reset within gitk.

By the way, revert doesn't mean what you think it does (it sounds like you're using it in an svn way, maybe? but I've never used svn). git revert is used to create a commit which cancels out (reverts) a previous commit, by applying the reverse diff. You use it when you want to undo a single previous commit which has already been published.

like image 125
Cascabel Avatar answered Sep 20 '22 07:09

Cascabel