Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

💻Virtual Lab: Git Branch

Now that you’ve seen how add, commit, push and pull move a single line of work between the working directory, staging area, local repo and remote, it’s time for the next idea: doing more than one thing at once. Branches let you split a project’s history so you can develop a new feature, try an experiment, or apply a hotfix without disturbing the work everyone else is doing on main. Merging is how those parallel histories come back together. This is sometimes clean, sometimes not.

The lab below renders the local repository as a left-to-right commit graph, with each branch on its own horizontal “lane”. The remote panel on the right is your GitLab origin: branches only appear there once you push them. The small legend under the graphs decodes the symbols: the ring marks HEAD, a node labelled M is a merge commit, and every commit keeps the colour of the branch it was born on. Hover over any commit to see its hash and message. As you create branches, switch between them, commit, and merge, watch how the graph changes shape; that shape is exactly what git log --graph shows you on the command line.

The lab is organised top to bottom the way you’ll use it: the graphs, then a row of preset scenarios that jump straight to a teaching moment, then the commands, grouped by what they do. Record makes commits, Pointers creates branches and moves HEAD, Combine merges, and Publish & tidy pushes to GitLab and deletes old labels.

Work through these in order. After each one, glance at the What happened panel at the bottom as it explains what each command did, in plain English.

  1. Make a branch and switch to it. From the fresh starting state (click the Fresh repo preset, or Reset at the top right of the Commands panel), go to the Pointers group, type a name like feature/login into the branch field and click git branch. A new label appears on the graph in its own colour, but notice that HEAD doesn’t move: you’ve created a label and you’re still on main. Now click git switch; the menu beside it has already selected your new branch.
  2. Commit on a branch. With HEAD on your feature branch, use the Record group to make two commits. Watch how only the feature lane grows; main sits still on its own commit. Use git push origin in Publish & tidy to publish the branch to GitLab. Switch back to main, then merge the feature branch from the Combine group. Notice that main now points to the same commit as your feature branch and HEAD is on main. Push again to publish the updated main to GitLab.
  3. Detach HEAD. Click any older commit directly on the local graph. HEAD detaches and floats above that commit with no branch label carrying it, the Commands header now reads (detached), and git commit is disabled: a detached HEAD points straight at a commit rather than riding a branch, so new work made there would be easy to lose. Use git switch to re-attach to a branch and carry on safely.

Git Branching Lab

Create branches, switch between them, and watch git merge handle fast-forwards, three-way merges, and conflicts. Push branches to GitLab so they're ready for a merge request.

Local Repository1 branch
HEAD → main
3fd0c1e · Initial commit · born on main3fd0mainHEAD
GitLab origin
0 branches pushed
No commits here yet: push a branch to publish it.
ring = HEADmerge commit (two parents)colour = branch a commit was born ontip: hover a commit for its message; click one to detach HEAD
Preset scenarios
jump straight to a teaching moment

main only, one commit

Commands
on branch main at 3fd0c1e
Record

commit records a snapshot and moves the current branch's pointer forward

Pointers

branch makes a label and nothing more; switch is what moves HEAD

Combine
Merge into main:

brings another branch's commits into the current one: fast-forward, 3-way merge, or conflict

Publish & tidy

push copies commits to GitLab; -d removes a label, never the commits it pointed at

What happenedlast 1 entry
Repository initialised on branch main with one commit. Ready to branch.

Try this: load Diverged branches, switch to main, run git merge feature, and watch a merge commit appear with two parents.

The mental model: Branches are not folders or copies of the code. A branch is a movable label that points at a single commit. git branch creates a label; git switch moves HEAD to a different label; committing advances whichever label HEAD is currently riding. The graph is the truth and the labels are just convenient names for spots on it.

Now try the presets. Each one is a single click, and the caption under the row reminds you what the scenario sets up:

  1. Fast-forward merge. Click Feature ready to fast-forward. You’re on main (i.e., HEAD), and feature is two commits ahead. Run git merge feature. The main pointer slides forward to meet feature; no merge commit is created, because main had no commits of its own to combine.
  2. Three-way merge. Click Diverged branches. Now both branches have new commits, so a fast-forward isn’t possible. Run git merge feature from main and look closely at the resulting commit: it has two parent edges feeding into it. That’s a merge commit, and it’s how Git records “these two histories came back together here”.
  3. Resolve a conflict. Click Merge conflict ahead. Both branches edited the same line of README differently. When you run git merge feature, the merge pauses and a conflict panel scrolls into view with the file showing <<<<<<<, ======= and >>>>>>> markers, exactly what you’d see in a real editor. Try the Keep ours, Keep theirs and Keep both shortcut buttons to see what each does, then commit to complete the merge.
  4. Hotfix while a feature is open. Click Hotfix while feature open. You’re on feature, and a hotfix has landed on main while you were working. First bring the hotfix into your branch by merging main into feature: a three-way merge commit appears, because both branches had moved. Now switch to main and merge feature to finish the job. This second merge fast-forwards. Can you explain why? (Hint: after the first merge, which commits could main possibly have that feature doesn’t?)
  5. Publish a branch to GitLab. With a feature branch checked out, click git push origin. The branch appears in the right-hand origin panel with the prefix origin/feature/login. In a real workflow this is the moment you’d open a merge request on GitLab.
  6. Clean up. Once a feature branch has been merged into main, you can safely delete the local label with git branch -d. The commits don’t disappear, since they’re still reachable from main, but the branch pointer is gone. Try deleting an unmerged branch and read the error: Git protects you from losing work.
Concept Match

Match Branching & Merging Concepts

Drag each definition into its matching concept slot, then click Submit. Tap × to return a placed card to the pool.

Fast-forward Merge
drag a definition here…
Three-way Merge
drag a definition here…
Merge Conflict
drag a definition here…
HEAD
drag a definition here…

Definition Pool

A merge that creates a new 'merge commit' to combine two diverged histories.
A special pointer that indicates which branch or commit you are currently working on.
A merge where Git simply slides the branch pointer forward because there are no competing changes.
A situation where Git cannot automatically decide which changes to keep on the same line.
Quiz
Select 0/1

What happens during a 'fast-forward' merge?

Quiz
Select 0/1

When does a 'three-way merge' occur?

Quiz
Select 0/1

Which markers does Git use to identify a conflict area in a file?

Quiz
Select 0/1

What does it mean when HEAD is 'detached'?

Quiz
Select 0/1

What is the result of deleting a local branch with 'git branch -d' after it has been merged?