Issue
-
I would like to rename a Git branch and retain the original branch build history within Jenkins.
-
Example: "We are changing our
master
branch to be namedmain
."
Resolution
1) Stop Jenkins or otherwise ensure the associated jobs are disabled.
2) Take backups:
-
Backup your
$JENKINS_HOME
. See our Backup and Restore Guide for further information. -
Backup your repository. Ensure you have a full clone of the current state of your repository prior to making any changes. This page has a few options detailed for creating backups of your Git repository.
3) Modify the git branches both on your local and on the remote repository.
-
Rename (move) your old branch to the a new branch:
-
git branch -m <old_name> <new_name>
-
-
Change your default branch on your remote repository:
-
For example, in GitHub UI: {repository} -> Settings -> Branches -> Default Branch
-
-
Delete the old branch from remote:
-
git push <remote> --delete <old_name>
-
-
Push your new branch to remote:
-
git push <remote> <new_name>
-
-
Reset the upstream branch for the new branch name:
-
git push <remote> -u <new_name>
-
4) Modify relevant files in the $JENKINS_HOME
directory
-
Navigate to the associated directory:
-
cd $JENKINS_HOME/jobs/<job_name>/branches
-
-
Rename the old branch folder name to the new branch name:
-
mv <old_name> <new_name>
-
-
Navigate into the new branch’s directory:
-
cd $JENKINS_HOME/jobs/<job_name>/branches/<new_name>
-
-
Modify the various config files associated with this branch’s builds:
-
vi config.xml
-
Swap instances of
<old_name>
with<new_name>
-
Ensure commit hash matches new branches latest commit hash
-
-
vi scm-last-seen-revision-hash.xml
-
Swap instances of
<old_name>
with<new_name>
-
Ensure commit hash matches new branches latest commit hash
-
-
vi scm-revision-hash.xml
-
Swap instances of
with -
Ensure commit hash matches new branches latest commit hash
-
5) Restart Jenkins
6) Run index scan for impacted Multibranch job
7) Verify your newly renamed branch contains your build history from before the rename.
References
-
[w3docs
How to Rename Git Local and Remote Branches](https://www.w3docs.com/snippets/git/how-to-rename-git-local-and-remote-branches.html)
-
[git-memo
Repository Backup](https://git-memo.readthedocs.io/en/latest/repository_backup.html)