Git is probably the most widely used version control system (VCS) for source code management and every cloud engineer has to know how to use it. One of Git’s killer features is code branching support. The branch is a pointer to one a more commits, which allows you effectively implement new changes without breaking already existing code. This article covers how to create a new branch, rename and delete branches using Git CLI commands.
Table of contents
Initializing Git repository
To illustrate our examples, I’ll create and initialize a new Git repository in my Cloud9 IDE.
To initializes a git repository and creates the initial .git directory in a new or already existing project, use:
mkdir git-demo-repository
cd git-demo-repository
git init

If it is your first repository, you have to configure your name and email. Git uses adds this information to a commit when you’re saving (committing) your changes.
git config --global user.email "Enter your address"
git config --global user.name "Enter your Username"

Now you can create an empty README file and make initial commit:
touch README.md
git add .
git commit -m "Initial commit"

Now, we’re ready to play with the Git branches.
Viewing Git branches
In this section we’ll cover how to view local and remote Git branches.
Viewing local Git branches
To view local Git branches, use the following command:
git branch

Viewing remote Git branches
To show remote Git branches, use -r
(remote) argument in addition to the previous command:
git branch -r

Note: if you’ve never pushed your code to remote repository, this command will provide an empty output.
To display all local and remote Git branches together, use the -a
(all) argument:
git branch -a

Your current branch is marked with an asterisk (*
).
Creating a new Git branch
Currently, we have only one branch. Let’s create more Git branches by using the command:
git branch branch-1
git branch branch-2

Note: It is very important to keep in mind which commit/branch you’re using before executing those commands. Right now, we created two branches pointing to the same commit from the master branch.

As soon as you start committing changes to those branches, your code changes tree will look something like this:

But if you commit changes to the branch-1
branch first and then execute git branch
command, you’ll create a completely different code branch tree:

Selecting Git branches
The newly created branch is not selected by default; you need to do it manually using git checkout
command:
git checkout branch-1

You can combine both commands to create and select Git branch using single command too:
git checkout -b branch-3

Renaming Git branches
Sometimes it is required to rename Git branch, for example if you have a typo in the branch name of willing to associate branch with another Jira ticket.
Renaming local Git branch
To rename local Git branch, use -m
or --move
argument:
git branch -m branch-1 branch1

Renaming remote Git branch
There’s no option to rename the remote Git branch using a single command.
First, you have to rename the branch locally:
git branch -m branch-1 branch1
Then, you need to push it to the remote repository:
git push origin branch1
And finally, you can to delete an old remote branch:
git push origin --delete branch-1
Deleting branches
When you’ve finished your feature development, and merged your changes to the master
branch, you might safely remove not needed branch:
Note: you cannot delete the branch in which you are currently working on.
Deleting local Git branch
Let’s delete branch-3, for example:
git branch -d branch-3

Deleting remote Git branch
You can also delete remote Git branch if you need to by executing the following command:
git push origin --delete branch-2
Summary
Git is probably the most widely used version control system (VCS) for source code management and every cloud engineer has to know how to use it. One of Git’s killer features is code branching support, which allows you effectively implement new changes without breaking already existing code. This article covers how to create a new branch, rename and delete local and remote branches using Git CLI commands.
Related articles
- How To Remove Git Remote Repository
- How To Rename Files And Directories In Linux
- Easy way to connect to multiple AWS CodeCommit repositories
- Terraform – Deploy Python Lambda (container image)
How useful was this post?
Click on a star to rate it!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
I’m a passionate Cloud Infrastructure Architect with more than 15 years of experience in IT.
Any of my posts represent my personal experience and opinion about the topic.