0

Is there a way to alias git checkout to git checkout if the branch exists OR git checkout -b if it does not?

1
  • 1
    No. Or, not easily. You could certainly alias checkout to a complicated shell function, but then ever time you made a typo you'd get a new branch, leading to confusion, especially if you didn't notice immediately.
    – larsks
    Aug 2, 2013 at 0:39

2 Answers 2

4

Here's one way. Set this up in your user .gitconfig:

[alias]
    magic = !sh -c 'git show-ref --verify --quiet refs/heads/\"$1\" && git checkout \"$1\" || git checkout -b \"$1\"' -

Usage: git magic <branch name>.

It works like this:

  1. show-ref is used to check if the branch exists. The --verify flag is used to specify an exact path, so if you do git show-ref --verify refs/heads/<branch name>, you'll only match the branches that are local to your repo, not branches with the same name on any remotes:

    Enable stricter reference checking by requiring an exact ref path. Aside from returning an error code of 1, it will also print an error message if --quiet was not specified.

    --quiet makes sure you don't see any error messages that you don't need:

    Do not print any results to stdout. When combined with --verify this can be used to silently check if a reference exists.

  2. If show-ref exits with code 0, then the branch exists, and the command after && is executed, checking out the branch. Otherwise, show-ref exits with non-zero status 1, and the command after && isn't executed, but the one after || is, which creates a new branch with that name and checks it out.

I should warn, however, that the alias as written doesn't play well with msysgit bash tab completion of branch names, it causes the following error to happen, and I'm not sure why yet:

$ git magic fsh.exe": declare: `_git_'git': not a valid identifier

If you don't try to tab-complete the branch name, the alias will still work ok. I found this Stack Overflow question and answer that might help with the tab complete issue, but I'm not sure: Git aliases - command line autocompletion of branch names.

If you're interested in learning more about how to pass arguments to Git aliases, see Advanced aliases with arguments in the Linux Kernel Git Wiki.

3
  • I'm a little bit tired right now for debugging boolean expression, but in A && B || C, if A evaluates to 1 I believe B is executed, whereas if it evaluates to 0 is skips to evaluating C, since A && B is known to be false already. I think you have to swap your B and C statements. Aug 2, 2013 at 1:25
  • @GabrielePetronella in other programming languages, what you say would make sense, but that's not the way it works in Bash, it's the opposite. If you test out the commands in a Bash prompt, you'll see that it works as I've stated.
    – user456814
    Aug 2, 2013 at 1:29
  • 1
    @GabrielePetronella if you're interested, you can read about Lists of Commands in the GNU Bash Manual to learn about how && and || behave in command sequences with exit codes in Bash.
    – user456814
    Aug 2, 2013 at 2:05
2

No, not if you want to preserve the command as git checkout. Per the git-config man page:

To avoid confusion and troubles with script usage, aliases that hide existing Git commands are ignored.

Other solutions, such as Cupcake's, could work, but you'll have to accept a new name for the alias.

Your Answer

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

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