简体   繁体   中英

Git: Accidentally used git branch -f instead of git checkout -f - how can I restore the branch?

While on a branch ( test ) I accidentally hit git branch master -f in an attempt to switch to the master branch... seconds later I saw my error and checked out master branch with git checkout master -f , and saw that the previous command seemed to have completely replaced the master branch with the test branch.

How can I reset master to it's previous state?

Thank you for your help.

Try running git reflog . It should still show the lost commits on the master branch. Take the most recent one (denoted XXXXXXX below) and make it the master's head:

$ git reflog
a092834 HEAD@{0}: commit: changes on test
b6affd1 HEAD@{1}: checkout: moving from master to test
XXXXXXX HEAD@{2}: commit: last commit to master

You can switch to a lost commit by checkout , the same way as normally. Then recreate the master there the same way you lost it:

git checkout XXXXXXX
git branch master -f  # Your command again!
git checkout master

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