How to alias git checkout -b with a string format for the branch name

397 views Asked by At

My team uses a format for our branch names for uniformity. For a given branch, the format would be: "feature/TeamName-CardNumber". The card number is the only part of this that will change, so I would like to make an alias such as:

git cobf '111'

Where 111 is the card number.

I tried this command to set the alias:

git config --global alias.cobf 'checkout -b feature/TeamName-"

But when running the git cobf 111 command, I get an error saying that 111 is not a commit and a branch cannot be created from it.

1

There are 1 answers

0
Romain Valeri On BEST ANSWER

Why does it fail?

It's because after expansion of the alias, your command is resolved as

git checkout -b feature/TeamName- 111

...which to git means "checkout a new branch named feature/TeamName- pointing at committish 111" (roughly put, "committish" is something that is either a commit or something pointing at one, like a branch or tag)


What to do instead?

To avoid that, use an ad-hoc bash function to handle parameters more conveniently :

git config --global alias.cobf '!f() { git checkout -b feature/TeamName-$1; }; f'

Note : the quotes are unneeded when you'll call the alias, git cobf 111 will do.