2

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?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
John Reed Avery
  • 121
  • 1
  • 7

3 Answers3

2

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Hey, Mureinik. Thanks for the reply. Let me rephrase a little bit, for more clarity: Are there any restrictions, on what other Branches that this newly-created-Branch can merge into, by specifying a particular "start-point" (regardless of what technique is used to specify it) ??? In other words: If I specify the "fishlegs" Branch as the "start-point", is there a possibility that I might not be able to merge the new Branch into the existing "chickenlips" Branch --which itself was earlier created without any reference to "fishlegs" ??? Thanks again. – John Reed Avery Jan 22 '22 at 19:25
  • 2
    You probably want to see the problem the other way around: a branch always have a starting point (unless you're about to compose the initial commit or you have used --orphan), but this starting point is implicit when you don't specify it and corresponds to your current position. It's useful to avoid checking out the revision in your work dir only for this purpose, especially on large projets. It's appreciable when scripting too, as well as during rebase operations. – Obsidian Jan 22 '22 at 19:47
1

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.

The <start-point> is actually a branch name, or a tree-ish:

tree-ish:

A tree object or an object that can be recursively dereferenced to a tree object.

The following are all tree-ishes:

  • a commit-ish,
  • a tree object,
  • a tag object that points to a tree object,
  • a tag object that points to a tag object that points to a tree object, etc.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Typically a human user checks out the branch that becomes the root of the new branch, and then a simple git branch new_branch is all that is needed. Not every starting point is a branch. I pass the extra argument most often when creating a branch from a tag, or a different commit than HEAD.

You can check out a tag. Git just informs you that you are in a detached state — HEAD does not point to the tip of a branch. You can certainly git branch new_branch from a detached HEAD, but this is not a natural workflow for most people. People tend to check out branches more often.

Another use case for the extra argument is one I use often when writing experimental code. I will create a branch to experiment with something. Several commits in I realize I've created complete and utter garbage. I might decide to create a new branch based on a previous commit in my experimental branch.

The last use case I can think of is an automated process or shell script. You might need to create a new branch in a script from an arbitrary commit as part of a continuous integration build.

Git needs to know the base of every new branch, and that is the purpose of the extra argument. I would say that not specifying the extra argument and assuming the new branch is created from HEAD is a convenience feature. The extra argument isn't really extra. It is required, but git intelligently assumes "the tip of the current branch" if omitted.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92