119

git checkout -b foo switches on foo branch (even if it doesn't exist, it is created), but if the foo branch already exists it throws an error like this:

fatal: A branch named 'foo' already exists.

What's the command that does the following check?

  • if the branch already exists, just switch on it (git checkout foo)
  • if the branch doesn't exist, create it and switch on it (git checkout -b foo)

6 Answers 6

131

Update Q3 2019 (Git 2.23): there now actually is a git switch command!

git switch -c aBranch 

Or, if the branch already exists:

git switch aBranch

As I explained in 2020, the git switch command is "experimental" in name only, and is here to stay.

You would need an alias though, which tries first to switch to the existing branch and, if it fails (because the branch might actually not exist), creates it:

switchoc = "!f() { git switch $1 2>/dev/null || git switch -c $1; }; f"

Note the name of the alias: switchoc (for "switch or create").

That combination git switch+git switch -c does precisely what the OP is asking for.

As jar pointed out in the comments:

Anyone trying this in 2021, note that you cannot shadow existing git commands with aliases.
Since git switch is a git command, this alias (named "switch") won't work. You must create your unique name for the alias, like "switchit" or something.


bgusach's alias mentioned below in the comment is safer (based on Jiří Pavelka 's answer):

switch = "!f() { git checkout $1 2>/dev/null || git checkout -b $1; }; f"

git switch abranch

Original answer (2014) You can try:

git checkout -B foo

From git checkout man page:

If -B is given, <new_branch> is created if it doesn’t exist; otherwise, it is reset. This is the transactional equivalent of

$ git branch -f <branch> [<start point>]
$ git checkout <branch>

As mentioned below, use it with caution as it does reset the branch, which is not always desirable.
If you did reset the branch by mistake with this command, you can easily revert to its previous state with:

git reset HEAD@{1}

9
  • 15
    Note that -B will reset the branch, see my answer for a (lengthier...) alternative.
    – ssmith
    Feb 20, 2016 at 17:48
  • 1
    Don't use this. It will more often than not destroy your branch and give you nice headaches bringing it back...
    – bgusach
    May 31, 2017 at 9:37
  • 6
    -1, because the answer is misleading. The behaviour that OP and most readers expect is "if the branch already exists, just switch on it (git checkout foo)". But the first line of code in this answer does a very different thing. Yes, it's explained below, but what people first read and try is this misleading code. Mar 3, 2019 at 6:41
  • 1
    This answer is not correct, git switch -c will not switch to an already existing branch. If it already exists, it will error out.
    – Jay
    Oct 28, 2022 at 17:00
  • 1
    Even git switch -C is incorrect, as that will blow away any changes and force that branch to point to HEAD instead of whatever it was pointed at before.
    – Jay
    Oct 28, 2022 at 17:01
102

Agreed with ssmith. Had the same problem and -B does not solve it, coz of reset. His solution works, however my solution looks simpler:

git checkout foo || git checkout -b foo

That works for me :)

EDIT

Without error output iff foo not exists

git checkout foo 2>/dev/null || git checkout -b foo
8
  • 4
    Ah yes, much simpler and shorter. =) There is however the rather minor caveat that if "foo" exists as something other than a branch (ex: tags) it'll check it out rather than create the branch, but it's a pretty edge case.
    – ssmith
    Mar 3, 2016 at 15:20
  • Another minor caveat, if the branch does not exist you get an error in the output. @ssmith solution is cleaner. +1 for simplicity anyway May 27, 2016 at 19:05
  • @GabrielePetronella simply hide error output, see EDIT May 28, 2016 at 9:59
  • 1
    Nice. I made an alias for this... just in case it helps somebody: switch = "!f() { git checkout $1 2>/dev/null || git checkout -b $1; }; f"
    – bgusach
    May 31, 2017 at 9:26
  • 3
    be careful: if foo matches a file in the repo, this will check out that file and revert changes to it. @ssmith 's answer is a little safer.
    – Josh
    Nov 16, 2018 at 20:23
30

Note the rather important fact that -B will reset an existing branch before checking it out, which I don't believe @Ionica wants according to his question.

I certainly didn't, so the best one-liner I could come up with is this:

git checkout $(git show-ref --verify --quiet refs/heads/<branch> || echo '-b') <branch>

This can be made into a handy alias like so:

[alias]
  # git cob <branch>
  cob = "!f() { git checkout $(git show-ref --verify --quiet refs/heads/\"$1\" || echo '-b') \"$1\"; }; f"
1
  • This should be the accepted answer.
    – ssppjj
    Feb 8, 2022 at 0:47
25

The command checkout -b creates a new branch and then checks out to that branch. So, if a branch already exists, it cannot create a new one.

Instead you need to do:

git checkout -B <branchname>

The above command does in a context sensitive way. If there's a branch, it switches, if not, it creates and checkout.

2
  • 1
    it cannot create a new one. -- I want to handle that. Nov 16, 2014 at 20:00
  • 9
    " it creates and checkout" …and reset the branch.
    – bfontaine
    Feb 16, 2017 at 16:19
4

Not very different from what George Pavelka suggested, but instead of relying on status of "git checkout ", this checks for the presence and then decides the command to use

git show-branch <branch> &>/dev/null && git checkout <branch> || git checkout -b <branch>
0

Try git switch foo (without -c).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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