Home > OS >  In Git, what is the value of including <start-point>, when creating a new branch?
In Git, what is the value of including <start-point>, when creating a new branch?

Time:01-23

The documentation says that, at the simplest level, I can run either of the following 2 commands to create a new branch (deliberately ignoring the "checkout" command, for this question):

$ git  branch  <newbranchname>

--OR--

$ git  branch  <newbranchname>  <start-point-branch>

I have a gut feeling for the difference(s) but I am so new to this stuff that I am not trusting my gut.

Can you all please explain to me the advantages & disadvantages of either including or not including the "start-point-branch" at the end of that command line?

CodePudding user response:

Without the extra argument, the newly created branch will branch out from the current HEAD. With the extra argument, it will branch out from there.

I don't think you can really discuss the advantages or disadvantages of including this argument - you should branch out from the correct commit. Once branched out, git retains no "memory" of how this branch was created. In other words, git branch new_branch some_starting_point will have the same end result as git checkout some_starting_point && git branch new_branch.

CodePudding user response:

If you want to then use the branch created from current HEAD or for another commit, do not use the confusing and obsolete checkout command.

Use git switch:

git switch [<options>] (-c|-C) <new-branch> [<start-point>]

Create a new branch named <new-branch> starting at <start-point> before switching to the branch. This is a convenient shortcut for:

$ git branch <new-branch>
$ git switch <new-branch>

If you omit the start point, you remain at current HEAD, but in a new branch, and can start working right away.
But you can create (and switch to) a new branch from an existing one by adding as a starting point the branch name from which you want to work.

  •  Tags:  
  • Related