Branches, merges, and conflicts¶
Objectives
- Get some more hands-on working with branches
- creation
- switching
- merging
- deletion
- handling uncommitted changes
- stashing
- discarding
- checkout with merge
- merging and merge conflicts
- rebasing: combining a sequence of commits to a new base commit.
- cherry-picking
Branches allow developers to work independently on features, fixes, or experiments.
Why use branches¶
There are many uses for branches:
- Test different directions for a project
- Several projects members would like to work on their own copy of the code
- Bug fixes that are not yet tested, but will later be merged into the main version
What is a Git branch?¶
A branch represents a separate development path.
Repositories typically begin with a default branch called main or master.
Until now, we have worked with a repository that only have one branch, with the commits done one at a time:

In the above picture, the master (or main) branch points to a commit. The current position is HEAD. (Time goes rightwards)
What is a Git branch - basic concepts¶
Now we want to look at repositories with several branches:

Branches are used to create another line of development. They are “individual projects” within a git repository.
- The branch is the commit and all its parent commits, not just the one we are currently pointing at.
- The main line of development is usually called the “master” branch.
- Different branches within a repository can have
- completely different files and folders
- almost everything the same except for a few lines of code in a file
Usually, a branch is created to work on a new feature. Once the feature is completed, it is merged back with the master branch.

Basic Branch Commands¶
# List branches and see which branch you are on
git branch
# Create a branch
git branch cool-feature
# Switch to another branch named cool-feature
git switch cool-feature
# Create a branch and switch to it immediately
git switch -c cool-feature
# Another way to switch branch
git checkout cool-feature
# Another way to create a branch and switch to it immediately
git checkout -b fancy-idea
Typical Branch Workflow¶
# Create and enter a branch
git switch -c my-feature
# Make edits
echo "Feature content" > feature.txt
git add feature.txt
git commit -m "Add feature"
# Return to main
git switch main
# or
git checkout cool-feature
# Merge the branch
git merge my-feature
# Delete merged branch
git branch -d my-feature
Branches: merging, deletion¶
- When you have decided you are happy with the changes you made to the new branch, merge it back to the master branch (or another branch)
- Note: The branch is always merged to the current HEAD.
- First switch to the branch you are merging it to:
- Then merge them:
- You can now delete the reference to the extra branch, the commits stay:
Exercise/example - Type along!
- Create a directory. Initialize a repository
- Create a file, stage it, and commit it
$ mkdir my-project; cd my-project/
$ git init
Initialized empty Git repository in /home/bbrydsoe/my-project/.git/
$ touch file.txt
$ git add file.txt
$ git commit -m "Committing the first file"
[master (root-commit) 1006b51] Committing the first file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file.txt
- Create a new branch, then switch to that branch
- Make some changes - add files and text ( > overwrites or are suitable for new file)
- Stage the file and commit it
$ git branch cool-feature
$ git checkout cool-feature
Switched to branch 'cool-feature'
$ echo "This is a text" > file.txt
$ git add file.txt
$ git commit -m "Added text to the first file"
[cool-feature 5bad966] Added text to the first file
1 file changed, 1 insertion(+)
- Switch back to the master branch, make some changes
$ git checkout master
Switched to branch 'master'
$ echo "Text to the second file" > second-file.txt
$ git add second-file.txt
$ git commit -m "Added a second file"
[master bdec2cf] Added a second file
1 file changed, 1 insertion(+)
create mode 100644 second-file.txt
- We should check the effect of the changes. I will use this command:
- It can be useful to make this into an alias so we can just say
git graph:
This is on the master branch
$ git graph
* bdec2cf (HEAD -> master) Added a second file
| * 5bad966 (cool-feature) Added text to the first file
|/
* 1006b51 Committing the first file
We now merge the branches and check again
$ git merge cool-feature
Merge made by the 'recursive' strategy.
file.txt | 1 +
1 file changed, 1 insertion(+)
- Note that in recent git versions (>=2.33) the default “recursive” strategy is replaced by the “ort” strategy.
$ git graph
* cf3e6b7 (HEAD -> master) Merge branch 'cool-feature'
|\
| * 5bad966 (cool-feature) Added text to the first file
* | bdec2cf Added a second file
|/
* 1006b51 Committing the first file
Now we can delete the reference to the new branch we had created, since all the content is now in the master branch.
In a somewhat nicer format, it looks like this:
We commit stuff to both branches
(Time goes leftwards)
Optional/self study
Merge ‘cool-feature’ to ‘master’ (3-way merge)”
3-way merges use a dedicated commit for connecting the two merge histories. The name comes from the fact that Git uses three commits to generate the merge commit: the two branch tips and their common ancestor.
Delete ‘cool-feature’
(Time goes leftwards)
If the branches have not diverged, i.e. if there is a linear path from the current branch tip and to the target branch, then it is possible to do a fast-forward merge. Git is not really merging the branches, just integrating the histories, i.e. it moves “fast forward” the current branch tip up to the target branch tip.
Optional example, fast-forward merging
When doing a fast-forward merge, the commit histories are combined and all commit histories can be reached from the current tip. An example would be to do a fast-forward merge of a feature into master/main.
A fast-forward merge is not possible if the branches have diverged, like in the previous example. This means that there is no linear path to the target branch and Git has to combine them via a 3-way merge.
This shows an example where a fast-forward merge would work.
Before FF merge:
After FF merge:
Switching with uncommitted changes¶
As mentioned above, you switch between branches with:
What happens if you have uncommitted changes (and/or new files added) when you try to switch?
-
The uncommitted changes will be carried to the new branch that you switch to, if possible.
-
Changes that you commit will be committed to the newly switched branch.
What if there is a conflict? Conflicts can happen when two branches modify the same part of a file.
- You will not be allowed to switch to the other branch.
- You must commit or stash any conflicting changes before switching branches.
Example - new file
Here we create a new branch, switch to it, then add a new file. Then we switch back to the master branch without committing the changes:
$ git checkout -b cool-feature
Switched to a new branch 'cool-feature'
$ touch newfile.txt
$ git add newfile.txt
$ git checkout master
A newfile.txt
Switched to branch 'master'
Git warns that there is a file added (A) in one branch but not the other, but the switch is allowed.
Example - modified file
We continue in the same repository!
First commit the newfile.txt in the cool-feature branch to clean the environment.
If we make changes to the file in one of the branches (go back to cool-feature) but not on the other and do not commit it, then git will again warn:
$ git switch cool-feature
$ git commit -m "newfile.txt"
$ echo "Adding some text" >> second-file.txt
$ git add second-file.txt
$ git checkout master
M second-file.txt
Switched to branch 'master'
Git warns that there is a file that is modified (M) in one branch but not the other, but the switch is allowed.
Example - uncommitted, conflicting change
We continue in the same repository!
Assume two branches, “cool-feature” and “morefeatures”
Create the branch “morefeatures” without switching to it Switch to branch “cool-feature”, add some text to a file, stage the file and commit it:
$ git branch morefeatures
$ git checkout cool-feature
Switched to branch 'cool-feature'
$ git commit -m "second-file.txt"
$ echo "add text" >> morefiles.txt
$ git add morefiles.txt
$ git commit -m "Some text"
[cool-feature 469542b] Some text
1 file changed, 1 insertion(+)
create mode 100644 morefiles.txt
Switch to branch “morefeatures”. Modify the same file, stage the file and commit it. Then try and switch back to the “cool-features” branch:
$ git checkout morefeatures
Switched to branch 'morefeatures'
$ echo "Adding yet some more text" >> morefiles.txt
$ git add morefiles.txt
$ git checkout cool-feature
error: Your local changes to the following files would be overwritten by checkout:
morefiles.txt
Please commit your changes or stash them before you switch branches.
Aborting
Now Git complains and do not allow the switch.
Handling uncommitted changes (that are causing conflicts)¶
So, what can we do if there is a conflict?
- Commit the changes before switching branch
- Stash the uncommitted changes
- Discard the uncommitted changes
- Checkout with Merge
Stashing¶
The command “stash” can be described as a drawer where you store uncommitted changes temporarily.
After stashing your uncommitted changes you can continue working on other things.
The uncommitted changes that are stored in the stash can be taken out and applied to any branch, including the original branch.
Stashing example - try on your own in the afternoon
First do a git status in the branch where you may have uncommitted changes:
$ git status
On branch morefeatures
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: file.txt
new file: morefiles.txt
You can see the dirty status.
To fix it, let us use git stash:
Checking again with git status:
You can now switch branches and work on something else.
To later restore the file, check the stashes you have saved with git stash list and restore the the latest stash with git stash apply.
Discarding uncommitted changes¶
If you do not want to stash your changes, but just get rid of them, you can use git clean.
WARNING: This command will remove all non-tracked files in your current directory!
You can safely test which files will be removed by running:
Handling uncommitted changes - merging¶
- There is a checkout with merge option. Add the flag
--merge(or-m): -
This will perform a three-way merge between your working tree and the new branch, with the current branch as the base.
-
After the merge, you will be on the new branch and the merged result will be in your working tree.
-
NOTE: As with any merge, conflicts may result and you will then have to resolve those.
Merging and merge conflicts¶
Conflicts happen when two branches modify the same part(s) of a file/files.
- Merge conflicts will happen now and then when you are working with more than one branch and try to merge them.
- In many cases, Git is actually able to do a merge without problems. However, merge conflicts can happen.
- If Git cannot safely merge something automatically, you will get a message like this:
NOTE
Always check that you are on the right branch before merging! You check the branch with git branch.
Git can automatically try to merge when you give the command:
while standing on the branch you want to merge to.
Conflict markers
If there is a conflict, Git inserts markers like this:
OPTIONAL/SELF study - Merge strategies¶
The most commonly used
- Fast Forward Merge
- the commit history is one straight line.
- You create a branch, you make some commits there, but no changes to the ‘master’. You then just merge onto the ‘master’. This just moves the pointer for the ‘master’ branch forward in a straight line.
- Recursive Merge (until 2.32)
- make a branch and make some commits there, but also make new commits that are made on another branch, like the ‘master‘.
- Then, when you want to merge, git will recurse over the branch and create a new merge commit. The merge commit will continue to have two parents.
- ORT (from git-2.33)
- acronym for “Ostensibly Recursive’s Twin”
- replacement for the previous default algorithm, recursive.
- This is the default merge strategy when pulling or merging one branch.
- Results in fewer merge conflicts without causing mismerges
Exercise/example: A merge conflict
TYPE ALONG!
Let’s create a merge conflict:
$ mkdir merge-test
$ cd merge-test/
$ git init
$ echo "Initial content" > myfile.txt
$ git add myfile.txt
$ git commit -m "first commit"
$ git checkout -b feature_1
$ echo "Feature 1 is a good implementation" >> myfile.txt
$ git commit -a -m "start work on feature 1"
$ git checkout master
$ echo "Working on a really cool feature" >> myfile.txt
$ git commit -a -m "start work on a cool feature"
We now have two branches, master and feature_1:
$ git log --all --graph --decorate --oneline
* d8e6809 (HEAD -> master) start work on a cool feature
| * 87934eb (feature_1) start work on feature 1
|/
* ce7e46c first commit
What are the contents of myfile.txt in the two branches?
$ git diff master feature_1 -- myfile.txt
diff --git a/myfile.txt b/myfile.txt
index b14ae98..5390ea7 100644
--- a/myfile.txt
+++ b/myfile.txt
@@ -1,2 +1,2 @@
Initial content
-Working on a really cool feature
+Feature 1 is a good implementation
Or use the git show :
$ git show master:myfile.txt
Initial content
Working on a really cool feature
$ git show feature_1:myfile.txt
Initial content
Feature 1 is a good implementation
Let’s try to merge the feature_1 branch on to the master branch:
$ git merge feature_1
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.
The merge failed due to a conflict. In this case, the conflict arises because there are changes in the same line on both branches.
We can get some more information with the git status command:
$ 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:
(use "git add <file>..." to mark resolution)
both modified: myfile.txt
no changes added to commit (use "git add" and/or "git commit -a")
Let’s check the file that lead to the conflict, note the “conflict dividers”:
One can abort the merge with git merge –abort. Or one may try to solve the conflict..Resolving merge conflicts¶
Resolving merge conflicts
- The most direct way to resolve the conflict is to edit the file yourself.
- Remember to also remove the conflict markers.
- Stage the corrected file
- When this has been done, you can attempt the merge again with:
Helpful commands
- identify conflicting files:
git status - list the conflicting commits among the branches:
git log --merge - find differences between the commits involved in the merge:
git diff - reset conflicted files to a known good state:
git reset
If you made a mistake when you resolved a conflict and have completed the merge before realizing, you can roll back to the commit before the merge was done with the command git reset --hard.
Exercises¶
Each of the exercises has a README.md file with explanations and descriptions of what to do. You can find all of them in the subdirectory 5.branches. The contents of the README.md files are also listed here. You should do them in the below order:
In order to do these exercises, you need to download the exercises.zip file if you did not already do so.
wget https://github.com/hpc2n/bioinformatics-hpc/raw/refs/heads/main/exercises/07.Git/git_materials.zipunzip git_materials.zipcd git_materialscd 5.branches
You are now in a directory with 5 subdirectories, one for each exercise. You will only be doing the first three exercises, as the two last are for more advanced/optional material (You can find the material towards the end of the branches, merges, and conflicts section found under “Advanced/self-study” in the left-hand menu).
1. Merging two local branches¶
Note
The purpose of this exercise is to test the command git merge and see that the merge goes well in this case.
Situation: The ingredient list in branch “master” has an error in the ingredients which is fixed in the branch “fixed-recipe”.
Start by making sure you are in the directory git_materials/5.branches/1.merge-ok/recipes.
- First do
git statusto look at the status. You can also rungit logso you can compare before and after merging. - Now try to merge the two branches. You will see that it merges with a “fast-forward” merge.
NOTE: Remember to check that you are on the right branch! Usegit branchto check.
Merging the branch “fixed-recipe” to the “master” branch: - See that the merge goes well, and that git reports using “fast-forward” merge.
- Do
git logandgit statusafter the merge and compare what you got before. - Think about why git could merge the two braches automatically and why it used “fast-forward” merge.
2. Merging two local branches, recursive¶
Note
In this exercise you will again try the command git merge and it should again go well. However, this time git will do a recursive merge, or in newer version an “ort” merge.
Situation: The ingredient list in branch “master” has an error in the ingredients which is fixed in the branch “fixed-recipes”. After that fix, a small change was made to the recipe in the “master” branch.
Start by making sure you are in the directory git_materials/5.branches/2.merge-ok-recursive/recipes.
- First do
git statusto look at the status. Also rungit logand see the commits that have been made and to which branches. - Now try to merge the two branches. You will see that the merge happens with “recursive” merge.
NOTE: Remember to check you are on the right branch before you try to merge!
Merge the branch “fixed-recipe” to the “master” branch using thegit mergecommand. - Notice that the merge goes well and that git reports using “recursive” merge.
- Do
git logandgit statusafter the merge and compare with what you got before.
- Why did git use “recursive” merge?
3. Merging two local branches resulting in a merge conflict¶
Note
This exercise will again feature the command git merge, but this time the merge will fail and git will give a merge conflict.
Situation: In the branch “metric” we change the recipe to use the metric system for measurements. Then we change back to the “master” branch and add some coffee to that version of the recipe.
Start by making sure you are in the directory git_materials/5.branches/3.merge-bad/recipes.
- Do a
git statusfirst and note the result. Rungit log. You could also look at the output from the longer command:
or with the alias commandgit graph
NOTE: Remember to change to the subdirectory “recipes” first! - Now try to merge the two branches with the
git mergecommand and see that a conflict happens.
NOTE: Check withgit branchto find out if you are on the right branch before trying to merge.
You will get an error similar to this:
$ git merge metric Auto-merging cakerecipe.txt CONFLICT (content): Merge conflict in cakerecipe.txt Automatic merge failed; fix conflicts and then commit the result. - Use
git log(including with the above mentioned flags) andgit statusto see where the problems are and see if you can fix the conflict and then reattempt the merge.