4

Is this a native git concept?

I've been researching this for a while and cannot seem to understand? All I get is Github documentation, and just that master is the initial default branch.

If possible, can someone please explain what this concept means native git-wise.

EDIT: For instance, what setting or idea makes one particular branch the default one ( not necessarily the initial one, but on an ongoing basis ) , and how would one change it outside of something like Github

My intuition is telling me it's where HEAD is pointing to ( in a repository you clone and merge back into, like a bare repository on a server ) in a more general sense, is this close?

Ty!

rubixibuc
  • 7,111
  • 18
  • 59
  • 98

4 Answers4

6

Is this a native git concept?

No. It's a GitHub (and other host) concept. It goes with pull requests, which are also not a Git concept.

When you make a pull request at GitHub, you push a branch; what branch should GitHub offer, by default, to merge your pull request branch into? That is the default branch.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • What about when cloning, there must be some sort of overlap here. When I clone it initializes my local repo to the default branch. Is that because GitHub has HEAD pointing to that branch? – rubixibuc Mar 19 '22 at 04:03
  • 1
    So this is part of what I was asking https://stackoverflow.com/questions/18726037/what-determines-default-branch-after-git-clone. Can one say the that default branch is an overlap of Github concepts and native git concepts? If I set the default branch in Github, it sets HEAD equal to that default branch right? – rubixibuc Mar 19 '22 at 04:16
  • Certainly there is a Git notion of the remote HEAD but it's not particularly interesting or important; it's pretty much pointless. There is also a notion of what "initial branch" Git will create and check out at the end of a clone, and yes that notion comes from the remote HEAD, but again, that's not very important, as you can change it as part of the clone command or right after cloning; personally I often prefer _no_ local branch to be created and checked out as part of a clone. – matt Mar 19 '22 at 11:59
  • When I clone my GitHub repo it will checkout the default or HEAD ref in GitHub. My underlying task is to not specify and have Jenkins do the same thing. I set out to understand what makes something the default branch to understand this. Should this be a separate question now? – rubixibuc Mar 19 '22 at 13:41
  • 1
    @rubixibuc: Git itself, during `git clone`, reads the other side's `HEAD` to figure out which branch to create *in* the clone, if you didn't use `-b`. If you *did* use `-b`, that overrides, of course. So yes, in a sense, the `HEAD` of the repository you're cloning is the default-suggested-branch-name. There's some weird historical oddities here for very old Git servers too, but you should never encounter that. – torek Mar 19 '22 at 14:18
  • It seems this was an x-y question? It's a good idea in general to ask the question you want to know the answer to, and not some other question. – matt Mar 19 '22 at 14:47
2

Is this a native git concept?

The “initial default branch name” is a native concept since Git 2.28, yes.[1]

Use init.defaultBranch to set the branch name that will be used when running git init.

The old built-in default was master. If you still want to use that then you might want to set it to that explicity. Or else you will get this yellow (in the terminal) “hint” wall of text on every git init:[2]

$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/kristoffer/programming/temp/.git

Use git var GIT_DEFAULT_BRANCH to get the currently configured default (master[2] if unset).

Notes

  1. Which was released long before this question was asked
  2. Tested with git version 2.40.0
Guildenstern
  • 2,179
  • 1
  • 17
  • 39
1

Is this a native git concept?

Effectively, yes -- but the native relevance is limited to git clone.

Git's algorithm behind the 'Default branch' is explained in detail here:

And the implementation of switching the 'Default branch' in GitHub likely follows a pattern similar to here:

As you surmised, this is ultimately comes down to HEAD:

In short: The 'Default branch' is the HEAD of the repo being cloned.

Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
  • Just to keep things confusing, [`git remote set-head`](https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emset-headem) (optionally) "Sets or delete the default branch" which "allows the name of the remote to be specified in lieu of a specific branch.". – Brent Bradburn Mar 27 '23 at 14:39
0

Branches are not a GitHub concept but a source code repository concept.

Default branch in Git (basics)

The master (or nowadays optionally main) branch is always the default branch in a repository. You can work on a copy of a branch to build independent features without breaking or affecting the production code. You can also clone a specific branch from a repository.

I found this blog post surely interesting for beginners.

Default Branch in Jenkins

In Jenkins you have to set a default branch, see here:

Branch to be checked out in the workspace. Default is ' master '. Note that this must be a local branch name like 'master' or 'develop'. Remote branch names like 'origin/master' and 'origin/develop' are not supported as the branch argument.

You can set up builds for one or multiple specific branches, see this SO post.

Christian
  • 4,902
  • 4
  • 24
  • 42
  • 1
    I didn't downvote, but maybe it could be because master doesn't mean production. Actually I prefer otherwise: master is your dev branch, and production is called unmistakably prod/production, same with staging, UAT, etc. Where you start working from scratch can't be prod/live, it has to be dev. Then you create other branches as your project progresses. – Andrew Mar 19 '22 at 03:48
  • OK, fair enough. Tried to include as much helpful information as possible, not overloading/confusing the OP/other readers. In our company, we use master/main as a production branch. We don't have dev branches (at least not in my project) but only feature branches. – Christian Mar 19 '22 at 03:51
  • 1
    Sorry, it will help explain what it is by how you set it? How is the default branch set, how does it exist is more my question. I understand what it is conceptually, but I don't understand what it is in native git terms. Also, I didn't downvote you either. Literally what makes something the default branch in actual git terms and how do you change it? – rubixibuc Mar 19 '22 at 03:53
  • Do you want to know something like this? https://stackoverflow.com/questions/51274430/change-from-master-to-a-new-default-branch-git – Christian Mar 19 '22 at 04:01
  • @Christian not exactly, but this is helpful. This just configures the initial default branch. I'm trying to understand what actually makes a branch the default. See my edits up top. There is some state saying this is the default branch, git-wise and implementation wise, what is this? – rubixibuc Mar 19 '22 at 04:04
  • Ok, you know a lot more than I initially thought. Your question seems to be pretty specific. A branch is just a concept. See https://www.perforce.com/blog/vcs/branching-definition-what-branch#:~:text=A%20branch%20is%20a%20copy,baseline%2C%20master%2C%20or%20mainline. Regarding the HEAD: When working with Git, only one branch can be checked out at a time - and this is what's called the "HEAD" branch. Often, this is also referred to as the "active" or "current" branch. The head doesn't need to be main though. Do you have a specific scenario/problem in mind? Can add/share an example? – Christian Mar 19 '22 at 04:19
  • Ok, so I'm asking this to figure out a different personal question, while still wanting to understand what exactly makes up the default branch. My underlying question has to do with Jenkins. Jenkins asks for a ref to specify which branch to build from. I want to build from the default branch from my Github repo (still don't know what to put in that textbox). My initial goal was to accomplish that, but now I'm down a rabbit hole trying to figure out what even is the default branch in terms of git. I'm looking for something way deeper than how to set it in Github if that makes sense – rubixibuc Mar 19 '22 at 04:26
  • OK cool, I updated my post with two more links which should bring some clarity. Maybe you should add jenkins to the question title. – Christian Mar 19 '22 at 11:08