This page looks best with JavaScript enabled

Renaming Git Branches

 ·  ☕ 2 min read

The following commands show how to rename the “master” branch to “main”. This is only an example and is equally valid for any other branch. Just replace the names “master” and “main” accordingly.

1
2
3
4
5
6
7
8
# cd into the local repo:
$ cd /path/to/local/repo

# switch to the "master" branch:
$ git checkout master

# rename it to "main":
$ git branch -m master main

The previous code renames the “master” branch in your local repo. However, the remote branch is still “master”:

1
2
3
4
5
$ git status
On branch main
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Renaming the remote branch is achieved by pushing the new, local branch “main” to the remote, if the remote “master” have been your default branch, make “main” the new default branch and finally delete the “master” branch.

Make sure your current local HEAD branch is still “main” when executing the following command:

1
2
3
4
5
6
7
8
# push branch "main" to the remote repo and set it up for tracking from local "main":
$ git push -u origin main

# delete the remote branch "master"
# (this only works if origin/master is not default):
$ git push origin --delete master  
To https://github.com/<user>/<repo>.git
 - [deleted]           master

If other repos are connected to the same remote repo, then the following commands convert the name in those as well:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# switch to the "master" branch:
$ git checkout master

# rename it to "main":
$ git branch -m master main

# get the latest commits (and branches!) from the remote:
$ git fetch

# remove the existing tracking connection with "origin/master":
$ git branch --unset-upstream

# create a new tracking connection with the new "origin/main" branch:
$ git branch -u origin/main

Philipp Westphal
WRITTEN BY
Philipp Westphal
Software Developer & Structural Engineer