2

Solution already found in this thread: The branch name is the same as a folder name in the repository.

I check out a git repo with different branches

$ git clone <repo> .
$ git branch -a
* b1
  remotes/origin/HEAD -> origin/b1
  remotes/origin/b1
  remotes/origin/b2
  remotes/origin/b3

There's no master branch, the default is b1

$ cat .git/config
...
[branch "b1"]
  remote = origin
  merge = refs/heads/b1

I am using git version 1.9.1. According to this thread I only need to git checkout b2 to checkout and track the remote branch origin/b2 with a local branch b2

$ git checkout b2
  Branch b2 set up to track remote branch b2 from origin.
  Switched to a new branch 'b2'

The branch is added to .git/config

But if I try to checkout another branch...

$ git checkout b3

...nothing happens. No error message, no checkout to the branch b3.

I can do

$ git checkout -b b3 origin/b3
Branch b3 set up to track remote branch b3 from origin.
Switched to a new branch 'b3'

What could be the difference between the two branches so that the checkout behaves differently?

Could it be with which git version a branch was created/committed/pushed?

UPDATE:

The branch names used in this post alias the real branch names (which I'd like to avoid showing here).

A checkout of a non-existing branch shows:

$ git checkout invalid_branch
error: pathspec 'invalid_branch' did not match any file(s) known to git.
radix
  • 352
  • 1
  • 2
  • 14
  • 2
    Not seeing a `remotes/origin/b3` branch in your list. Just a typo? – Phil Aug 02 '17 at 11:07
  • 2
    Also, seems odd not to have a remote `HEAD`. I would expect to see `remotes/origin/HEAD -> origin/b1` if `b1` is the default – Phil Aug 02 '17 at 11:10
  • **git checkout -t -b master origin/master** for checkout branch which not present in local. – Mahesh Gareja Aug 02 '17 at 11:24
  • @Phil thanks for pointing out, fixed both – radix Aug 02 '17 at 11:28
  • @MaheshGareja I think the point is that OP **should** be able to use `git checkout b3` which is equivalent to `git checkout -b b3 --track origin/b3` – Phil Aug 02 '17 at 12:02
  • @radix are there any other remotes? Check with `git remote -v` – Phil Aug 02 '17 at 12:03
  • @Phil No, there are no other remotes. – radix Aug 02 '17 at 12:12
  • That's very odd then. You should at least expect to see some messaging or errors. Try starting from scratch (and not cloning into the current directory) – Phil Aug 02 '17 at 12:14
  • @Phil that's what I thought, too: **If git can't accomplish the command, it should report some error**. Cloning from scratch with `mkdir new_dir; git clone new_dir` didn't make any difference – radix Aug 02 '17 at 12:34
  • And your output from `git branch -a` above is complete? No editing? – Phil Aug 02 '17 at 12:35
  • I've added a comment resp. branch names to the post. I replaced the original branch names (containing company names) with b1, b2, b3 (which is why I had errors in the `git branch -a` output in the first place). – radix Aug 02 '17 at 13:04
  • 1
    Weird, I think I just replicated this by following the answer at https://stackoverflow.com/questions/34287722/git-checkout-master-does-not-switch-branch-repository-broken. Is that the case here, do you have a folder with the same name present? Seems like git has to be tracking files within that folder to see the weird behaviour. – Chris Aug 02 '17 at 13:08
  • **Yes. Solved.**! There's a folder with the same name as the branch which git doesn't switch to. Makes my post a duplicate, I'll add it to the post. Only the heading of the other post is quite specific, I even didn't find the thread. – radix Aug 02 '17 at 13:18
  • Super weird bug, I did a naive google search for "git checkout does nothing", seemed like an interesting problem =D – Chris Aug 02 '17 at 13:27

0 Answers0