How to change default branch on github

How to change default branch on github

github/renaming

Use Git or checkout with SVN using the web URL.

Work fast with our official CLI. Learn more.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Renaming the default branch from master

We’re not the only organization in the Git ecosystem making these changes: there are upcoming changes in the Git project (statement, code change), as well as coordinated changes from multiple vendors.

We’re making changes to GitHub in a few phases, designed to cause as little disruption to existing projects as possible.

What’s already changed

New repositories use main as the default branch name

Users, organizations, and enterprises that previously selected a default branch for new repositories are not impacted by this change. Existing repositories are also not impacted by this change.

main is the most popular replacement for master that we’re seeing across GitHub. We like it because it’s short, it keeps your muscle memory intact, and it translates well across most languages. We’re using main for our newly-created repositories and for the repositories we’re moving now, like dependabot-core.

Renaming existing branches

You can now rename any branch, including the default branch, from the web.

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

Renaming a branch will:

About

Guidance for changing the default branch name for GitHub repositories

About branches

In this article

Use a branch to isolate development work without affecting other branches in the repository. Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.

Branches allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.

You always create a branch from an existing branch. Typically, you might create a new branch from the default branch of your repository. You can then work on this new branch in isolation from changes that other people are making to the repository. A branch you create to build a feature is commonly referred to as a feature branch or topic branch. For more information, see «Creating and deleting branches within your repository.»

You can also use a branch to publish a GitHub Pages site. For more information, see «About GitHub Pages.»

You must have write access to a repository to create a branch, open a pull request, or delete and restore branches in a pull request. For more information, see «Access permissions on GitHub.»

About the default branch

When you create a repository with content on GitHub.com, GitHub creates the repository with a single branch. This first branch in the repository is the default branch. The default branch is the branch that GitHub displays when anyone visits your repository. The default branch is also the initial branch that Git checks out locally when someone clones the repository. Unless you specify a different branch, the default branch in a repository is the base branch for new pull requests and code commits.

By default, GitHub names the default branch main in any new repository.

You can change the default branch for an existing repository. For more information, see «Changing the default branch.»

Working with branches

Once you’re satisfied with your work, you can open a pull request to merge the changes in the current branch (the head branch) into another branch (the base branch). For more information, see «About pull requests.»

After a pull request has been merged, or closed, you can delete the head branch as this is no longer needed. You must have write access in the repository to delete branches. You can’t delete branches that are directly associated with open pull requests. For more information, see «Deleting and restoring branches in a pull request»

If you delete a head branch after its pull request has been merged, GitHub checks for any open pull requests in the same repository that specify the deleted branch as their base branch. GitHub automatically updates any such pull requests, changing their base branch to the merged pull request’s base branch. The following diagrams illustrate this.

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

Now when you merge the feature2 pull request, it’ll be merged into the main branch.

Working with protected branches

Repository administrators can enable protections on a branch. If you’re working on a branch that’s protected, you won’t be able to delete or force push to the branch. Repository administrators can additionally enable several other protected branch settings to enforce various workflows before a branch can be merged.

Note: If you’re a repository administrator, you can merge pull requests on branches with branch protections enabled even if the pull request does not meet the requirements, unless branch protections have been set to «Include administrators.»

To see if your pull request can be merged, look in the merge box at the bottom of the pull request’s Conversation tab. For more information, see «About protected branches.»

When a branch is protected:

mheap/github-default-branch

Use Git or checkout with SVN using the web URL.

Work fast with our official CLI. Learn more.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

GitHub Default Branch

For each repo, this tool will:

Create a personal access token with the repo scope. This is the value for in the examples.

If you don’t want your token to be stored in your shell history, you can set GITHUB_TOKEN in the environment and that will be read instead

Set DEBUG=»ghdb*» as an environment variable to see debug information

FlagDescriptionDefault
—patGitHub API TokenN/A
—oldThe name of the branch to renamemaster
—newThe new branch namemain
—repoThe repo to update (format: user/repo)N/A
—userUpdate all repos owned by the provided user (example: my-user)N/A
—orgUpdate all repos in the provided org (example: my-org-name)N/A
—teamUpdate all repos in the provided team (example: my-team-name), only usable in combination with org parameterN/A
—dry-runOutput log messages only. Do not make any changesfalse
—skip-forksSkips forked repositoriesfalse
—confirmRun without prompting for confirmationfalse

Part of this script checks for the existence of files and updates their contents. Replacements are the mechanism for these updates.

If there is nothing to replace, then the script moves on to the next replacement.

How to Add a Replacement

The file with the path in your repo will have any line matching from be swapped out with to

How can I create a Git repository with the default branch name other than «master»?

In the Pro Git book, it says

That means, we can use our default branch name as main or main-branch or something like that. I didn’t see any option in man git-init which will initialize my repo with a different default branch name.

GitHub shows how to set the default branch name in its settings page. But I am not talking about how to set it on any specific Git hosting site. I am strictly asking in terms of Git only, not in regards to any specific Git hosting site.

Is there a way to do that?

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

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

Newer Git, New Repo

This is configurable with the init.defaultBranch setting. If I want all new repos to have «trunk» as the default branch:

Older Git, New Repo

Bare Repos

For bare repos, you cannot run git checkout (that’s what it means to be bare). Instead, you can change HEAD to point at a different branch:

Old Repos

This renames the branch from master to trunk once it’s created.

This does seem a bit clunky since the mechanism is different depending on whether the repository is empty, but it works. You can also approach it as «creating a new branch and deleting master «.

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

The user can override the default value of the configuration variable with:

Read the git doc chapter for further details Introducing init.defaultBranch

How can I create a Git repository with the default branch name other than » master «?

You would use Git 2.28 (Q3 2020): the name of the primary branch in existing repositories, and the default name used for the first branch in newly created repositories, is made configurable, so that we can eventually wean ourselves off of the hardcoded ‘ master ‘.

This change is one of many changes GitHub is making to support projects and maintainers that want to rename their default branch.
To learn more about the changes we’re making, see github/renaming.

init : allow specifying the initial branch name for the new repository

Signed-off-by: Johannes Schindelin

There is a growing number of projects and companies desiring to change the main branch name of their repositories (see e.g. Mislav Marohnić’s tweet for background on this).

init : allow setting the default for the initial branch name via the config

Helped-by: Johannes Schindelin
Helped-by: Derrick Stolee
Signed-off-by: Don Goodman-Wilson

To allow users to override the initial branch name more permanently (i.e. without having to specify the name manually for each and every git init invocation), let’s introduce the init.defaultBranch config setting.

Note: commit 489947c, about the merge commit message, has since been reverted in Git 2.29, see «how can I customize git’s merge commit message?».
The init.defaultBranch setting remains.

This impacts submodules:

submodule : fall back to remote’s HEAD for missing remote..branch

Helped-by: Philippe Blain
Signed-off-by: Johannes Schindelin

Even so, it should be okay to fix this behavior without anything like a longer transition period:

Revert «contrib : subtree : adjust test to change in fmt-merge-msg «

Signed-off-by: Emily Shaffer

In 6e6029a8 ( fmt-merge-msg : allow merge destination to be omitted again) we get back the behavior where merges against ‘ master ‘, by default, do not include » into ‘master’ » at the end of the merge message. This test fix is no longer needed.

With Git 2.29 (Q4 2020), update the tests to drop word ‘ master ‘ from them.

tests : avoid variations of the master branch name

Signed-off-by: Johannes Schindelin

The term master has a loaded history that serves as a constant reminder of racial injustice. The Git project has no desire to perpetuate this and already started avoiding it.

The test suite uses variations of this name for branches other than the default one. Apart from t3200, where we just addressed this in the previous commit, those instances can be renamed in an automated manner because they do not require any changes outside of the test script, so let’s do that.

Seeing as the touched branches have very little (if anything) to do with the default branch, we choose to use a completely separate naming scheme: topic_ (it cannot be topic- because t5515 uses the test_oid machinery with the term, and that machinery uses shell variables internally, whose names cannot contain dashes).

This trick was performed by this (GNU) sed invocation:

And, still with Git 2.29:

t1415 : avoid using main as ref name

Signed-off-by: Johannes Schindelin

Since the refs in question are worktree-local ones (i.e. each worktree has their own, just like HEAD ), and since the test case already uses a secondary worktree called » second «, let’s use the name » first » for those refs instead.

While at it, adjust the test titles that talk about a «repo» when they meant a «worktree» instead.

Git Better

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

How to change git default branch from master

Easily rename the default branch master to other name

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

Hey there! I’m Srebalaji. You are receiving this email because you have subscribed to level up your game in Git.

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

Recently, there are many suggestions about renaming the default branch master to some other name. This was mainly due to the master-slave metaphor that some people are talking about.

There is evidence that states it was intended to mention master-copy or master recording. And it is not intended to master-slave.

But I think it’s people’s perspective and if some think that they are not cool with that they can change the default branch.

So let’s see how to change the default git branch master to default (you can use your preferred name)

Before starting I have to tell you that I have tried this in multiple repos and its working fine without any breaking changes. If you are not confident enough then fork your repo and try it first.

Prerequisites

Make sure your CI/CD flow doesn’t get interrupted.

Make sure you have merged all the PRs targeting master. Other PRs are fine

There are three steps in renaming the default branch

Change the branch name

Set remote upstream tracking for the new branch

Change the new branch name in repo host (Github, Gitlab)

Change the branch name

The above command just renames the default branch to the name default.

-m attribute is used to rename the branch name without affecting the branch’s history.

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

Now the default branch is changed in the local but not in the remote.

Set remote upstream tracking for the new branch

The above command will push the new branch to the remote.

-u attribute is used to set the upstream tracking for the branch.

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

As you can see that the upstream is set for the new branch. But still, the reference to the old upstream is present in the local.

Change the new branch name in the repo host

In this tutorial, let’s consider Github. But the same option is also available in Gitlab, Bitbucket.

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

That’s it you are done.

But remember that the old branch’s upstream is still present. It won’t affect your workflow. But you should delete it to keep your repo clean.

To delete the old branch’s upstream you can use

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

As you can see the old remote stream is deleted.

Now the changes are done in your local and in the remote host. Let’s see how to bring those to other people who are already using the repo.

I mean there will be other people who will be using this repo. They have to do few changes to complete the flow.

How to change default branch on github. Смотреть фото How to change default branch on github. Смотреть картинку How to change default branch on github. Картинка про How to change default branch on github. Фото How to change default branch on github

As you can see still the branch master is present in the local of others who are already using the repo.

There are three steps for people who are already using the repo

Fetch all the branches

Update the upstream remote’s HEAD

Rename the default branch

Fetch all the branches

The above command will just fetch all the remote branches to your local.

Update the upstream remote HEAD

The above command will query the remote host for the HEAD upstream and it updates that upstream in the local.

Rename the default branch

This is the same as the old one. We are just moving the branch without affecting the history of the branch.

As we have already set the remote upstream in the previous step, the new branch is changed and is in sync with the remote.

Now the person can work with the default branch.

As I said earlier, I have tried this is in multiple repos of mine and it’s working fine without any breaking changes. This may seem to be confusing at first, but if you understand the process and read it multiple times you will get familiar.

If you got any doubts or stuck somewhere, you can contact me.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *