Last updated Feb 26, 2021

Understanding Git Branching Step by Step Guide

In this tutorial, you’ll learn how to use GIT branches in real-world project step by step.

Understanding Git Branching Step by Step Guide

In this tutorial, you’ll learn how to use  GIT branches in real-world projects step by step.

What are GIT Branches?

GIT branches are a very useful feature provided by git, which lets you work with multiple developers within the same project.
-

Branches become more useful when you want to work on multiple features at the same time. You wouldn’t want to end up with a master branch which has one feature half-done and second feature also half-done.

By using branches you can separate the code base into two branches and work on and commit to them separately. As soon as one was ready, you might merge this branch back into the master branch and push it to the remote server.

How to use Git Branching Step by Step Guide

Step1: Create and Checkout to Branch

When developers are working on a feature they’ll often create a copy of their code where they can make separate commits to. Then when they’re done they can merge this branch back into their main master branch.

In the previous git article, we have created a Demo repository and it contains only the master branch by default.

Also read, How to Use Git and Github a Complete Beginners Guide

Image

now let’s create another branch called ‘test_branch‘, where we  will do all other work.

git branch test_branch

Great! Now if you type git branch you’ll see two local branches: the main branch named master and your new branch named test_branch.

You can switch branches using the git checkout <branch> command:

git checkout test_branch

You can also use shorten method to create branch and checkout to create a branch at once using the below command:

git checkout -b test_branch

now if you type git branch you’ll see two local branches with selected test_branch.

now you are in test_branch, we will create sample_file.txt here.

by typing the git status command you’ll see sample_file.txt as an untracked file.

Step 2: Commit changes to Git

Add the file you just created to the files you would like to commit the change.

git add demo.txt

Let’s commit those changes to the repository

git commit -m "Add sample_file.txt"

Push your files to remote repository by typing git push command

git push origin test_branch

Now we have pushed test_branch changes to the remote repository, go to your GitHub account and you’ll be able to see your new branch with your committed changes.

Image

Step 3: Switching Back to master

Great, you’re almost finished, you just need to switch back to the master branch so you can merge your changes from the test_branchback into the master branch.

Go ahead and check out the master branch:

git checkout master

Step 4: Preparing to Merge

Alrighty, now we  have to merge  changes from the test_branch into the master branch.We’re already on the master branch, so we just need to tell Git to merge the test_branch branch into it:

git merge test_branch

Step 5: Delete the Branch

Congratulations! You just accomplished your first successful merge. Since you’re done with the test_branch  you don’t need it anymore.You can use git branch -d <branch name> to delete a branch. Go ahead and delete the test_branch now:

git branch -d clean_up

Great, now you have done creating a new branch and merging it with a remote master branch, Take a look at your GitHub profile and you’ll see your changes to the master branch.

Here’s what you accomplished in this tutorial:

  • What are GIT  branches?
  • Uses of GIT Branches
  • Creating a new branch
  • Committing to the new branch
  • Make Push and Merge branch with remote Master branch

Conclusion

Thanks for reading.

Do let me know If you face any difficulties please feel free to comment below we love to help you. if you have any feedback suggestions then please inform us by commenting.

Don’t forget to share this tutorial with your friends on facebook and twitter.

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

0 comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

No comments yet — be the first to start the discussion.

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.