Migrating your GitHub default branch is not difficult.
NOTE: For clarity, we assume a migration from master
to main
, but the advice and tool are useful for any value of $previous_default_branch
and $new_default_branch
For most repositories (no CI tooling and no backlog of open PRs):
-
Create the new branch via the UI
- On GitHub, navigate to the main page of the repository.
- Branches link on overview page
- Click the branch selector menu.
- Type the name of your new branch, then select Create branch.
-
Alternatively, you can use the command line
> git checkout -b main master > git push origin main
-
- Click
Settings
in the top repository menu (to the far right of code, issues, pull requests, etc) - On the left hand menu, click
branches
- Change the default branch in the dropdown
- Click
You may need to be more thoughtful if your repository is slightly more complex:
- You have CI that runs off master
- You have existing PRs that need to be updated
- Create
main
branch and have it mimicmaster
. Whenver a change is merged intomaster
,main
will be updated as well.- This GitHub action will automatically create the
main
branch - On every merge to
master
, this GitHub action will updatemain
to the same commit.
- This GitHub action will automatically create the
- Then, you can update any Continuous Integration/Continuous Deployment scripts to point to
main
.- This is specific to your repository and setup
- This can happen at your own pace
- Don't manually merge anything into
main
during this time. All developers should continue to usemaster
.
- Once everything is updated to work with
main
, switch the default branch in the GitHub UI.- See step 3 from Simple Repositories above.
- All developers should now use
main
instead ofmaster
- Update all open Pull Requests that are based on
master
to be based onmain
.- This GitHub action will update all pull requests based on
master
to be based onmain
when:- The default branch is correctly set to
main
in GitHub - A commit is merged to
main
- The default branch is correctly set to
- This GitHub action will update all pull requests based on
- Congratulations
- You can uninstall this action from your repo
- Create a file:
.github/workflows/default-branch-migration.yml
- Copy and paste the following:
name: Default Branch Migration
on: [push]
# The script does nothing if the push isn't to one of the default branches
# However, it still runs before exiting. If you would like to not even start it,
# you can specify the two branches here, uncomment it, and remove the `on: [push]`
# on:
# push:
# branches:
# # TODO: Change this to the default branch you want to migrate away from
# - master
# # TODO: Change this to the default branch you want to migrate to
# - main
jobs:
migrate_branch:
name: Migrate Branch
runs-on: ubuntu-latest
steps:
- name: Migrate
uses: liyanchang/[email protected]
with:
# GitHub will fill in this template with the correct token
# You don't need to edit this line
github_token: ${{ secrets.GITHUB_TOKEN }}
# TODO: Change this to the default branch you want to migrate away from
previous_default: master
# TODO: Change this to the default branch you want to migrate away to
new_default: main
If you're in an organization, you may need to enable GitHub actions. Namely, click on the repo settings, then click on Actions in the left menu bar, then Enable local & third party Actions
.