Rename a git branch locally and remotely

When working on a group project on git, you create a branch. Except that you realize that it doesn’t follow the convention set by the team or that the name is simply incorrect. Git allows us to rename our branch very easily, and we’ll see how to do it together.

We’ll take the feature/hello branch as an example of the branch we want to rename to feature/hello-there.

live server vscode install extension

Rename the branch locally

git branch -m feature/hello feature/hello-there  
live server vscode install extension

Delete the old branch

git push origin :feature/hello  
live server vscode install extension

Push to the new branch

Push on the new branch while defining that the local branch will follow the new remote branch.

git push --set-upstream origin feature/hello-there  
live server vscode install extension
live server vscode install extension

Conclusion

git branch -m old new  
git push origin :old
git push --set-upstream origin new  

Why not make it an alias? 🤔

We have several choices, we can either create a git alias or a bash alias. In this example, I give you a bash alias. I have also put a link for git aliases 😉

alias gmv='mv() { git branch -m $1 $2; git push origin :$1; git push --set-upstream origin $2; }; mv'

# example of use
gmv feature/hello-there feature/hello-there-another-time

Note that for the git conventions on my projects I use the Angular conventions.

https://github.com/angular/angular/blob/master/CONTRIBUTING.md

See also

Before you leave…
Thanks for reading! 😊