Skip to content

Latest commit

 

History

History
47 lines (41 loc) · 1.52 KB

File metadata and controls

47 lines (41 loc) · 1.52 KB

Rename GitHub Default Branch

If you try to rename the default branch of a repo, when you delete the old branch from the remote, it closes all pull requests that had it as a base.

This python script:

  • Renames the branch on your local repo
  • Pushes the new branch
  • Changes all pull requests with the old branch as a base to the new branch
  • Changes the repo's default branch in GitHub
  • Deletes the old branch from GitHub

Protected Branches

GitHub's API does not allow changing the Branch Pattern Name of a branch protection rule. You will have to do this as a manual step before running the script.

Install

  1. Set up a GitHub Personal Access Token
  2. Clone this repo
  3. Install dependencies
$ pip install PyGithub
$ pip install gitpython

Usage

  -h, --help            show this help message and exit
  -p PATH, --path PATH  Path to Local Repo
  -t TOKEN, --token TOKEN
                        GitHub Personal Access Token
  -r REPO, --repo REPO  org/repo name
  --old-branch OLD_BRANCH
                        Old Branch Name
  --new-branch NEW_BRANCH
                        New Branch Name

Example

$ python rename_default_branch.py -p ~/git/my-repo -t token -r pcarn/my-repo --old-branch master --new-branch main
Renamed local branch from main to master
Pushed new branch
Changed PR #2 from base main to master
Changed PR #1 from base main to master
Changed repo's default branch to master
Deleted remote branch main