In this article, I am going to talk about a very important feature of Git, Git branches. So, let’s get started.
Let’s say, you’re working on your project. All of a sudden, you have a great idea that you want to test out. But, you’re not sure whether it would work or not. Don’t fear, Git branch is here!
Well, you can create a new Git branch on your project, then start testing your ideas on the new branch. If you like it, you can merge it with the master branch. If you don’t like it, you can discard/remove it anytime. It won’t affect your original work.
Note that, the master branch is the default branch in Git. It is automatically created and used when you initialize a new Git repository.
In the next sections of this article below, I am going to show you how to create Git branches, use Git branches and remove Git branches. So, let’s move forward.
Preparing a Git Repository for Testing:
In this section, I will clone one of my GitHub repository to my computer. I will do different branching operation in this repository later. If you want, you can use your own Git repository as well.
If you want to use my GitHub repository for testing the things I’ve shown in this article, then run the following command to clone it.
Once the Github repository is cloned, navigate to the project directory as follows:
Listing Existing Git Branches:
You can list all the existing Git branches you have on your Git repository with the following command:
As you can see, I have only one branch master on my Git repository. As you create more branches, it will show up here. The active branch has an asterisk (*) in front of it. It will also have different color than the other branches. As you can see, the master branch has an asterisk (*) in front of it, so it is the currently active branch.
Creating Git Branches:
Now, let’s say you want to create a new Git branch (let’s call it new-features) for trying out your awesome ideas. To do that, run the following command:
A new branch new-features should be created from the HEAD (last commit) of the master branch.
Now, if you list all the existing Git branches on your Git repository, the new branch should be listed as you can see in the screenshot below.
Checkout to Git Branches:
Earlier, you’ve created a new Git branch new-features. But, it is not active as you can see in the screenshot below.
Activating a branch is called checkout in Git.
To checkout to the new branch new-features, run the following command:
Now, if you list all the branches, you should see the new-feature branch is active.
Creating and Checking Out to A New Branch:
If you don’t want to create a branch first and checkout to it later with two different commands, then Git has a solution for you as well. You can create and checkout to your newly created branch at the same time with a single command. To do that, you can use the -b option of git checkout.
We could’ve created the new-feature branch and checked out to it very easily in the earlier example with the following command:
Committing Changes to Git Branches:
Once you checkout to your new branch new-features, you can start adding new commits to this branch.
For example, I’ve changed a file package.json in my Git repository as you can see from the git status command:
Now, you can add new commits to the new branch new-features as you can see in the screenshot below.
$ git commit -m ‘fixed package versions in the package.json file’
As you can see, the new commit is added to the new-feature branch.
Now, if you checkout to the master branch, you won’t see the new commit. The new commit is only available in the new-features branch until you merge the two branches.
Creating New Branch from Another Branch or Commit:
If you want to create a new branch from another commit or from the HEAD (last commit) of another branch, you will have to specify it when you create the new branch.
If you don’t specify the source branch or commit from which to create the new branch, then the new branch will be created from the HEAD (last commit) of the branch you’re currently checked out as.
To create a new branch (let’s say test) from the HEAD (last commit) of another branch (let’s say, new-features), run the following command:
As you can see, both the test and new-features branch has the same commit history.
Let’s say, you want to create a new branch test2 from an existing commit from another branch master.
First, list all the commits with the following command:
As you can see, all the commits of the master branch are displayed. Notice that each of the commits has a unique hash. To create a new branch from an existing commit, you have to use the hash of your desired commit.
Now, let’s say, you want to use to the commit 45c336e as the HEAD (last commit) of the new branch test2. To do that, run the following command.
As you can see, the new branch test2 has the commits up to 45c336e.
So that’s how you create branches on Git. Thanks for reading this article.