Change from master to a new default branch git

As far as I know, git has no concept of a "default branch". User interfaces such as GitHub do have a default branch in the sense that when you open the web page, you see a certain branch (generally master) by default. But just speaking about git, the master branch is not special, it's just the name given to the first branch.

To create a new branch, use the -b flag with checkout, as in:

git checkout -b develop

The git branch command will list all of the existing branches, with a * next to the current branch. Any commits you make will be added to the current branch.

In your question, you mention pulling from a remote. The relevant concept is "Tracking Branches"; see the section with that name at https://git-scm.com/book/id/v2/Git-Branching-Remote-Branches.

In short, when you do

git pull origin master

it will pull changes from the master branch of the origin repository into whichever branch is currently checked out. If you'd like, you can set up remote tracking so that git already knows where you want to pull from based on which branch you have checked out.

e.g. if you have a remote develop-remote branch and a local develop-local branch, you can set your local branch to track the remote branch via:

git checkout develop-local
git branch --set-upstream-to origin/develop-remote

Also, they might both be simply called develop, I just distinguished them here for clarity.

Finally, I should mention that when you do git pull <url-of-repo>, remote tracking is automatically established.

P.S. for more information about remotes (e.g. what does origin mean?), see https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes


With git 2.28 you can set a global config with this command git config --global init.defaultBranch {branchName}

Replace {branchName} with the default branch name and now whenever you create a new git repo, the default branch will be this.

More details in my video here: https://www.youtube.com/watch?v=YccHk6QlRss


When cloning a repo from GitHub, the default branch gets stored in the HEAD file:

$ cat .git/refs/remotes/origin/HEAD
ref: refs/remotes/origin/master

If the default branch is changed on GitHub after the repo has been cloned, this is not updated automatically, but can easily be fixed locally:

git remote set-head origin -a

-a will set refs/remotes/<name>/HEAD according to remote

Or explicitly to a named branch:

git remote set-head origin develop

Now, HEAD points to the new location:

$ cat .git/refs/remotes/origin/HEAD
ref: refs/remotes/origin/develop

Tags:

Git

Github