Image for post
Image for post

git — Rename and Delete Branches

Krishna Damaraju
Jun 27, 2017 · 3 min read

Silly mistakes are common while typing and I do them a lot. But think if it happens while naming any branch in git; you have to react quickly before anyone finds it, right?. And thankfully it is possible to correct them in git and it is very easy.

Image for post
Image for post

If you are a new to git, you might find these words very often in the article: local and remote.

local — is the code on your machine/device and remote —is the code repository hosting services like GitHub, GitLab, Atlassian BitBucket or even your own git server where you maintain all of your code…

So this post covers

✅ Renaming a branch locally and Updating it in the remote

✅ Deleting a branch in the local and in the remote

Image for post
Image for post

Step-1: Renaming your local branch

  • type git branch to get the list of all branches.

if you are in the same branch

git branch -m new-name

if you are not in the same branch

git branch -m old-name new-name

Step-2: Pushing changes to the remote

if the renamed branch is not pushed to the remote then you can push it normally by

git push origin new-name

if the renamed branch is already there on the remote (if you push it using the above method, it will create a new branch instead of replacing it) use

git push origin :old-name new-name
Image for post
Image for post

Step-1: Deleting only in the local

if you want to delete a branch only in the local and not in the remote you can use this

git branch -d branch_name -or-git branch -D branch_name

use -D only If there are un-merged changes which you want to delete.

Step-2: Deleting only in the remote

You want a branch to present in the local but not in the remote, just do this

git push origin --delete branch_name

Step-3: Deleting in both local and remote

if this is the case, then

Complete Step-1 and do Step-2 or vice-versa.

And that’s it, you got your back ;P
Adios Amigos.

Thanks for reading my post. I’d like to hear your thoughts in comments. If you like the article, please click the💚 below so that more people may read it.

Small Things

Coding tips for beginners

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store