How to rename a git branch

  • 13 May 2020
  • ADM

 

How to rename a git branch - images/logos/git.jpg

 

If you need to rename a branch follow the next steps.

Git status - check your current branch

To check the current branch run the command git status

$ git status
On branch FEATURE-128
Your branch is up-to-date with 'origin/FEATURE-128'.

nothing to commit, working tree clean

Git rename branch - Rename your local branch

If you are already on the branch you want to rename, use the following command:

$ git branch -m <new-name>

Example

$ git branch -m FEATURE-200

If you are on a different branch use the following command:

$ git branch -m <old-name> <new-name>

Example

$ git branch -m FEATURE-128 FEATURE-200

Git replace origin

To delete the old remote branch and push the new branch, use the following command:

$ git push origin :<old-name> <new-name>

Example

$ git push origin :FEATURE-128 FEATURE-200
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for FEATURE-200, visit:
remote:   https://gitlab.com/admfactory/restrepo/-/merge_requests/new?merge_request%5Bsource_branch%5D=FEATURE-200
remote:
To https://gitlab.com/admfactory/restrepo.git
 - [deleted]         FEATURE-128
 * [new branch]      FEATURE-200 -> FEATURE-200

Reset upstream branch

To reset the the upstream branch, use the following command:

$ git push origin -u new-name

Example

$ git push origin -u FEATURE-200
Everything up-to-date
Branch FEATURE-200 set up to track remote branch FEATURE-200 from origin.

 

References