4

I'm just starting to use branches with git, and I created yesterday a branch foo and pushed it to the remote. Today I wanted to work on it from home, so after running a pull I typed without thinking git checkout -b foo.

I know I shouldn't have added the -b option, because I got a message Switched to a new branch 'foo' and none of the code I wrote yesterday shows up in my folders so I figure I accidentally messed up the branch names.

I tried renaming the branch using git branch -m foo bar hoping that it would deal with the local branch only and deduplicate the branch names, but alas git checkout foo issued the message error: pathspec 'foo' did not match any file(s) known to git.

How can I retrieve the branch I created yesterday?

Community
  • 1
  • 1
Jonathan H
  • 7,591
  • 5
  • 47
  • 80
  • 1
    I am assuming all your changes after accidentally creation of foo branch is not pushed to remote. Try to reset hard your repo to what is there on the remote – null1941 Jan 16 '16 at 13:37
  • Yep I did that and it seems ok :) Is that the right way to go though, if this happens again in the future and I have uncommitted changes on other branches? – Jonathan H Jan 16 '16 at 13:48

1 Answers1

5

The problem you ran into was that you created a new branch that did not track the remote branch that you wanted.

I think that the most "right" way to correct this would be to set the branch to track the intended remote branch:

git checkout foo
git branch -u origin/foo

After doing that you can move your local branch to the current commit of the remote branch

git reset origin/foo

You can then add and commit any local changes. Then pull and push as usual.

(An alternative to reset would be to rebase on origin/foo instead, but I would not recommend that to a beginner)

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158