August 21, 2020
4min Read
Edward S.
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.
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:
git branch -m new-name
git checkout master git branch -m old-name new-name
git branch -a
Although it isn’t possible to rename a remote branch directly, the process of renaming one involves these three easy steps:
git push origin --delete old-name git push origin :old-name new-name
git push origin -u new-name
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.
cd repository-name
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
git checkout new-branch-name
git status
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.
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
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
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.
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.
To clone a repository, we will use the Git clone command. Additionally, you will also need to specify the URL of the repository:
git clone https://github.com/user-name/your-repository-name.git
For SSH:
git clone ssh://github.com/user-name/your-repository-name.git
cd your_apps
git status
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.
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.
September 15 2019
Thank you for taking the time to write such a clear tutorial!
September 23 2019
Nevermind
Leave a reply