VPS

How to Rename a Git Branch

Git is a distributed version control system where all members within a team have a complete version of the project. Initially working with Git in the command line may be inconvenient. But as you moves on working with git through it, you will start to love it. Let’s cover how to rename a Git branch and some extra topics.

Prerequisites

For this tutorial, we recommend you get to know Git before moving on, including the basic Git commands. You will also have to log into your VPS server using SSH. You’ll also need to have Git installed onto your VPS.

Git repositories

A repository is just like a folder for your project which contains all of your project’s files and stores file’s revision history. The repositories can be private or public. You can also share repositories 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.

Git Branches

Basically, a Git branch is a pointer to the snapshot of the changes you made to the project files. It is useful in situations where you want to add a new feature or fix a bug within the projects files by spawning a new branch. A branch not only encapsulates the changes but also makes sure that the unstable code doesn’t get merged into the main project’s files. Once you are done with updating codes for a branch, you can merge the working branch with the main one known as the master branch.

How to Clone a Remote Repository

To clone a repository use the clone option of the Git command. Moreover, you also need to specify URL of the repository:

1. Clone the master branch

You can clone the master branch from the remote repository by using HTTPS or SSH. We’ll use HTTPS:

git clone https://github.com/user_name/your_apps.git

2. Navigate to the root of the cloned repository

To navigate to the root of the cloned repository you can use the cd command:

cd your_apps

3. Check the status of the branch

Checking the status of the branch can be done easily with the following Git command:

git status

How to Create a New Local Git Branch

Before you create a new branch, remember that every repository contains a master branch that reflects a production ready state of your projects:

1. Navigate to the root of the master branch

This can be done easily with the cd command:

cd your_apps

2. Create a new branch from the master branch

To create a new branch from a master branch, just use the following Git command:

git branch new-branch-name

3. Checkout the new branch

Checkout your new Git branch by using the checkout option for the command:

git checkout new-branch-name

Or

git checkout -b new-branch-name

4. Verify new branch

Once you have checked out to new branch, verify that you are on the new branch:

git status

Rename a Git Branch – Local

Make sure you’re on the branch you want to rename:

1. Rename a Git branch with the -m command option

The Git rename command will require you to ad the -m option to your command:

git branch -m new-name

2. Rename a Git branch from another branch

You can also rename a branch from another branch. The following example shows, how to rename a Git branch from the master branch:

git checkout master
git branch -m old-name new-name

3. Verify the process

List all the branches to verify that the branch you want has been renamed:

git branch -a

Rename a Git Branch – Remote

It is not possible to rename a remote branch directly. The process of renaming a remote branch involves following three steps:

1. Rename the local branch

To start with, you need to rename a local branch by following previous steps. Once you have renamed the local branch, delete the old-name remote branch and push the new-name local branch.

2. Delete the remote branch with the old name and push the new branch to the remote repository.

You can do this easily with the following command:

git push origin :old-name new-name

3. Reset the upstream branch

Reset the upstream branch for the new-name local branch and you are done with renaming remote branch.

git push origin -u new-name

Remove Local Git Branch

To remove a local branch, you can use either of the following Git commands.

git branch -d branch_name

Or

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 branches.

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

Remove Remote Git Branch

You can remove a remote branch using following git command by specifying remote name and branch name. In most cases the remote name is  origin. The command would look like this:

git push remote_name --delete branch_name

Alternatively, you can also use the following Git command to do the same:

git push remote_name :branch_name

Conclusion

That’s it! You can now manage Git branches by using git branch  commands. You can rename a Git branch, create one, list existing Git branches, and delete branches. Hope you found this tutorial helpful! See you in the next one.

About the author

Edward S.

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.

Add Comment

Click here to post a comment

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

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!

More in VPS
Linux Tail Command: What It is and How to Use It
Close