How to create, rename and delete Git branches

Andrei Maksimov

Andrei Maksimov

0
(0)

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.

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
1. How to create, rename and delete Git branches - Initializing Git repository

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"
2. How to create, rename and delete Git branches - Configuring Git user and email

Now you can create an empty README file and make initial commit:

touch README.md
git add .
git commit -m "Initial commit"
3. How to create, rename and delete Git branches - 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
4. How to create, rename and delete Git branches - Viewing local Git branches

Viewing remote Git branches

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

git branch -r
5. How to create, rename and delete Git branches - Viewing remote Git branches

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
6. How to create, rename and delete Git branches - Viewing all Git branches

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
8. How to create, rename and delete Git branches - Create Git branches

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.

9. How to create, rename and delete Git branches - Commit pointer

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

10. How to create, rename and delete Git branches - Code branch tree - Example 1

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:

11. How to create, rename and delete Git branches - Code branch tree - Example 2

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
12. How to create, rename and delete Git branches - Selecting Git branch

You can combine both commands to create and select Git branch using single command too:

git checkout -b branch-3
13. How to create, rename and delete Git branches - Creating and selecting Git branch

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
14. How to create, rename and delete Git branches - Renaming Git branch

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
15. How to create, rename and delete Git branches - Deleting local Git branch

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.

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Top rated Udemy Courses to improve you career

Subscribe to our updates

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Want to be an author of another post?

We’re looking for skilled technical authors for our blog!

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use https://codeshare.io/ or a similar tool as Facebook comments are breaking code formatting.