简体   繁体   中英

git how to undo Merge branch 'master' of repo into develop branch

I was working with the develop branch and made the commit.

And then, I was supposed to do a git pull origin develop - instead I accidentally did this git pull origin master

Then, I saw this message Merge branch 'master' of repo into develop branch

Is there anyway I can undo this ?

Reset the merge commit:

git reset --hard HEAD~

and pull again

git pull origin develop

PS: I assume that your develop branch actual points to the merge commit.

EDIT

if I do this, would I loose the commit made in develop branch ? No, right ?

The commit will not be referenced anymore, but it is still there. Git will Keep unreachable commits for a while. The default is 90 days. See git gc

The optional configuration variable gc.reflogExpire can be set to indicate how long historical entries within each branch's reflog should remain available in this repository. The setting is expressed as a length of time, for example 90 days or 3 months. It defaults to 90 days .

So within this period you can restore the commit, but you must know it's id. You find the id in the reflog if it is not expired yet or just save it somewhere.

If you don't feel fine with this you can just create a local branch that points to the actual commit of your develop branch before doing a git reset --hard . In this case the local backup branch will reference the commit and git gc will not delete it (because it is referenced). Eg

git branch backup/develop
git reset --hard HEAD~

If you think now: "Oh no I want to go back". Just do

git reset --hard backup/develop

otherwise delete the local backup branch

git branch -D backup/develop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM