Skip to content

Git Branch

Software engineers need to isolate their code spaces during the development. Isolating development activities on the project avoids messy code conflicts. Git offers branching to achieve code space isolation for each collaborator in the project.

In this code note, we will explore following branching concepts:

  • What is a Branch
  • Creating Branches
  • Switching Branches
  • Modifying Branches
  • Merging Branches
  • Merge Conflicts



› What is a Branch

You can think of a git branch as a separate internal copy of the entire project made at a specific time.   Testers use branches to isolate their automation development activities in the project to prevent messing up existing stable codes.

When you turn your project into a git repository, default master or main branch will be created, which is a stable version of the project.   From this branch, everybody creates their own branches to isolate their development and later on merge their development back into the master branch.

In test automation activities, we will use the following three branches:

The branch name Its purpose
master or main contains stable and peer reviewed test automation code.
develop temp branch used for merging each collaborator's code changes into main branch
sprint contains each collaborator's isolated code base, made from master branch

Also known as the master branch, every git project starts with this default branch.   This branch is meant to be the version of your project that contains the most stable code base.   For example, our test automation execution scripts are pulled from this branch every time the automation test is executed on a dedicated test server.

Every git repository should have this branch among all the collaborators.   In addition, this branch serves as a merging branch for all the individual branches of collaborators.   Once the code reviews and merging conclude, this branch will merge into the main, incrementing the project's stable code bases.

Every collaborator creates this branch from the main branch for each sprint coding activity.   This allows them to isolate their new code development to avoid messy code conflicts among collaborators.   Once satisfied with their progress in this branch, they can dump their changes from their sprint branch into the develop branch, where the code review and merging occur.



› Creating Branches

Now that we understood the concept of git branches, let's take a look at git commands that we will use when we work with branches.

Displaying available branches

You can display the branch you are currently working on.

To see what branch you're currently on :

git status

To see the list of branches your repository has on local :

git branch -l 

To see the list of branches your repository has on remote :

git branch -r

To see the list of branches your repository has on both local and remote :

git branch -a

Creating a new branch

You can create a branch based on the current branch you are working on.

Creating a branch titled sprint8nijat

git branch sprint8nijat



› Switching branch

You can switch between the branches. However, please keep in mind that switching to a different branch without saving or taking a snapshot of the current branch could cause an unforeseen error. Always make sure to save or commit your changes before switching to another branch.

Switching to develop branch:

git checkout develop
Note that the checkout command tells git to which branch you want your changes applied. So if you had an unsaved change on one branch and you switched to a different branch. Then that unsaved change will be part of the new branch you just switched to.

Switching to develop without worrying about saving changes :

git switch develop



› Modifying Branches

You can easily rename a Git branch at any time. However, please be cautious with renaming your branch since it sometimes involves deleting existing branches ( remote ) depending on the renames you want to do.

To rename a Git branch locally:

git branch -m newAwsomeBranchNameHere

This will rename the current branch you have, say develop, to newAwsomeBranchNameHere.

To delete a local branch ( Only DO THIS AFTER CODE MERGE )

git branch -d sprint1nijat

To delete a remote branch ( DO NOT DO THIS! )

git push origin --delete sprint1nijat

To rename of remote branch

1
2
3
git push origin -d sprint1nijat      # delete the remote branch 
git branch -m gozilla                # rename the local branch to the desired name
git push origin -u gozilla           # push the renamed branch to the remote 



› Merging Branches

In git we have two types of merging.

  • Fast-forward merging ( if branches have not diverged )
  • 3-Way merging ( if branches have diverged ) , have to make merge commit.

Fast Forward Merge: bugfix, master if you want to merge the changes in the bugfix into the master. And if master is still behind the bugfix and does not have any commits. Then when we merge these two branches, it will be fast-forward merge by default.

If you want to have 3-way commit when you merge and if its gonna be taken care of by the fast forward merge, you can specify this by doing:

git merge --no-ff bugifx   # create a merge commit.  

To list of branches that is merged to the current branch you are on:

git branch --merged

The list of branches that is not merged to the current branch you are on:

git branch --no-merged



› Merge Conflicts

Conflict happens when:

  • Same line of code has changed in different ways in two different branches
  • One file is changed in one branch but deleted on other branch
  • Exactly the same file has been added in two different branches, but the content is different
$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:   #<-- under this is the file that has conflict
  (use "git add <file>..." to mark resolution)
        both modified:   objectives.txt

no changes added to commit (use "git add" and/or "git commit -a")