4

I've cloned a large, well-maintained repository. By accident I used the -b flag when checking out a different branch. Git says "Switched to a new branch". Was the pre-existing branch overwritten, is git's output misleading, or did I make another mistake?

cmbuckley
  • 33,879
  • 7
  • 69
  • 86
mda
  • 1,048
  • 13
  • 18

1 Answers1

10

Was the preexisting branch overwrote,

No.
The default branch has been checked out, and a new local branch has been created from its HEAD.

You can see all the branches with:

git branch -a

Simply switch back to the upstream branch of your choice with

git checkout -b aBranch --track origin/aBranch
# if the local branch was already there
git checkout -B aBranch --track origin/aBranch

Note that if you have work in progress, you need first to go a git stash, as explained in "To git checkout without overwriting data".

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Thanks, you're getting me closer. I get this output (should I just choose a different branch name?): git checkout -b client_release_6.12 --track remotes/origin/client_release_6.12 fatal: A branch named 'client_release_6.12' already exists. – mda Jan 21 '13 at 18:41
  • 2
    @mda: if you already have a *local* branch `client_release_6.12`, then the error message is normal. The question is: does this branch start from `origin/client_release_6.12`. You can reset it by typing `git checkout -B client_release_6.12 --track origin/client_release_6.12` (note the `-B` instead of `-b`) – VonC Jan 21 '13 at 19:31
  • I cloned the remote repository as usual git clone URL, then possibly created a new local branch of the same name as a remote branch...not sure from git's output. – mda Jan 21 '13 at 20:06
  • @mda ok, but if the remote repo has a branch named `client_release_6.12`, then it is best forcing the start of the local `client_release_6.12` branch to `origin/client_release_6.12` (the branch from the upstream repo). – VonC Jan 21 '13 at 20:45
  • OK, that worked, but I had to do "git stash" first. So the commands are: git stash; git checkout -B client_release_6.12 remote/origin/client_release_6.12 – mda Jan 21 '13 at 21:48
  • If my comment above sounds right, please edit your answer and I'll give it the check-mark :-) – mda Jan 21 '13 at 21:49
  • @mda your comment sounds right, and I have added a link to another SO question, which illustrates it. – VonC Jan 21 '13 at 22:17