Skip to content

Git branching

In this hands-on session you will practice branching in git. Still working locally.

So far you have worked on a single straight line of commits — what Git calls a branch. Branches are what make Git powerful for experimentation and collaboration: they let you work on a new idea in isolation, without risking the stable version of your project. In this workshop everything stays on your own machine; remotes (GitHub, pushing, pull requests) come in a later session.

By the end of the workshop you should be able to:

  • Explain what a branch is (a movable pointer to a commit)
  • Create, list, switch between, and delete branches
  • Merge a branch back into another and understand a fast-forward merge
  • Recognise and resolve a merge conflict
  • Visualise your history with git log --graph

Start by opening your project in Positron

Check where you are

git status
Useful branch commands:
Command What it does
git branch List branches (current marked with *)
git branch <name> Create a branch (without switching to it)
git switch <name> Preferred: move onto an existing branch
git switch -c <name> Preferred: create and switch to a new branch
git checkout <name> Older equivalent of git switch <name>
git checkout -b <name> Older equivalent of git switch -c <name>
git branch -d <name> Delete a branch you no longer need
git branch -D <name> Force-delete a branch (even if unmerged)

Name branches by intent

Use descriptive names like add-logging, fix-mean-bug, or try-new-model. Avoid vague names like test or branch1.

Exercise 3.1 — Branch and commit in isolation

Start from the project you built in the previous session (the one with analysis.R / analysis.py and its tests).

  1. Create and switch to a branch
  2. On this branch, edit your script to also report the median value.
  3. Run your script to confirm everything works.
  4. Stage and commit the change
  5. Switch back to main branch

What happend?

Help

Create and switch to branch: git switch -c improve-summary Median: median(values) Run script: Rscript analysis.R Stage: git add analysis.R # or analysis.py Commit: git commit -m "Report median in summary" Switch back to main: git switch main

your median change is not there. That is the point: the work is isolated on improve-summary.

Exercise 3.2 — Merge a branch back
  1. Visualise your history
  2. Make sure you are on main branch
  3. Merge improve-summary onto main
  4. Explain what happend to HEAD
Help

Visualise: git log --oneline --graph --all Check branches: git branch Merge: git merge improve-summary

If main has not moved since you branched, Git simply moves the main pointer forward to the same commit as improve-summary. This is a fast-forward merge — no new commit is created, because the histories are already a straight line.

What happend to HEAD

If main has not moved since you branched, Git simply moves the main pointer forward to the same commit as improve-summary. This is a fast-forward merge — no new commit is created, because the histories are already a straight line.

To always record a merge commit even when a fast-forward is possible, use:

git merge --no-ff improve-summary

The --no-ff (no fast-forward) flag creates an explicit merge commit, which keeps a clear record that the work came from a separate branch. Many teams prefer this for readability.

Visualise your history: gitgit log --oneline --graph --all. What do you see?
* 232528e (HEAD -> main, improve-summary) added median
* d3d4264 added values
* f701e59 added min values
* e15b308 first commit
Exercise 3.3 — A second branch and a real merge
  1. From main, create another branch called: add-range.
  2. On add-range, edit the script to also report the range.
  3. Check that script works, stage and commit it.
  4. Switch back to main and add standard deviation
  5. Check that script works, stage and commit it.
Help

Create and switch to new branch: git switch -c add-range Range: diff(range(values)) or max(values) - min(values) Commit: git add analysis.R and git commit -m "range added" Switch: git switch main SD: sd(values)

Inspect the result: git log --oneline --graph --all. What do you see?
* d6c6e69 (HEAD -> main) sd added
| * 7e5c133 (add-range) range added
|/
* 232528e (improve-summary) added median
* d3d4264 added values
* f701e59 added min values
* e15b308 first commit
Exercise 3.4 —Merge conflicts

A conflict happens when two branches change the same lines of the same file and Git cannot decide which version to keep. This is normal and not dangerous — Git just pauses the merge and asks you to choose.

  1. Merge add-range into main
Help

Merge: git merge add-range

What happens?

If you got a conflict lets wait with the merge: git merge --abort

Let's add a deliberate conflict

  1. From main, create git switch -c change-label.
  2. On change-label, edit the printed label text (for example change "Mean:" to "Average:"). Commit it.
  3. Switch to main, then create git switch -c change-value from main.
  4. On change-value, edit the same line to a different label (e.g. "Mean value:"). Commit it.
  5. Check your commit log git log --oneline --graph --all
  6. Merge change-label branch into main then change-value into main

What happens?

Help
git switch main
git merge change-label
git merge change-value

The second merge stops with CONFLICT (content): Merge conflict in analysis.R.

Resolve the conflict

Open the conflicted file. Git marks the conflicting region like this:

<<<<<<< HEAD
cat(sprintf("Mean value: %.2f\n", mean_value))
=======
cat(sprintf("Average: %.2f\n", mean_value))
>>>>>>> change-value
  • The part between <<<<<<< HEAD and ======= is your current branch's version.
  • The part between ======= and >>>>>>> change-value is the incoming branch's version.

Edit the file so it contains only the version you want (delete the marker lines), or try the Resolve in Merge Editor if that is available. This tool can be helpful especially when there are larger conflicts.

then stage and commit:

git add analysis.R          # or analysis.py
git commit -m "Resolve label conflict, keep 'Mean value'"

Check log again: git log --oneline --all --graph. Are the any unmerged branches (eg. add-range)?

If so, 1. Try again to merge add-range into main and resolve any conflicts 2. Check the log again

Exercise 3.5 — Clean up finished branches

After a branch is merged and you no longer need it, delete it to keep the list tidy:

git branch -d improve-summary
git branch -d add-range
git branch -d change-label
git branch -d change-value

Check your local branches

Git refuses git branch -d on a branch whose work is not yet merged, which protects you from losing work. Use -D only when you are certain you want to discard an unmerged branch.

Remote branches

A remote branch is a branch on another copy of a repository, usually hosted on GitHub. After git fetch, Git updates a remote-tracking reference such as origin/main to show the latest commit it knows about on that remote branch. You do not work directly on origin/main; you work on a local branch and later share commits with the remote. You will practise fetching and tracking remote branches in the collaboration exercises.

Recap of the commands you learned

Command Purpose
git branch List, create, or delete branches
git switch <name> / -c Move to / create-and-move to a branch
git checkout -b <name> Older equivalent of git switch -c
git merge <branch> Merge another branch into the current one
git merge --no-ff <branch> Merge but always create a merge commit
git merge --abort Cancel an in-progress merge
git log --oneline --graph --all Visualise branch and merge history
git branch -d / -D Delete a branch (safe) / force-delete

What you can now do

You can isolate experimental work on branches, merge finished work back into main, resolve the inevitable conflicts, and keep your history readable with a graph view — all locally.