Skip to content

Commit d7f7429

Browse files
authored
Merge pull request #1476 from HonkingGoose/feature/add-section-change-existing-branch-name
2 parents 98cf2d6 + 4f50044 commit d7f7429

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

book/03-git-branching/sections/branch-management.asc

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,110 @@ $ git branch --no-merged master
7373
featureB
7474
----
7575
====
76+
77+
==== Changing a branch name
78+
79+
[CAUTION]
80+
----
81+
Do not rename branches that are still in use by other collaborators.
82+
Do not rename a branch like master/main/mainline without having read the section "Changing the master branch name".
83+
----
84+
85+
Suppose you have a branch that is called _bad-branch-name_ and you want to change it to _corrected-branch-name_, while keeping all history.
86+
You also want to change the branch name on the remote (GitHub, GitLab, other server).
87+
How do you do this?
88+
89+
Rename the branch locally with the `git branch --move` command:
90+
91+
[source, console]
92+
----
93+
$ git branch --move bad-branch-name corrected-branch-name
94+
----
95+
96+
This replaces your bad-branch-name with corrected-branch-name, but this change is only local for now.
97+
To let others see the corrected branch on the remote, push it:
98+
99+
[source,console]
100+
----
101+
$ git push --set-upstream origin corrected-branch-name
102+
----
103+
104+
Now we'll take a brief look at where we are now:
105+
106+
[source, console]
107+
----
108+
$ git branch --all
109+
* corrected-branch-name
110+
main
111+
remotes/origin/bad-branch-name
112+
remotes/origin/corrected-branch-name
113+
remotes/origin/main
114+
----
115+
116+
Notice that you're on the branch corrected-branch-name.
117+
The corrected branch is available on the remote.
118+
However the bad branch is also still present on the remote.
119+
You can delete the bad branch from the remote:
120+
121+
[source,console]
122+
----
123+
$ git push origin --delete bad-branch-name
124+
----
125+
126+
Now the bad branch name is fully replaced with the corrected branch name.
127+
128+
===== Changing the master branch name
129+
130+
[WARNING]
131+
====
132+
Changing the name of a branch like master/main/mainline/default will break the integrations, services, helper utilities and build/release scripts that your repository uses.
133+
Before you do this, make sure you consult with your collaborators.
134+
Also make sure you do a thorough search through your repo and update any references to the old branch name in your code or scripts.
135+
====
136+
137+
Rename your local _master_ branch into _main_ with the following command
138+
139+
[source,console]
140+
----
141+
$ git branch --move master main
142+
----
143+
144+
There's no _master_ branch locally anymore, because it's renamed to the _main_ branch.
145+
146+
To let others see the new _main_ branch, you need to push it to the remote.
147+
This makes the renamed branch available on the remote.
148+
149+
[source,console]
150+
----
151+
$ git push --set-upstream origin main
152+
----
153+
154+
Now we end up with the following state:
155+
156+
[source,console]
157+
----
158+
git branch --all
159+
* main
160+
remotes/origin/HEAD -> origin/master
161+
remotes/origin/main
162+
remotes/origin/master
163+
----
164+
165+
Your local _master_ branch is gone, as it's replaced with the _main_ branch.
166+
The _main_ branch is also available on the remote.
167+
But the remote still has a _master_ branch.
168+
Other collaborators will continue to use the _master_ branch as the base of their work, until you make some further changes.
169+
170+
Now you have a few more tasks in front of you to complete the transition:
171+
* Any projects that depend on this one will need to update their code and/or configuration.
172+
* Update any test-runner configuration files.
173+
* Adjust build and release scripts.
174+
* Redirect settings on your repo host for things like thee repo's default branch, merge rules, and other things that match branch names.
175+
* Update references to the old branch in documentation.
176+
* Close or merge any pull requests that target the old branch.
177+
178+
After you've done all these tasks, and are certain the main branch performs just as the _master_ branch, you can delete the _master_ branch:
179+
[source, console]
180+
----
181+
$ git push origin --delete master
182+
----

0 commit comments

Comments
 (0)