简体   繁体   中英

How can I force git to always branch from master?

I keep accidentally branching from the feature branch that I was last working on, rather than from master. This causes confusion in the history.

I would like to either:

  • Block branching from all branches except master
  • Make master the default branch to create new branches from, ie setting as a default here:

     git branch [<oldbranch>] <newbranch>

Any help would be much appreciated!

I would configure a Git alias to accomplish this.

git config alias.braster "checkout master -b"

To use it type (braster as in branch master):

git braster feature1

Overwriting git's default operation can lead to some unexpected results when using external git tools.

I would recommend creating an alias for this purpose.

add the following line to your .bashrc file:

alias gb="git checkout master; git checkout -b"

or run the following command:

echo alias gb="git checkout master; git checkout -b" >> ~/.bashrc

Branches don't have source branches. They are just labels, pointing to commits. (See also What exactly do we mean by "branch"? ) It's the commits that really matter: A Git repository is all about commits. What branch names do for us is that they let us find commits, whose real names—their hash IDs—appear to be random, and which no human can ever remember.

More specifically, a branch name is simply a human-readable place in which your Git stores the raw hash ID of one commit, with another special feature: if you run git checkout <name> , Git not only checks out that one particular commit, it also remembers that name as being your current branch . 1 Whatever branch name is your current branch, when you make a new commit, the raw hash ID of the new commit you just made gets stuffed into that branch's name.

The result is that if you make new branch name branch9 like this:

git branch branch9 a123456

then the name branch9 now points to the existing commit whose hash ID is a123456 . The git branch command will take anything that can be turned into a raw commit hash ID. Using:

git branch branch9 master

turns master into a hash ID (by reading out the hash ID stored in the name master ) and uses that to set up the new name branch9 to hold the same hash ID.

Aside from cloning the source to Git and changing it and building your own version, there's no way to change the syntax or options that git branch will accept here. The same goes for git checkout -b , git checkout -B , and git reset , all of which can create new branches or force a raw hash ID into a branch name.

Your best bet is probably to set up your own alias (shell / CLI alias, or Git alias), as in Rickard Körkkö's answer and gkpin3's answer .


1 There are additional special features for branch names, none of which apply to remote-tracking names like origin/master , but this particular one—the one that Git effectively calls an attached HEAD , as compared to what Git calls a detached HEAD anyway—is the most important by far.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM