In the last tutorial, we discussed what are branches in Git. It gave us a general overview of the concepts of branches. Git Create Branch can be created through a remote repository directly or through Git on our local system. Although in the real-world scenarios, creating branches through GitHub directly is not the primary choice for creating the branches. The foremost choice of the developers is creating the branch locally through the console on the local machine and pushing those changes to the remote repository on GitHub. In this tutorial, we will learn about:
- How to display all branches in Git?
- How to Create a new branch in Git?
- How to Switch branch in Git?
- How to Create & Switch in a single command in Git?
How to display all branches in Git?
Before we go-ahead to create the branches on our local system, we need to learn how to view all the branches through Git. Viewing the branches helps us keep a check on the synchronization between the remote and the local repositories. We can view all the branches by a simple command:
Execute the following command in Git to view the branches:
Once you clone the repository to your local machine, all the branches will be visible through this command.
Executing the git branch without any flag shows only those branches that are available locally. Since we just have the master branch, only the master branch is shown in the output. To check all the branches (including remote branches), type the following commands:
git branch -a
Executing the command lists all the branches as shown below:
In the highlighted red color, one more branch can be seen which is the “dev” branch. As we create more branches in the next section, those will be reflected here. We will keep checking from time to time to show the user all the available branches.
Git Create Branch On A Local Machine
Creating branches in Git is a simple step-by-step process. But, before starting, ensure you have a clean working directory (nothing to commit) by running the git status command.
Open Git Bash and navigate to the local working repository.
Type the following command to view all of your branches.
The local branches are the same that are shown in the previous section i.e. “dev“.
Let’s create a new branch now in our local working git repository. Type the following command to create a new branch named “prod” (short for production).
git branch <branch_name>
This is the initial and simpler way to create a branch in Git. We will see an alternate way later in this tutorial.
Now check the branches on the local system by typing the git branch command again:
Our branch is now created in the local working directory. As we might assume, this must also reflect on our GitHub account with a new branch added in the list of the branches.
Let’s check that out.
I navigate to my account and check the number of branches in the “Branch” dropdown.
Damn! I still have the previously created branches and the new branch that I created locally (prod) is yet not synced here.
By this, I assume that creating a new branch is a change made to the repository like any other change, so I need to push our changes to the remote repository through git push command. So, let’s try the same thing here and see if it works or not.
Use the following command to update the branch on the GitHub repository.
git push -u origin prod
Note: The u flag adds the tracking reference for the prod branch so that your upcoming pushes do not require any extra arguments and the local branch will be linked to the remote branch.
Press enter and execute the command.
This message shows that our branch is all set up and tracked. Let’s confirm it once again on our GitHub account.
The Branch dropdown shows that the new branch has been added and synced in GitHub remote repository successfully.
How to Switch branch in Git?
Since we are now dealing with multiple branches, it is important to know how to switch between these branches and work on each of them separately. Switching the branches is a very frequent operation so it stands quite important for us.
In the following steps, we will try to switch the branch from master to prod.
Check the branch you are currently on which is visible alongside directory name.
Switch to the “prod” by executing the git checkout prod command.
And here you go. We have successfully switched the branch from master to prod. Now, whatever operations you do, they will be logged inside the prod branch since now you have separated yourself from the master. More often while working in Git, we create a branch and start working in it from that point in time. This would normally require two commands:
- git branch to create the branch.
- git checkout command to switch to the branch.
Executing both of these branches takes some time and fortunately, Git has a solution for it. Let’s see how.
How to Create & Switch in a single command in Git?
Git provides us with an option to create a branch and switch to that branch by executing a single command. Since it is frequently required that we create a branch and switch to it at the same time, this command helps a lot to achieve the same. Execute this command and check the output:
git checkout -b <name_of_branch>
Execute this command and you will be automatically switched to the newly created branch.
Note: Git Branch command does not switch to the newly created branch automatically as is done in git checkout command.
Once we are done with these operations, we have a newly created branch to start work upon. It is obvious that if you know the creation, you should also now the deletion for all the opposite purposes you required in the creation of branches. In the next tutorial, we will talk about the deletion of branches and why do we need to delete a branch. As a practice, you can practice creating the branches and committing a few files and check the log for each branch you create.