💻Virtual Lab: Git Branch

Branching, Merging and Conflicts
Section titled “Branching, Merging and Conflicts”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.
Tasks to try
Section titled “Tasks to try”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.
- 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/logininto the branch field and clickgit branch. A new label appears on the graph in its own colour, but notice thatHEADdoesn’t move: you’ve created a label and you’re still onmain. Now clickgit switch; the menu beside it has already selected your new branch. - Commit on a branch. With
HEADon your feature branch, use the Record group to make two commits. Watch how only the feature lane grows;mainsits still on its own commit. Usegit push originin Publish & tidy to publish the branch to GitLab. Switch back tomain, then merge the feature branch from the Combine group. Notice thatmainnow points to the same commit as your feature branch andHEADis onmain. Push again to publish the updatedmainto GitLab. - Detach HEAD. Click any older commit directly on the local graph.
HEADdetaches and floats above that commit with no branch label carrying it, the Commands header now reads(detached), andgit commitis disabled: a detachedHEADpoints straight at a commit rather than riding a branch, so new work made there would be easy to lose. Usegit switchto 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.
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:
- Fast-forward merge. Click Feature ready to fast-forward. You’re on
main(i.e., HEAD), andfeatureis two commits ahead. Rungit merge feature. Themainpointer slides forward to meetfeature; no merge commit is created, becausemainhad no commits of its own to combine. - Three-way merge. Click Diverged branches. Now both branches have new commits, so a fast-forward isn’t possible. Run
git merge featurefrommainand 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”. - Resolve a conflict. Click Merge conflict ahead. Both branches edited the same line of
READMEdifferently. When you rungit 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. - Hotfix while a feature is open. Click Hotfix while feature open. You’re on
feature, and a hotfix has landed onmainwhile you were working. First bring the hotfix into your branch by mergingmainintofeature: a three-way merge commit appears, because both branches had moved. Now switch tomainand mergefeatureto finish the job. This second merge fast-forwards. Can you explain why? (Hint: after the first merge, which commits couldmainpossibly have thatfeaturedoesn’t?) - Publish a branch to GitLab. With a feature branch checked out, click
git push origin. The branch appears in the right-handoriginpanel with the prefixorigin/feature/login. In a real workflow this is the moment you’d open a merge request on GitLab. - Clean up. Once a feature branch has been merged into
main, you can safely delete the local label withgit branch -d. The commits don’t disappear, since they’re still reachable frommain, but the branch pointer is gone. Try deleting an unmerged branch and read the error: Git protects you from losing work.
Knowledge Check
Section titled “Knowledge Check”Match Branching & Merging Concepts
What happens during a 'fast-forward' merge?
When does a 'three-way merge' occur?
Which markers does Git use to identify a conflict area in a file?
What does it mean when HEAD is 'detached'?
What is the result of deleting a local branch with 'git branch -d' after it has been merged?
© 2026 Derek Molloy, Dublin City University. All rights reserved.