Table of contents
1.
Introduction
2.
git branch & git checkout 
3.
git merge 
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Git branch commands

Author GAZAL ARORA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Git is a global version-control system for managing changes in any set of files. Git commands play an important role in a developer's life. It was originally created to help programmers collaborate on source code during software creation.

Today, we will learn about the basics and working implementation of the following commands: 

  1. git branch
  2. git checkout
  3. git merge

git branch & git checkout 

In this section, we will discuss git branch and git checkout commands. 

So how does the git branch help developers?

Imagine a situation where you and your fellow team members are working on a project. Now, you are in a position where your source code runs perfectly fine and passes all the test cases. The client comes to you and demands a new feature. You certainly don't want to mess with the code running perfectly fine until now, so you create a copy of the working directory and assign it to one of the developers in your team who will work on that feature that the client has demanded. 

Git makes it super easy to achieve this using the git branch and the git checkout command. Let us see through a working example. 

1. Create a new directory.

mkdir exploring-git-branch-commands


2. Enter into the directory.

cd exploring-git-branch-commands

 

3. Initialize the repository as a git repository so we can use the git commands.

git init

 


 

4. Create a README file

touch README.MD

 

5. Edit the README file (add some content). 

vim README.MD

 

Enter into insert mode by pressing i, add some text, and exit from the insert mode by pressing Esc. Save the file and after that return to the command line by pressing:wq. (w = write, q = quit). 

6. Check if the content is added.

cat README.MD

 

7. Check the current status of the repo.

git status 

 

When starting a project, we are by default on the master branch. We don't have any commits, but we have one untracked file, "README.MD." So we need to add it to the staging area and then commit it. 

8. add and commit 

git add README.MD
git commit -m "added readme"

 

-m option enables developers to add a commit message.

9. Check the new commit. 

10. Check all the branches associated with the repo. 

git branch

git branch command list all the branches. * denotes that the current branch is master. In simple terms, a branch is some version of your project.  

11. Let us create a new branch to add a new feature to our project. 

git checkout -b <branch_name>

 

The git checkout command is generally used to switch between different branches. The -b flag creates a new branch if it already does not exist. We can use the git branch command to list all the branches. 

12. The new branch's contents(files/folders) are the same as the branch from which it is created. In our case, the contents of the feature/new-feature branch initially are the same as the master branch. One can confirm this by using the ls command. 

ls

You can use cat README.MDcommand to see the contents of the README file. 

13. Let us add a new feature. Create a new file, edit that file and save (we already saw in the previous section when we created, edited, and saved the README file). 

14. Stage the new-feature file and commit the changes. 

15. Review the current version (feature/new-feature) of your project. 

ls

 

This version(feature/new-feature) of our project has two files instead of one (master version).

16. Check the logs for commit history.

git log

 

We have two commits, the new-feature added commit, which is evident as we just made it. The other commit added readme was made when we were working on the master branch, but we could see this commit since we checked out the feature/new-feature branch from the master

17. We can switch back and forth between branches. Let us go back to the master branch and review its content. 

git checkout master
ls

 

Notice that this version of the project only has the README.MD file. 

Therefore, we can say that git branch and git checkout commands help us create a snapshot of the source code (which is passing all the tests), and we can go back to that anytime we want. We can create a new branch for adding new features or experimenting, and we don't have to worry about breaking the earlier code.

git merge 

When working in a team, you and your fellow developers may likely be working on different features on different branches. Once you and your team are done building the product, it's time to combine all the code into a single codebase and deploy it onto the server. That's when git merge comes into the picture. 

We will be seeing a working implementation of the git merge command. 

  1. It's time to merge the new feature one of our developers has worked on. Switch to the destination branch (the branch you want to merge into) using. 
git checkout <branch_name>

and then merge the new feature branch using:

git merge <source_branch>

 

2. See the merge commit using:

git log

 

What is happening here is that the feature/new-feature branch is merged into the master branch. 

The following illustration provides more clarity. 

 

 

Check out most important Git Interview Questions here.

Frequently Asked Questions

1. Is it possible to create a new branch from another branch in git?
Yes, creating a new branch from an existing branch in git is possible. They are two methods: 

  • git branch <new_branch_name> will create a new branch from the branch you are currently working on. 
  • git checkout -b <new_branch_name>, this also works similarly.
     

2. What is the best way to see which branch is active?
You can use the command git branch to list all the branches, the one with an asterisk (*) is the active branch. 

Conclusion

In this blog, we learned about git branch commands. We learned about git branch, checkout, and merge commands. 
We also learned to create a new branch, switch between branches, and merge a branch into another with a working implementation of each.
We also learned about other git commands like git log, git add, git commit, etc.

 

Happy Coding!.

You can use Coding Ninjas Studio to practice various kinds of DSA questions asked in the interviews. It will help you in mastering effective coding techniques, and you will also get interview experiences with people working in big companies.

Live masterclass