Git Branch and Git Checkout
A branch in Git is a pointer to one of the commits. Branching is an essential feature of version control and Git provides a robust and lightweight branching model. We will be frequently creating new branches and merging them with the existing ones or moving between several existing branches. Git Branch and Git Checkout are two commands that provide us with these functionalities.
What is a Branch?
- A branch in Git is just a pointer to one of the commits. It is like a reference to one of the commit points in an existing series of commits.
Branching helps us to create something on top of a commit without worrying about messing up the existing series of commits or snapshots. It provides developers an independent environment to work in and experiment with.
These new branches can also be merged with the existing branches.
Consider the following scenario to better understand the concept of branching.
Suppose there is a local master branch with 3 commits.
Now assume that a developer wants to add a new feature to the project but does not want to corrupt the local master branch. He can create a new branch and start working in an independent environment.
The developer adds three new commits to this new branch.
Now consider that this developer wants to add another feature but that feature must be built on top of commit point B. He can create a new branch from that commit and can start working independently on that branch. The developer adds two new commits to his branch as well.
These new branches can then be merged with the existing master branch.
Git Branch Command
Git provides us with the Git Branch command that lets us create new branches, view and rename the existing branches and delete unwanted branches.
To create a new branch at the HEAD(last commit of the current branch you are working on):
$ git branch <branch-name>
To create a branch at some other commit point except the HEAD, we can pass the hash of that commit.
$ git branch <branch-name> <commit-hash>
To view the existing branches:
$ git branch
We can use the -a flag to view the local and remote branches.
$ git branch -a
To rename the branch we are currently working on, we can use the -m flag.
$ git branch -m <new-branch-name>
To delete a branch we can use the -d flag.
$ git branch -d <branch-name>
But this will not be able to delete unmerged branches as Git does not want us to lose the changes on that branch. To delete these unmerged branches we can use the -D flag instead.
$ git branch -D <branch-name>
What is Checking Out?
Just creating a new branch is not enough. We must be able to move between the branches to start working on them and add commits to these branches. This is where Git Checkout comes into action.
In Git terminology, checkout means the act of switching between different entities. These entities can be files, commits, or branches.
This command is also used for restoring previous versions of a specific file but in this tutorial, we will be focusing on only the branching aspect of this command.
Git performs this checking out by changing the HEAD to point to the commit of the branch that we want to navigate to.
Consider the following scenario to understand the Git Checkout better.
Let's say that we have a master branch with three commits. The HEAD will be pointing to the most recent commit of this local master branch.
Now let's create a new branch called new_branch by using the Git Branch command. Now the new_branch will reference the commit point C but the HEAD still points to the local master indicating that the local master is our current working branch.
If we run the Git Checkout command to move to the new_branch then the HEAD will point to the new_branch.
Consider that we add two commits to this new branch. The HEAD will now point to the most recent commit.
Now if we again run the Git Checkout command to move to the local master, the HEAD will go back to the most recent commit of that branch.
Git Checkout Command
There are many flags and options we can use with the Git Checkout command. Let's understand how to use them.
To simply move to a different branch, we can just specify the branch name.
$ git checkout <branch-name>
Instead of creating a new branch using Git Branch and then move to that branch by using the Git Checkout command, we can directly create a new branch and move to that branch by using the -b flag with Git Checkout.
$ git checkout -b <new-branch-name>
By default Git Checkout with the -b flag will create a new branch from the HEAD of our current working branch. If we want to create a new branch from the commit point of some other existing branch we can pass the name of that branch.
$ git checkout -b <new-branch-name> <existing-branch-name>
We can also switch to remote branches by first fetching them and then running the git checkout command.
Branching is a very important and useful function of Git. It gives developers the freedom to try out new things without worrying about ruining previous progress. Git Branch is the function that lets us create, rename, view, and delete branches. We can use this command to start a new branch from any of the previous commit points. Git Checkout is another useful command for branching. It helps us to navigate between branches and allows us to create new branches and directly jump to them without using the Git Branch command.