Branching in Version Control

Branches are different paths in version history. They make working in the same repository as a team easier as the work can be separated into different branches when necessary.

Branches in Git are references to a certain commit. A new branch can be made from any commit, they can be moved from commit to commit and merged.

Different ways to use branches

Working in the main line is the most straightforward way to work with Git. There no other branches besides the main is used. Changes are designed and implemented in small increment and code must be commited regularly and daily. The history of version control created by commits must be considered unchanged. Learning version control is usually started with this approach.

Branching according to features makes it possible to maintain the projects official, working version under the main branch. The workflow is the following: coding is started in the main branch. A new branch is created for the new feature that is updates (add, commit, push). Each push adds the changes into the remote repository into the defined branch. Others can review and comment on the feature under development before the code is accepted as a part of the main branch. This makes using pull requests in accepting code into the main branch possible. The benefit of pull requests instead of just merging code directly into the main branch comes from the possibility to not just isolate feature development from the main into brancehs but also to enable discussion between team members. This way version control can continuously monitor core quality during implementation.

Using branches

First we’ll go over how:

  • Branches are made.

  • Branches can be inspected.

  • You can move between branches.

  • Branches can be deleted.

  • Branches behave with remote repositories.

Creating branches

We have two different ways of creating branches. Commands git branch and git checkout.

git branch <branch name>

Creates a new branch to the commit that HEAD currently points to.

git checkout -b <branch name>

Creates a new branch to the commit that HEAD currently points to. Makes HEAD point to the new branch.

Listing branches

Existing branches can be inspected with following commands

git branch

Lists all local branches. Current branch is highlighted with a star (*).

git branch -a

Lists all branches in local and remote. Current branch is highlighted with a star (*).

Removing branches

Removing branches from local repository

git branch -d <branch name>

Removes named branch from local repository


Removing a local branch doesn’t automatically remove it from remote repository when doing a push

Moving between branches

In basics we used checkout to recover old file versions. Checkout can also be used to move HEAD.

git checkout <branch name>

Moves HEAD to named branch

git checkout <commit>

Moves HEAD to a commit which results in a detached HEAD

We might want to move to a commit when creating new branch or if we want to test another version of the program.

Comparing branches

We can compare differences between branches and commits by using command git diff. By default diff shows the difference between worktree and index. You can also choose to compare one or more branches.

git diff HEAD <commit/branch>

Compares HEAD with the commit or branch

git diff <commit/branch1> <commit/branch2>

Compares two commits or branches

git diff HEAD <commit/branch> \-\- <path>

Compares HEAD to a commit or branch but only in the specified path

Moving a branch

You can move branch to another commit by using command git reset.

git reset <commit>

Moves HEAD to point to the specified commit

git reset HEAD~<integer>

Moves HEAD backwards in history by the specified amount of commits

git reset \-\-hard <commit>

Moves HEAD to point to the specified commit. Resets worktree to match the commit.

Instead of doing a hard reset one should do a soft reset. In this case you can create a new commit of your current worktree. You can still proceed with the hard reset after manually checking out the worktree.