Ad

Prevent Git From Creating Branches That Use Names Of Remotes?

- 1 answer

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)?

Ad

Answer

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-guess is 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 simple git switch fixbugs is enough.
  • if has a -t option, but no -b option, so you would get an error in this case!
Ad
source: stackoverflow.com
Ad