Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo last commit/merge

I have messed up my git repo a little. I worked on a feature in a separate branch. After finishing the work, I switched to the master to merge it into, but my partner pushed a few files which came into conflict with mine. After the merge, the conflict and pushing the new changes, I saw that I committed the older changes of my partner too.

Now I want to redo this commit/merge. I tried git reset --soft HEAD^ but when I wanted to push, I got this error message Merge the remote changes before pushing again.

Can anyone help me?

like image 577
Siggy Petersen Avatar asked Apr 11 '11 14:04

Siggy Petersen


People also ask

Can I 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 do you undo a git merge that has been pushed?

Now, if you have already pushed the merged changes you want to undo to your remote repository, you can right-click on the merge commit and select Revert commit from the context menu.

How do I cancel a 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.

What is reverse merge in git?

To revert a merge commit, you need to use: git revert -m <parent number> . So for example, to revert the recent most merge commit using the parent with number 1 you would use: git revert -m 1 HEAD. To revert a merge commit before the last commit, you would do: git revert -m 1 HEAD^


2 Answers

Your feature branch will still point to your work. You will not lose those changes.

As plaes said, you can reset master back one with

git reset --hard HEAD^ 

If you want to grab some specific files from your branch without merging, you can check them out:

git checkout yourbranch -- file1 file2 etc 

If you want some files from master from before the merge you can also check these out:

git checkout master^ -- file3 file4 etc 

This is not ideal but it is what is needed sometimes. A merge /may/ mean that you reject some changes from either side in a merge. The best way to attain a proper merge is to:

git merge --no-commit yourbranch 

from master, then run the git checkout commands from above and finally commit:

git add . -A git commit 

When you push this branch now, you will need to add the force option

git push --force 

or

git push -f 

Hope this helps.

like image 191
Adam Dymitruk Avatar answered Sep 18 '22 13:09

Adam Dymitruk


Hehe, you almost figured it out:

git reset --hard HEAD^ 
like image 26
plaes Avatar answered Sep 20 '22 13:09

plaes