9

You type the command "git checkout foo".

If there is a branch called "foo" yet no file by that name, it switches to the branch -- and if there is a file by that name and no such branch, it updates the file "foo".

But I wonder --- is there a way to enter the command so that Git will unconditionally interpret "foo" as the name of a branch no matter what? This includes (but is not limited to) the specification that if there is no branch called "foo", the operation will fail even if a file by that name exists.

So - is there any way to do this?

Thanks.

Sophia_ES
  • 1,193
  • 3
  • 12
  • 22
  • I don't know of a way to check out the branch explicitly, but there's a way to make it check out the file explicitly: git checkout -- master – Acey Nov 11 '14 at 01:42

4 Answers4

15

Yes, from the reference, there is this usage:

git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>…

In your case, that boils down to:

git checkout foo --

Here, foo corresponds to <tree-ish> and -- says that anything after this is referring to file names. So, if you use the above command (with no file names after --) it will fail if there is no tree-ish (branch) named foo:

fatal: invalid reference: foo
Yawar
  • 11,272
  • 4
  • 48
  • 80
2

Just checked the behaviour of git 2.1.2 and checkout always interprets the name as a branch name. You can force it to be a file instead using ./foo.

In your case git checkout foo will always try to switch to branch foo.

If you want to force the argument to be a branch, so that even if a file is matching it's not updated, you can try to trick git using git checkout -B foo foo. If foo exists, git will check it out and update the branch pointer to itself. If it doesn't, the new branch will not be created, because the start point is missing.

viraptor
  • 33,322
  • 10
  • 107
  • 191
2

You can add an explicit check if the branch name exists first:

git show-ref --verify --quiet refs/heads/foo && git checkout foo

That will make sure the branch exists and if it does, check it out. Yes, there is a race condition (the branch could be deleted in between), but hopefully that's not a problem in practice.

Is there a better way to find out if a local git branch exists?

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thank you very much. :-) If I could check two answers as the best answer, I would have checked yours as well - as it is a very close second-best. And yes - the race condition is the tie-breaker --- something that isn't *likely* to be an issue at any given time, however (a) if I use it as a solution, the more time elapses, the more likely that the rare situation where this causes problems is to at some point arise (b) minor as this problem is, someone else provided an answer that does not have this problem at all. Therefore, your answer is a very *close* second, but still, only second place. – Sophia_ES Nov 11 '14 at 16:19
  • @Sophia_ES: I agree with you--Yawar's answer is better. :) – John Zwinck Nov 12 '14 at 00:31
0

Kindly try the below commands, it works for me.

$ git checkout <existing_branch>
$ git checkout -b <new_branch>
$ git checkout branch
ShahinSorkh
  • 599
  • 1
  • 9
  • 23
QA Tester
  • 23
  • 3