Level 3 — Branches
Intermediate Git CLI GitLab UI GitHub UI
Objective
Do a complete piece of work the way a team does: one issue, one branch, two commits, a published branch, a comparison, a merge, and a cleanup. This is the loop you will repeat for every change from now on, so the aim is to run it once slowly and see what each command reports.
Starting state
- The playground connected to your own private project, from Lab 2.
git remote -vshows your project andgit statussays your branch tracksorigin/main. mainup to date:git switch main && git pull.- A working tree with nothing uncommitted:
git statussays "nothing to commit, working tree clean".
Pretend issue #33 exists: "FAQ: add a question about Windows".
Instructions
-
Start from a current
mainand branch.Terminal $ git switch main $ git pull $ git switch -c docs/33-faq-windowsAlready on 'main' Switched to a new branch 'docs/33-faq-windows'Checkpoint:
git statussaysOn branch docs/33-faq-windows. -
First commit. Add a question at the end of
docs/faq.md:Markdown ## Does it run on Windows? Yes, with Python 3.10 or newer.Then:
Terminal $ git add docs/faq.md $ git commit -m "docs: add Windows question to FAQ (#33)"[docs/33-faq-windows 683cb88] docs: add Windows question to FAQ (#33) 1 file changed, 4 insertions(+)Note the branch name in the output: the commit went to your branch, not to
main. -
Second commit. Add one more line to the same answer, for example
See the installation guide for details., then:Terminal $ git commit -am "docs: link the installation guide from the FAQ (#33)" $ git log --oneline -20755cde docs: link the installation guide from the FAQ (#33) 683cb88 docs: add Windows question to FAQ (#33) -
Publish the branch.
Terminal $ git push -u origin docs/33-faq-windowsTo https://gitlab.com/you/trailguide.git * [new branch] docs/33-faq-windows -> docs/33-faq-windows branch 'docs/33-faq-windows' set up to track 'origin/docs/33-faq-windows'.Checkpoint: refresh the project page. The branch appears in the branch list, and the platform offers to create a merge request from it.
-
Read what your branch adds, exactly as a reviewer will:
Terminal $ git log --oneline main..docs/33-faq-windows0755cde docs: link the installation guide from the FAQ (#33) 683cb88 docs: add Windows question to FAQ (#33)Terminal $ git diff main...docs/33-faq-windows --statdocs/faq.md | 6 ++++++ 1 file changed, 6 insertions(+)Two commits, one file, six added lines. That is the whole change.
-
See the tracking relationship.
Terminal $ git branch -vv* docs/33-faq-windows 0755cde [origin/docs/33-faq-windows] docs: link the installation guide from the FAQ (#33) main a2bd5c9 [origin/main] Initial import of trailguideEach local branch, its newest commit, and the remote branch it tracks.
-
(Optional but realistic) Open a merge request on the website from
docs/33-faq-windowsintomain, withCloses #33in the description. Then merge it there and skip to step 9, because the platform will have done step 8 for you. To practise the command instead, close the merge request and continue. -
Merge locally.
Terminal $ git switch main $ git merge docs/33-faq-windowsUpdating a2bd5c9..0755cde Fast-forward docs/faq.md | 6 ++++++ 1 file changed, 6 insertions(+)Fast-forward: nothing had changed onmain, so the label simply moved (lesson 6.6). Push it:Terminal $ git pushTo https://gitlab.com/you/trailguide.git a2bd5c9..0755cde main -> main -
Clean up both copies of the branch.
Terminal $ git branch -d docs/33-faq-windowsDeleted branch docs/33-faq-windows (was 0755cde).Terminal $ git push origin --delete docs/33-faq-windowsTo https://gitlab.com/you/trailguide.git - [deleted] docs/33-faq-windowsIf you merged through the website with "delete source branch" ticked, the second command answers that the branch is already gone; run
git fetch --pruneinstead. -
Confirm the end state.
Terminal $ git log --oneline --graph --decorate -3* 0755cde (HEAD -> main, origin/main) docs: link the installation guide from the FAQ (#33) * 683cb88 docs: add Windows question to FAQ (#33) * a2bd5c9 Initial import of trailguide
Expected result
maincontains both commits, and local and remote agree (HEAD -> main, origin/mainon the same commit).git branchlists onlymain.- The branch is gone from the project page.
- Your FAQ has the new question.
Common mistakes
- Committing on
mainby mistake. The output ofgit commitnames the branch; if it says[main …], see lesson 6.9 for the rescue: branch from where you are, then resetmain. fatal: The current branch … has no upstream branchon the first push. Use-u origin <branch>once, as in step 4.git mergewhile still on the feature branch. That mergesmaininto your branch instead (lesson 6.4). Read the first line ofgit statusbefore merging.- Deleting only the local branch. The name stays in everyone's branch list. Step 9 has both halves.
- A merge that is not a fast-forward. If
mainmoved while you worked, step 8 makes a merge commit instead, which is correct and not a problem.
Show solution
The whole lab as one sequence, with the editing steps described:
$ git switch main && git pull
$ git switch -c docs/33-faq-windows
# add the Windows question to docs/faq.md
$ git add docs/faq.md
$ git commit -m "docs: add Windows question to FAQ (#33)"
# add one more line to the same answer
$ git commit -am "docs: link the installation guide from the FAQ (#33)"
$ git push -u origin docs/33-faq-windows
$ git log --oneline main..docs/33-faq-windows
$ git diff main...docs/33-faq-windows --stat
$ git switch main
$ git merge docs/33-faq-windows
$ git push
$ git branch -d docs/33-faq-windows
$ git push origin --delete docs/33-faq-windowsWhat happened and why
Creating the branch cost nothing: one label at the commit main was on (lesson 6.1). Your two commits moved that label while main stayed put, which is why step 5 could show exactly what the branch adds. Publishing created the same branch on the server and linked the two, so git status could report ahead and behind. The merge was a fast-forward because main had gained nothing meanwhile, so no merge commit was needed. Deleting the branch removed two labels; every commit is still there, now reachable from main.
In real work step 7 replaces step 8: the merge request is where review and the pipeline happen, and the platform presses the merge button for you. The commands are worth knowing because they are what the button does.
Next: Level 4 adds a second person, and with them the first real conflict.