How to Rename Local and Remote Git Branch

Published on April 01, 2020

git

If you are developing a software project along with many other team members and use a git repository, you will most likely be using branches. Let's assume the name of your branch is feature. Your steps to check in your code would be something like this:

git checkout feature
git add .
git commit -am 'Added xyz'
git push -u origin feature

Your local branch feature is now pushed to remote branch.

You get a request to rename the branch to assortment.

If you hadn't pushed the branch to remote, this would have been the solution:

git branch -m assortment

However, since the branch has been pushed to remote, we need to do a few extra steps.

  1. Rename the local branch:
git checkout feature
git branch -m assortment
  1. Push the newly named local branch assortment and reset the upstream:
git push -u origin assortment

If the above command doesn't work, you can edit the file .git/config and replace the remote upstream name feature with the local branch name assortment.

  1. Delete the old name remote branch feature:
git push origin --delete feature

This would effectively rename the remote branch to the new name.

You can verify all branches with:

git branch -a

and you should be able to see the local and remote assortment branches.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on April 01, 2020