A Simple Way to Rename Local and Remote Git Branchby@kodewithchirag

A Simple Way to Rename Local and Remote Git Branch

tldt arrow
Read on Terminal Reader

Too Long; Didn't Read

Git allows us to rename the git branch locally and under the remote. Renaming a local Git branch is just a matter of running a single “git branch -m” command. You can’t directly rename a remote branch as you need to push the renamed local branch to the remote repository and then delete the old branch from there. If your local branch is already pushed to a remote repository then this command will help you to rename it. If you want to check the changes then you can log in to your GitHub or GitLab portal and see the changes there.

Company Mentioned

Mention Thumbnail
featured image - A Simple Way to Rename Local and Remote Git Branch
Kode with Chirag HackerNoon profile picture

@kodewithchirag

Kode with Chirag

Learn More
LEARN MORE ABOUT @KODEWITHCHIRAG'S EXPERTISE AND PLACE ON THE INTERNET.
react to story with heart

Have you ever come across a situation where you want to rename a Git branch? If yes, then this article will help you with that. Earlier, I faced the same situation where I wanted to rename the git branch locally and on remote, and luckily I found that git allows us to rename the branch very easily, lets see how.

I will share the solutions to rename the git branch locally and under the remote.

Rename local git branch

If you are already in the local branch which you wanted to rename, you can hit this command.

git branch -m <new_name>

If you are under another branch and want to rename the branch then hit the below command.

git branch -m <old_name> <new_name>

Now check your current branch name by hitting

git status 

or
 
git branch 

you will see the changes, isn’t it is so simple 😉

Rename remote git branch

If your local branch is already pushed to a remote repository and you want to rename it and reset the upstream branch then this command will help you to rename it.

git push origin -u <new_name>

git push origin --delete <old_name>

Now if you want to check the changes then you can login to your GitHub or GitLab or whatever the git client portal you are using and see the changes there.

Conclusion

Renaming a local Git Branch is just a matter of running a single “git branch -m” command. However, you can’t directly rename a remote branch as you need to push the renamed local branch to the remote repository and then delete the old branch from there.

Hope you liked this article and found it useful, feel free to comment with your thoughts and opinions and stay connected with me here and on Twitter🐦.

Previously published here.

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa