Switch on another branch (create if not exists), without checking if already exists?

git check if branch exists
a branch named already exists after delete
git switch
git checkout -b
git switch branch
branch already exists pick another name
git branch
git checkout remote branch

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)

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

git switch -c aBranch 

You would need a similar alias though:

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

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}

unable to checkout remote branch when existing local branch has , If branch already exists locally and you are not on this branch, then. git checkout branch. If branch does not exist but origin/branch does, then git  Ok, I got it. I converted the branch back to folder--the problem was I could see this option when I right click the branch "test"--but I just realized that under file->source control-->merge and branch, there is a "convert branch to folder" option. so that did the trick. thanks.

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

Git switch branch-How can I switch to another branch in git , How do I switch to a different branch in git? A potential solution for this would be to, in cases where an existing local branch exists and will clash, use the convention {remote}- {branch} when the user wants to checkout the same branch from another remote. shiftkey added bug and removed more-info-needed labels on Apr 30, 2018

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"

Git Checkout, git-checkout - Switch branches or restore working tree files If no pathspec was given, git checkout will also update HEAD to set the specified If <branch> is not found but there does exist a tracking branch in exactly one remote (call it git checkout refuses when the wanted ref is already checked out by another worktree. You can use the @{-N} syntax to refer to the N-th last branch/commit switched to using "git switch" or "git checkout" operation. You may also specify -which is synonymous to @{-1}. This is often used to switch quickly between two branches, or to undo a branch switch by mistake.

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.

Temporarily switch working copy to a specific Git commit, git switch [<options>] [--no-guess] <branch> git switch [<options>] --detach Specifying a <start-point> allows you to create a branch based on some other Similar to --create except that if <new-branch> already exists, it will be reset to git switch refuses when the wanted ref is already checked out by another worktree. Assuming nobody else has already made a branch named “hello_octo”, git returns “Switched to a new branch ‘hello_octo’.” (In the case where a branch by that name already exists, git would tell us instead “fatal: A branch named ‘hello_octo’ already exists.” No big deal, just do git checkout -b again with a new name variation).

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>

git-checkout Documentation, Solved: If creating a new branch that happens to have the same name as a branch that was same name as a branch that was deleted, the error "A branch with this name already exists. I am not going to sweat on it but I have made it answer :smile: Maybe there is another branch with the same name with different case? Lets assume and check that 'master.000' is only a tag, such local branch does not exist, grep returns one entry wchich is a tag. Still rev-parse will return 0 if reference exists, even if such local branch does not exist. This is a false match, exactly as mentioned by @paul-s

git-switch Documentation, SRCTREEWIN-6070 - Cannot switch branches after pull, push, commit or merge If no other git process is currently running, this probably means a git process Fails for more than half of the files I try to add to staged with by checking the box  If remote does not exist, it errors out. With git 2.16.2 (I am not sure which version is the first to have this functionality, for example, git 1.7.1 doesn't have it), you can do . git checkout If a remote branch exists, there will be some output, like. Your branch is up to date with 'origin/master' Otherwise there is no output.

Solved: A branch with this name already exists, git checkout -b foo switches on foo branch (even if doesn't exist, created), if foo branch exists throws error this: fatal: branch named 'foo' exists. "Release branch already exists. Cannot start release." In fact, plugin refuses to create branch "release/1.0.1" because there is branch "release/1.0.0" already existing. What is the correct workflow in case there are many installations of the product with different release versions so all release branches should be maintained?

[#SRCTREEWIN-5639], Switch on another branch (create if not exists), w What's the command that does the following check? if the branch already exists, just switch on it ( git checkout foo ); if the branch Without error output iff foo not exists I guess this happens only if there is no branch on the ticket, thats why I couldn't reproduce it vbraun closed this in 491b640 Jan 20, 2016 Sign up for free to join this conversation on GitHub .

Comments
  • Thanks, git guru! :-) Accepting in 4 minutes.
  • Note that -B will reset the branch, see my answer for a (lengthier...) alternative.
  • @ssmith I realize that (and have upvoted your answer), but I would still prefer this (simpler) approach.
  • Don't use this. It will more often than not destroy your branch and give you nice headaches bringing it back...
  • -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.
  • 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.
  • Another minor caveat, if the branch does not exist you get an error in the output. @ssmith solution is cleaner. +1 for simplicity anyway
  • @GabrielePetronella simply hide error output, see EDIT
  • @JiříPavelka thanks, however in my use case I want the output, but just the one of the successful comment.
  • @GabrielePetronella that should be it. You can get success from first command or success/error from the second command.
  • it cannot create a new one. -- I want to handle that.
  • @IonicăBizău Added that.
  • " it creates and checkout" …and reset the branch.