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

This content is a draft and will not be included in production builds.

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. As you create branches, switch between them, commit, and merge, watch how the graph changes shape — that shape is what git log --graph is showing you on the command line.

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 (press “reset”), type a name like feature/login into the branch field and click git branch. Notice that HEAD doesn’t move as you’ve created a label, but you’re still on main. Now use git switch to move onto the new branch.
  2. Commit on a branch. With HEAD on your feature branch, make two commits. Watch how only the feature lane grows; main sits still on its own commit. Use “git push origin” to publish the branch to GitLab. Switch back to main and then merge the branch into main. Notice that the HEAD is now on the feature branch. If you push now you can publish this update to GItLab.

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
3fd0mainHEAD
GitLab — origin
0 branches pushed
No commits here yet — push a branch to publish it.
Commands
on branch main at 3fd0c1e
·
Merge into main:
·
Preset scenarios
jump straight to a teaching moment
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 — 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 some presets:

  1. Fast-forward merge. Load the Feature ready to fast-forward preset. 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. Load the Diverged branches preset. 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. Load the Merge conflict ahead preset. Both branches edited the same line of README differently. When you run git merge feature, the merge pauses and a red panel appears 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. 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.
  5. 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 — 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 situation where Git cannot automatically decide which changes to keep on the same line.
A merge that creates a new 'merge commit' to combine two diverged histories.
A merge where Git simply slides the branch pointer forward because there are no competing changes.
A special pointer that indicates which branch or commit you are currently working on.
Knowledge Check

What happens during a 'fast-forward' merge?

Knowledge Check

When does a 'three-way merge' occur?

Knowledge Check

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

Knowledge Check

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