Changes

Jump to: navigation, search

GitHub/Changing Default Branch

2,055 bytes added, 22:54, 19 January 2021
Add instructions on preventing reuse of the old branch
There are many good reasons to rename the default branch for your repository. GitHub gives some of the reasons and [https://github.com/github/renaming/ provides guidance] for the procedure.

However, if your repository has been in use for a while, everyone who has clones of the repository much update their clones to properly reflect the change. GitHub will add messages to the output of git commands to inform folks, but it is very easy to miss those. If the per-clone changes are not made, then the branch could accidentally be re-created with old contents. The procedure below provides a workaround, which can be enabled by a repository admin.

1. Rename the default branch, following [https://github.com/github/renaming/#renaming-existing-branches GitHub's instructions].

2. Create a branch protection rule for the old branch name ([https://docs.github.com/en/github/administering-a-repository/about-protected-branches instructions]). Make sure the following checks are enabled:
* [https://docs.github.com/en/github/administering-a-repository/about-protected-branches#require-pull-request-reviews-before-merging Require pull request reviews before merging] - any number is fine
* [https://docs.github.com/en/github/administering-a-repository/about-protected-branches#require-status-checks-before-merging Require status checks before merging]
* [https://docs.github.com/en/github/administering-a-repository/about-protected-branches#include-administrators Include administrators]

3. Unfortunately, that branch protection rule doesn't apply until the 2nd commit to the old branch name. So make an unrelated first commit:
mkdir /tmp/rename
cd /tmp/rename
touch do_not_use_this_branch_name
echo "Do not use this branch name!" > README.md
git init
git add --all
git commit -m "Block commits to this branch name on GitHub"
git push git@github.com:$OWNER/$REPOSITORY HEAD:refs/heads/$OLD_BRANCH_NAME
4. Review any CI configurations that may refer to the old branch name, and update as needed.
Confirm
1,351
edits

Navigation menu