I've made some changes to my git repo that I wish to undo.
My git repo looked like this:
A-B---- master \ / C-D * develop
I was on the develop
branch, forgot that it differed from the master
branch, made a change on develop
, merged it into master
, and then pushed to my remote (called publish
).
Because there were no changes on master since B (common ancestor), git did a fast-forward merge.
Now, my repo looks like this:
A-B-C-D master, develop, remotes/publish/master, remotes/publish/develop.
I wanted to revert the last merge, restoring master
to B.
From what I read in How to undo last commit(s) in Git?, I used git reset sha-of-B
to restore my master
branch to revision B.
Questions:
develop
to revision D?Disables the default fast forwarding on merge commits. Use git config --add merge. ff false to disable fast-forward merging for all branches, even if it is possible.
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.
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 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.
If you reset the branch master, it won't touch develop branch. In order to reput all in order, you should do:
git checkout master git reset --hard sha-of-B git checkout develop git reset --hard sha-of-D git checkout master git merge develop --no-ff
git reset --hard @{1}
git push publish develop
git push publish master --force-with-lease
--force-with-lease
in a safe way--force
might silently overwrite work from others, so I never use thatgit push publish develop --force-with-lease=master
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With