Home / Linux/Unix/BSD / Git Rename Branch

Git Rename Branch

Working with Git allows you to better manage projects with your team. However, you could encounter incidents when you accidentally name branches mistakenly.

That’s totally forgivable.

To fix this concern, we’ll explain the step by step procedures to rename local and remote git branches.

We’ll throw in instructions on how to delete a branch git too. Let’s get those projects organized!

What is Git?

Git is a distributed version control system (DVCS) created by the Linux operating system makers.

Git helps programmers check the progress of their software development process.

Rather than just having one working copy, repository branches record in the form of snapshots where all history of changes are made.

Branches are isolated lines of your software development. This helps you monitor questionable codes.

This is especially beneficial if you want to add additional features or fix a bug on a targeted portion of the project. Its purpose is for better performance, security, flexibility, and overall version control.

This assures stable codes and works up an ideal project workflow before merging everything into the master branches.

Git Repository

This serves as a folder containing all your files and a history of changes to the codes.

The repository may be public or private, depending on the access you give to certain members of your team of developers.

Initializing a repository will create a .git/ directory, where your project files are tracked and where object, refs, and other information are stored.

Make sure not to delete the git branch repository, or else all your progress will be erased.

Local and Remote Git Branch

The local branch is a branch that exists on the local machine. The local user will be the only person to view it.

remote branch is a branch that can be tracked from a remote location.

How to Rename a Local and Remote Branch

Before we make any changes to the branches, here are a few pre-requisites:

  • An existing installation of Git on CentOS or Git for Ubuntu
  • A Linux-based operating system
  • Access to the command line or terminal window (Ctrl+Alt+T or Ctrl+Alt+F2)

Rename a Local Branch

Select the branch you want to rename:

git checkout old-name

To see your local branch list, use the command:

git branch --list

Add an -m option to your rename branch command:

git branch -m new-name

Another option is to use either of these two commands:

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

To verify that your local and remote branches have been newly renamed from its old branch name, use this command to see the list:

git branch -a

Rename a Remote Branch

You will first need to rename a local branch by using the same steps above:

git branch -m new-name

git checkout master

git branch -m old-name new-name

The next step you need to do is delete the old branch and push the new one using either of these commands:

git push origin --delete old-name

git push origin :old-name new-name

Lastly, reset the upstream branch for your new local branch:

git push origin -u new-name

How to Delete Branch Git

You might have other commands you wish to perform on a branch git. Delete a branch that is local by using the following commands:

git branch -d branch-name

The -d (-delete) option is used to remove a local branch already pushed and merged with the remote git branch.

git branch -D branch name

The -D (-delete – force) option is used to remove a local branch whether or not it is pushed ad merged with the remote git branch.

To remove a remote branch, you can specify both local and remote branch names with the following commands:

git push remote_name --delete branch_name

git push remote_name :branch_name

Conclusion

To summarize, rename the branch with the following commands above. Verify that the branch already contains the new name. And lastly, push the new branch and reset the upstream, so everything is updated and good to go!

To rename a local branch and a remote branch is easy if you are keen to follow simple instructions carefully.

By copy-pasting the correct command, your branches are well on their way to being organized. This gives you that peace of mind you and your team can better navigate around your project.

Was this helpful? For any questions and concerns, leave us a message in the comments section below!