access_time
hourglass_empty
person_outline

How to Rename a Local and Remote Git Branch – A Quick Guide

When you’re working in Git, sometimes you accidentally name a branch the wrong way or simply want your project to be better organized. These are rather common incidents, so, let’s cover how to rename both local and remote Git branches. We will also briefly explain what Git repositories are and mention some other helpful commands.

Install and use Git alongside powerful web hosting solutions with Hostinger.

90% OFF for Web Hosting

How to Rename a Local Git Branch?

Before we begin, make sure you’ve selected the branch you want to rename:

git checkout old-name

If you want to see all of your local branches, use the following command:

git branch --list

When you’re all clear, follow these steps:

  1. Using the Git rename branch command will require you to add an -m option to your command:
    git branch -m new-name
  1. You can also rename a local branch from another branch by using the following two commands:
    git checkout master
    git branch -m old-name new-name
  1. Lastly, this command will list all — both local and remote — branches to verify that it has been renamed:
    git branch -a

How to Rename a Remote Git Branch?

Although it isn’t possible to rename a remote branch directly, the process of renaming one involves these three easy steps:

  1. To start, you will need to rename a local branch by following the previous steps.
  2. Then delete the old branch and push the new one. You can do this easily with the following commands:
    git push origin --delete old-name
    git push origin :old-name new-name
  1. Reset the upstream branch for your new local branch and you will be all set:
    git push origin -u new-name

How to Create a New Local Git Branch?

Before you create a new branch, remember that every repository, which we will talk about later, contains a master branch that reflects a production-ready state of your projects. When you create a branch, all Git is doing is creating a new pointer.

We can create a new local branch by following the steps below.

  1. Navigate to the root of your master branch:
    cd repository-name
  1. You can create a branch from a master branch with the following command:
    git branch new-branch-name

    Or you can create a new branch and switch to it:

    git checkout -b new-branch-name

    You can also clone a branch and then switch to it:

    git checkout -b new-branch-name origin/new-branch-name
  1. Switch to your new branch:
    git checkout new-branch-name
  1. Finally, verify that you are on the new branch:
    git status

How to Remove a Local Git Branch?

To remove a local branch, you can use one of the following Git commands:

git branch -d branch_name
git branch -D branch_name

The -d option (–delete) will remove your local branch if you have already pushed and merged it with the remote branch.

The -D option (–delete –force) will remove the local branch regardless if you have pushed and merged it with the remote branch.

How to Remove a Remote Git Branch?

You can also remove a remote branch by specifying both the remote and branch names. In most cases, the remote name is origin, and the command will look like this:

git push remote_name --delete branch_name
git push remote_name :branch_name

Inspection and Comparison

In Git, you can view any changes that you’ve made at any time. To see these changes enter the following command:

git log

Or, for a more detailed summary:

git log --summary

What Are Git Branches?

Git is a distributed version control system (DVCS) where all members within a team have a full version of a project. It’s designed specifically with performance, security, and flexibility in mind when it comes to project management.

Branches are an isolated line of your project’s development. They are a way to work alongside with your master branch, but free from any code that’s not fully ready. Branches help clean up the cluttered history before you merge them together.

Git branching helps you create, delete, and list other branches. However, a branch also acts as a pointer to the snapshot of the changes you’ve made — or wish to make — to the project’s files. It is useful in situations where you want to add an additional feature or fix a bug within the project.

A branch not only encapsulates the changes but also makes sure that unstable code doesn’t get merged into the main project’s files. Once you are done updating the code for a branch, you can merge the working branch with the master branch.

What is a Git Repository?

A repository acts like a folder for your project — it contains all of your files and stores their revision history. Repositories can be private or public and you can also share them with other people in your organization.

When you initialize a Git repository, it creates a .git/ directory in the root of the project folder. This is where Git tracks changes in the project files, stores objects, refs, and more information to manage repositories.

Be careful not to delete the .git/ folder, unless it’s done deliberately because you will delete all of your project’s history.

How to Clone a Remote Repository?

To clone a repository, we will use the Git clone command. Additionally, you will also need to specify the URL of the repository:

  1. You can clone the master branch from the remote repository by using HTTPS or SSH.
    For HTTPS:

    git clone https://github.com/user-name/your-repository-name.git

    For SSH:

    git clone ssh://github.com/user-name/your-repository-name.git
  2. To navigate to the root of the cloned repository, you can use the cd command:
    cd your_apps
  1. Checking the status of the branch can be done easily with the following Git command:
    git status

Additional Information on Git

If you need information on how to use, there is official documentation available online. Furthermore, check out our article on basic Git commands, how to use PuTTY SSH terminal to connect to your hosting account or a VPS server, how to have Git installed on Ubuntu as well and a comprehensive guide on GitHub.

Conclusion

You now know how to manage Git branches by using different commands — you can rename a branch, create one, list existing ones, and delete them as well.

We hope you found this tutorial helpful. See you in the next one.

The Author

Author

Edward S. / @edvardasstabinskas

Edward is Hostinger's copywriter. He's an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Related tutorials

Author

Salim Reply

September 15 2019

Thank you for taking the time to write such a clear tutorial!

Author

Noji Ratzlaff Reply

September 23 2019

Nevermind

Leave a reply

Comment*

Name*

Email*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in GIT
Basic GIT Commands

Close