How to change default branch in git
How to change default branch in git
Changing the default branch
In this article
If you have more than one branch in your repository, you can configure any branch as the default branch.
People with admin permissions to a repository can change the default branch for the repository.
About changing the default branch
You can choose the default branch for a repository. The default branch is the base branch for pull requests and code commits. For more information about the default branch, see «About branches.»
Note: If you use the Git-Subversion bridge, changing the default branch will affect your trunk branch contents and the HEAD you see when you list references for the remote repository. For more information, see «Support for Subversion clients» and git-ls-remote in the Git documentation.
You can also rename the default branch. For more information, see «Renaming a branch.»
To change the default branch, your repository must have more than one branch. For more information, see «Creating and deleting branches within your repository.»
Changing the default branch
On GitHub.com, navigate to the main page of the repository.
Under your repository name, click
Settings.
In the «Code and automation» section of the sidebar, click
Under «Default branch», to the right of the default branch name, click
.
Use the drop-down, then click a branch name.
Click Update.
Read the warning, then click I understand, update the default branch.
Help us make these docs great!
All GitHub docs are open source. See something that’s wrong or unclear? Submit a pull request.
Change the default branch
Azure DevOps Services | Azure DevOps Server 2022 | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018
The default branch is the first branch that Git will check out on a fresh clone. Also, pull requests target this branch by default.
We’ll walk through the process of changing the default branch. We’ll also cover other things you must consider and update when making this change. Finally, we’ll look at a tool for easing the transition.
Set a new default branch
You can use a branch other than main for new changes or change your main line of development in your repo. To change the default branch name for new repositories, see All repositories settings and policies.
To change your repo’s default branch for merging new pull requests, you need at least two branches. If there’s only one branch, it’s already the default. You must create a second branch to change the default.
This procedure might require you to Set Git repository permissions.
Under your project repo, select Branches.
On the Branches page, select More options next to the new default branch you want, and choose Set as default branch.
After you set the new default branch, you can delete the previous default if you want.
Select the settings button in your project bottom-left corner to open the project administration page.
Select Repositories.
Select your Git repository. Your branches are displayed under your repo.
Select the . next to the branch you want to set as default, then select Set as default branch.
Once you’ve set the new default branch, you may delete the previous one if you want.
Select the settings button in your project to open the project administration page.
Select Version Control.
Select your Git repository. Your branches are displayed under your repo.
Select the . next to the branch you want to set as default, then select Set as default branch.
Once you’ve set the new default branch, you may delete the previous one if you want.
There are other aspects you should consider before making this change.
Choose a name
Update other systems
When you change to a different default branch, other parts of your workflow may be affected. You’ll need to take these parts into account when you’re planning a change.
Pipelines
Update the CI triggers for all pipelines. Designer pipelines can be edited in the web. YAML pipelines can be edited in their respective repositories.
In-flight pull requests
Existing clones
Incoming links
Some bookmarks, documents, and other non-code files that point to files in Azure Repos will need to be updated. The branch name for a file or directory can appear in the URL.
Temporary mirroring
A Git repository can only have one default branch. However, for a while, you can set up ad-hoc mirroring between your old default and your new default. This way, if your end users continue pushing to the old default, they won’t need to redo the work on their end. We’ll use Azure Pipelines to set up this temporary mirroring.
The mirroring pipeline
These instructions are not fool-proof, and your repository setup may require additional changes such as loosening permissions and policies.
If the old and new default branches are both updated before this pipeline runs, then the pipeline won’t be able to mirror the changes. Someone will have to manually merge the old default branch into the new default branch to get it running automatically again.
For all existing CI builds, update them to trigger against your new default branch instead of the old one.
Grant the build identity Contribute permission to your repo. Navigate to Project Settings > Repositories > (your repo) > Permissions. There may be up to two identities, one for the project collection build service and the other for the project build service. Make sure the Contribute permission says Allow.
If the new default branch has branch policies, also grant the build identity the Bypass policies when pushing permission. This permission is a security risk since a malicious user could craft a pipeline to sneak code into a repository in your project. When mirroring isn’t needed anymore, be sure to remove this permission.
Troubleshooting
If the pipeline begins failing with an error message like «Updates were rejected because a pushed branch tip is behind its remote», someone will have to merge the old branch into the new branch by hand.
How to set the default branch in GitHub.com?
I found the following info, but didn’t find where to go to access it. Didn’t find it in account settings. Is there a repo settings page? Do you have to be the repo owner?
How to set the default branch on github.com when I go to browse commits.
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Do you have to be the repo owner?
How to set the default branch on github.com when I go to browse commits.
If you fork the repository, you can change the default branch of your fork, by clicking on the Settings button (see below).
This will allow you to pick your favorite branch as the default one for your own fork, but you’ll have to keep your forked repository in sync with the upstream repository by yourself.
Or you can directly jump to the following url https://github.com/<:user>/<:repo>/settings
Just log into your GitHub account and on the far right side in the navigation menu choose Settings, in the Settings Tab choose Default Branch and return back to main page of your repository.
Here’s a visual for the answer:
2021 Update
Click the Settings tab then, on the left, click Branches:
Mousing over the switch branch icon (right/left arrow, bottom right of pic) pops up a hint. [or click the pencil icon to give the default branch a new name]:
Clicking on switch branch pops up a list of branches:
Select the new default branch (I chose master), click Update, and acknowledge that you know what you’re doing:
Initially, I confirmed the «Default Branch» was ‘master’ even though clones were defaulting to another branch ‘stackato’. Here’s what I tried.
Change and change back
and/or Wait a while
I cannot rule out the possibility that it just took some time for my change to ‘propagate’.
Change from master to a new default branch git
Here is a scenerio:
We have a default branch Master and we work off from this, creating branches and pushing up etc.
We have now created a Develop branch off Master and set this as a default development branch.
What I would like to know is, how do I now know if my git pull command is requesting changes from the default branch via the command line? or point to this new default branch?
Also, any neww branch, will it be from Master or Develop?
update: this change of default branch occurred after cloning the repo.
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Replace
When cloning a repo from GitHub, the default branch gets stored in the HEAD file:
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:
-a will set refs/remotes/ /HEAD according to remote
Or explicitly to a named branch:
Now, HEAD points to the new location:
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.
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
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:
How to change default branch in git
The R community is a diverse and inclusive group. About a month ago (June 2020) GitHub announced it would start to remove references to ‘master’ across its sites and I’m surprised the R community hasn’t jumped on the opportunity to be on the bleeding edge of this change. I think we can all take a very quick and very small step forward in removing divisive language in tech by changing the default branch name in R package repositories to ‘main’ instead of ‘master’. There are a wealth of other posts/tutorials that others have written. Here is a small sampling listed in order of publishing date:
Eugene Shen even made a small web app to automate the entire process for you that is available at: (https://eyqs.ca/tools/rename/). I thought I’d share the 5 simple steps that I tested and used to make the change in under 1 minute. Yes, you can do this in under a minute, so what’s stopping you? If you are concerned about compatibility, GitHub is providing updates on their renaming efforts here: https://github.com/github/renaming.
Below are screenshots to walk you through each step. In addition, there is a block of code with all Terminal commands in one place at the end of this post here and GitHub repo here: https://github.com/StevenMMortimer/master-to-main.
Steps
Remember that git is version control software on your local machine and GitHub is the remote server that stores your code. For this reason, you’ll have to push your new ‘main’ branch up to GitHub and tell the local branch to start tracking the remote branch with the same name.
At this stage if ‘master’ was your default branch you cannot remove it without first changing HEAD, the pointer to the current branch reference. This following command will point it to ‘main’.
All three steps are shown in the screenshot below:
Now that the local ‘master’ is gone and there’s nothing pointing to it on Github you can delete it with the following command:
That’s it! You’re done! You should no longer see “master” or “remotes/origin/master” locally or on your GitHub repository. If you want to check, you can always run the following:
Источники информации:
- http://docs.microsoft.com/en-us/azure/devops/repos/git/change-default-branch?view=azure-devops
- http://stackoverflow.com/questions/11334045/how-to-set-the-default-branch-in-github-com
- http://stackoverflow.com/questions/51274430/change-from-master-to-a-new-default-branch-git
- http://stevenmortimer.com/5-steps-to-change-github-default-branch-from-master-to-main/