Basics of Branching and Workflow on Git

Matthew Aquino
4 min readFeb 16, 2021

So you have created a local repository, connected it to a remote repo and are ready to work on a project with your partner or team. In the following article we are going to discuss 3 key git commands: git branch, git checkout, and git merge and how to integrate their use into your daily workflow.

Git Branch

When working on a codebase most version control systems will provide a branching feature that allow you to fix a bug or work on a new feature. Git branches are a reference point for changes within your codebase. It is good practice to create a new branch no matter how big or small the change may be.

Example of 2 features branching off the main master branch.

Branching allows for 2 (or more) separate lines of development to work parallel to each other without ‘breaking’ the codebase.

Common commands:

git branch #lists all of the branches in your repository
git branch <branch-name> #creates a branch named <branch-name>
git branch -d <branch> #delete the LOCAL copy of the branch.
git branch -m <branch> #renames the current branch to <branch>
#assuming you have connected your local repo to a remote one
git push origin --delete <branch> #deletes the remote copy

You can now create branches but how do you switch over to another branch?

git checkout <branch> #switches over to the indicated branch

Git Checkout

The git checkout command allows you to seamlessly switch between branches, think of it as a way to update the files in your working directory to the files of the version you would like to work on. A common command to create and switch onto a new branch is —

git checkout -b <new branch>

This neat little line will create the new branch as well as switch you over onto this new branch. Note that this branch is local and will still need to be pushed up to the remote repo if you would like your team to collaborate on it.

git fetch --all #will 'fetch' all branches stored in the remote repo

Once you have fetched the various branches you can perform the normal git checkout command to switch into that directory.

Git Merge

Once you have completed a feature or worked out any bugs in your code, you’re going to want to merge it back into the master branch. The basic idea of a merge is to combine various sets of commits into one unified history.

In the above example, a feature was forked off to a new branch at the Common Base and merged back to the master branch at the New Merge Commit.

Prepare to merge:

  1. Ensure that you are within the the receiving branch by using git checkout, in most cases it will be the master branch that is receiving the new changes.
  2. Make sure the receiving branch and merging branch are up to date with remote changes by performing a git fetch. To ensure that the master branch is up to date perform a git pull.
  3. Initialize the merge of the master and the feature branch:
git merge <feature-branch>

There are 2 main types of merges a fast-forward merge and a 3-way merge. A fast-forward merge occurs in a linear path where the commits to the feature branch are directly added to that of the master branch, as seen below.

This type of merge is only possible IF the branches have not diverged. In the event that a merge is not linear, Git will attempt a 3-way merge where it uses 3 commit targets to generate the merge commit. The two branch tips, one being for the master and the other for the feature branch, and their common ancestor.

Example of a 3-way merge

A common merge strategy is to use fast-forward merges for small bug fixes but for larger, longer-running features the approach is generally to use a 3-way merge.

In the above example while the feature has been in development the master branch has progressed by one step. In order to merge the 2 branches now Git performs a 3-way merge. Git attempts to resolve all conflicts but in the event that the same part of a file was updated on both branches, user input will generally be required to resolve merge conflicts. In this example, merge conflicts can be resolved manually on VSCode.

Summary:

  1. Use git branch to check the branches within your local repository
  2. To create and switch to another branch use git checkout -b <new-branch>
  3. Ensure that you are on the correct receiving branch before performing a git merge <feature-branch>.
  4. When merging be aware of the branch you’re on as well as the type of merge you are attempting. User input maybe required for merge conflicts.
  5. It is good practice to create new branches when working on any type of feature — big or small.

--

--

Matthew Aquino

Navigating through software engineering one blog post at a time.