Techscrypt

Working with GIT Branches

git

The previous lesson introduced essential git concepts like working with files, staging and commtting them. In this lesson, we will take a look at how to work with branches and use them effectively in the development workflow.

Introduction

Branches are a really useful feature of git because they allow us to build code that diverges from the main line of development.

Git commits are made to individual branches, so committing changes to one branch does not affect the commits on other branches. Git provides elegant ways to apply changes from one branch to another like merging and rebasing. This allows developers to build features separately and eventually merge them together once they are tested and ready to be released.

In a typical development workflow, git branches serve three primary use cases.

  1. Building on top of existing code without modifying the original code
  2. Testing new features before releasing them
  3. Team work and collaboration

Working with branches

Creating new branches

A new branch can be breated using the git branch <branch_name> command.

You can create and switch to a the newly created branch at the same time using the checkout command with the -b option git checkout -b <branch_name>

Create a branch

All the local branches in the repository can be listed using the git branch command. The output of this command should look something like this with the * indicating the current active branch.

  master
new-feature-branch
* test

Switching branches

We can switch between branches using the git checkout <branch_name> command.

Switch to a branch

Quick Tip - You can switch back to the previously checked out branch using git checkout -. This command is specially useful when you have to consecutively switch between the same two branches, for example the development branch and the master or main branch.

Switch between branches

Merging

Merging one branch into another is an essential workflow when working with branches. Merge adds the changes from the source branch to the target branch. The git merge <branch_name> command can be used to merge a branch into the currently active branch. For example, when the current branch is master, the changes from feature-branch can be added to master using git merge feature-branch

Merge branches

Note that the merge used "fast-forward". Which means that the commit on the feature branch was directly ahead of the commit on the master branch.

Checking diffs

When committing changes, it is useful to check the updates made to the code before commtting them. This can be done using the git diff command. This shows the exact changes to each file, which looks something like this, where the + indicates that the line is added and - indicates that a line has been deleted from the file.

Diff

Deleting branches

Branches can also be deleted once they are no longer required. For example, after the feature branch changes have been merged into master, the feature branch is no longer required. Branches can be deleted using the git branch -d <branch_name> command.

Delete

Summary

This article covered the basics of working with branches and how to use them in the git workflow. Working with branches also involves things like rebasing, resolving conflicts and working with remote branches. Stay tuned for upcoming articles that will explore some of those git concepts.


Subscribe to the Newsletter

We will only use your email address to send out the weekly newsletter with the latest updates from Techscrypt.

* indicates required