I often accidentally checkout remote tracking branches incorrectly:
git checkout -b origin/fixbugs
The -b should be a -t.
This mistake creates a branch called "origin/fixbugs". How could I get git to give me an error instead of creating this branch when the branch name begins with "origin/" (or any other remote name)?
CodePudding user response:
Since v2.28 there's a reference-transaction hook you can use to vet all such updates.
#!/bin/bash
case $1 in
prepared)
while IFS='/ ' read old new refs type name rest; do
if [[ $type != remotes && $new = *[^0]* ]] && git config remote.$name.url >&-
then echo $name${rest: /$rest} would pun remote name $name
exit 1
fi
done
;;
esac
will do it unless you've taken to using multilevel remote names, you could beef up the check loop if you wanted.
CodePudding user response:
First, you don't have to use the -t option, since git checkout has a guess mode.
git checkout fixbugs
If
<branch>is not found but there does exist a tracking branch in exactly one remote (call it<remote>) with a matching name, and--no-guessis not specified, treat as equivalent to:$ git checkout -b <branch> --track <remote>/<branch>
Second, use the new git switch command (with Git 2.23 , Q3 2019):
- it has the same guess mode as
git checkout, so a simplegit switch fixbugsis enough. - if has a
-toption, but no-boption, so you would get an error in this case!
