1

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.

1 Answer 1

3

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
1
  • Worked Perfectly! Thank you very much choroba.
    – camursm
    Commented Jan 15, 2016 at 19:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.