Undo last commit/merge
Asked Answered
W

5

43

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?

Wellturned answered 11/4, 2011 at 14:53 Comment(1)
#1378345Calicle
R
59

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.

Ruthenian answered 11/4, 2011 at 18:43 Comment(2)
Thanks for reply adymitruk, i tried the git reset --hard HEAD^ and after that git push origin +master but i get still an error with the message ! [remote rejected] master -> master (pre-receive hook declined)Wellturned
if you are going to lose a commit on the remote because of a push, you need to tell git that it's ok with the --force or -f option. Updating answer.Ruthenian
C
21

Hehe, you almost figured it out:

git reset --hard HEAD^
Calicle answered 11/4, 2011 at 15:4 Comment(2)
Hey plaes thanks for the answer, but i want to keep the files i have pushed wrongly. Is there a solution for it?Wellturned
git reset --soft may help you.Kayleigh
B
10

The problem here is that you want to undo changes that you already have pushed to the central repository. If you hadn't pushed them in the first place, a git reset --hard/--soft/--mixed HEAD^ would have done the trick.

So when you have reseted your HEAD git complains when you try to push to the origin and update the remote ref which is not an ancestor to your HEAD. Use --force:

git push --force origin master
Bosomed answered 1/5, 2011 at 13:25 Comment(0)
F
1

Use git reset --hard origin/master instead, this will do hard reset as well as delete the merge as well. Take from link Undo a Git merge that hasn't been pushed yet

Frontward answered 17/8, 2021 at 9:29 Comment(0)
W
0

Let me simplify it for the users that don't understand git very well, like me :D

If you want to unmerge the commit and remove it from the remote git as well. Follow:

git reset --hard HEAD^

or

git reset --merge hash-of-commit-before-merge i.e. c4ab5de

Now you have successfully reset on your local, it's time to update/sync this on remote

git push --force // this will remove the merge or afterwards commits
Walls answered 1/8, 2023 at 5:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.