Skip to content

A tool to rename the github default branch without closing PRs

License

Notifications You must be signed in to change notification settings

pcarn/rename-github-default-branch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A tool to rename the github default branch without closing PRs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages