How to Rename a Local and Remote Git Branch?
Using Git, developers can keep track of their software packages as they progress through the development process. In Git, there is a branch that organizes a central repository of codes. When you mistakenly name a branch and now want to change it, you can do it effortlessly. A wrongly named branch can complicate the tracking of software, so it would be best to change it. It doesn’t matter if it is a local or remote branch.
In this blog, we are going to show you how to easily rename a Git branch using a few simple commands. The commands are here for local and remote Git branches alike. Here are the things you should need if you want to rename the local and remote branches in Git repositories:
- Your operating system should be Linux-based
- You should be able to access the terminal or command line window
- There should be Git installed on your CentsOS or Ubuntu
If your system meets these requirements, you can move on to the next sections where we have demonstrated how to rename the local and remote branches in Git
Renaming a Local Git Branch-
You have to select the branch first to rename it. Select the branch first that you want to rename with this command:
git checkout old-name
In case you want to view all the local branches in Git, use this command instead:
git branch –list
The steps are as follows:
Step 1: You will have to put the name of the branch in the command-line tool that you want to change: git checkout <em>old-branch</em>
Step 2: An output will appear that will tell you that the branch exists.
Step 3: Now you can rename the branch using git branch –m <em>new-name</em>
Step 4: You can also use a single command instead if you are not in the master: git checkout master
Step 5: This command will change the branch name: git branch –m <em>old-name</em> <em>new-name</em>
Step 6: A verification will tell you whether or not the rename was successful if you use this command: git branch –a
If the rename was successful, the output will confirm so with a “*new-name” message. But these steps are for renaming the local Git branch. How do you rename a remote branch then?
Renaming a Remote Git Branch
You cannot directly rename a remote branch if you are accessing a remote Git repository. But you will have to delete the existing branch title in your local repository. And then you can push a branch with a new name to the remote repository. You should verify if your local branch holds the correct name, using this command:
git branch -a
Now you can delete the old branch with the old name on the remote git repository: git push origin ––delete <em>old-name</em>.
When the branch is deleted, the output will tell you –[deleted] old branch. Now you can push the new branch with the right name to the past repository and reset the remote branch name as well. To do so, use this command: git push origin –u <em>new-name</em>.
If you want, you can also rewrite the remote branch with a single command that is: git push origin :<em>old-name</em> <em>new-name</em>. You can reset the upstream branch using git push origin –u <em>new-name</em>.
Whenever you see the old-name command in a line, you should replace it with an actual name that is existing and you want to modify. The new-name command will contain the new name of the branch that you will use from now on.
This is how you rename a local and remote git branch. But if you are still not sure what Git branches are, read on…
What is a Git Branch?
In Git, all the members of a group can access the entire version of the software project. This distributed version control system (DVCS) makes the security, versatility, and performance of the software packages better when it is about managing those packages. In the development stage of the software, branches work as separate lines. The branches work together with the master branch but do not contain any haphazard code that is not easy to understand or completely available. With branches, you can clean up the cluttered history prior to merging them mutually.
Git branches enable you to list, create, and remove other branches. It also serves as a record of the changes you have made to the other branches or that you intend to make to the project files. In addition to adding new features to the software project, you can use Git branches to fix defects in the software packages. Therefore, the Git branch is useful not just for summarizing changes from other branches, but for assuring that the right and available code is being merged into the project files. After the code in a git branch has been updated, it is ready for merging with the master branch. In addition to learning about git branches, you might want to study git repositories.
What is a Repository in Git?
Git repositories are folders that contain all of your software project files. These repositories also store the revision history of these files, and they can be either public or private. You can share a repository with your colleagues or friends that are also working at the same organization like yours.
When you start a Git repository, it will make a .git/ directory in your main project folder. This place will track the changes of the project files as well as keep their objects and references, and other data that help manage the repositories. So, if you delete the .git/ folder mistakenly or deliberately, it will delete all your project’s history.
That’s how the Git branches and Git repositories work. And as you have learned, you can easily rename the local and remote branches with a few simple commands. However, you can additionally clone a Git repository or clone the master branch from the remote repository. It asks you to use HTTPS or SSH. If you use HTTPS, the git clone https://github.com/user-name/your-repository-name.git command will work. In the case of SSH, use git clone ssh://github.com/user-name/your-repository-name.git. Use the cd your_apps command to go to the root folder of the cloned repository. And once done, confirm the cloning using the git status command.
Now you know how to manage Git branches with different commands and clone one as well. If you need any additional help, ask us in the comment box below. See you again in our next article.