CodeRigor

FREE LESSON

Branches, HEAD, tags & history navigation

Create and switch branches, inspect commit ancestry, and distinguish branch names, tags and detached HEAD.

A branch is a name for a line of development, implemented as a reference to its tip commit. Creating one does not copy every file or create a second object database. A checked-out branch advances when you commit; other branch references stay where they were. This is why recording a feature on the wrong branch is a reference-management mistake even when its file contents are correct.

Creation and checkout are separate actions

git branch feature/report creates a branch at the current commit but leaves HEAD where it was. git switch feature/report checks out an existing branch. git switch -c feature/report combines creating and switching. Check git branch --show-current before committing. Switching may carry compatible uncommitted edits with you; Git refuses changes that would overwrite conflicting local modifications. Switching does not automatically commit your edits.

Create versus switch
start / 4
Create versus switch
Git commands
git switch -c feature/report
git add feature.txt
git commit -m "Add report generation"
git log --all --graph --decorate --oneline

The new commit is reachable from feature/report. main still names the old tip. git merge-base --is-ancestor main feature/report tests ancestry by exit status; it does not require comparing timestamps. In a commit graph, parent links describe historical relationships, not chronological sorting alone.

Divergent branch tips
start / 2
Commit arrows point to parents. Only the checked-out feature branch advanced.
Divergent branch tips
Commit arrows point to parents. Only the checked-out feature branch advanced.

Inspect a revision without moving a branch

HEAD~2 follows first-parent links twice. HEAD^2 selects the second parent of a merge, which is a different question. For a simple history main..feature/report selects commits reachable from feature/report that are not reachable from main. For git log, three dots select the symmetric difference; for git diff A...B, three dots instead compare the merge base to B. The meaning depends on the command, so avoid memorizing the punctuation without its context.

git switch --detach <revision> checks out a commit without attaching HEAD to a branch. Committing still works there, but no named branch advances. If the result matters, give it a name with git switch -c keep-experiment before navigating away. A lightweight tag is a named reference; an annotated tag has its own metadata-bearing tag object. Tags normally mark a revision and do not advance as you commit. Creating a tag locally does not automatically publish it with every push.

Names have different roles
start / 4
Names have different roles

Check the contract, not just the files

The lab asks for the feature on a named branch and main unchanged. Identical visible files are insufficient if you advanced the wrong reference. Inspect the decorated graph after running your commands. The grader checks branch identity, ancestry, committed contents and the final clean state. The broader habit is simple: name the intended line of development before recording work, and inspect references when moving between tasks.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Review the object/reference model and core workflows here. The authored lesson and linked Git manual cover the current command forms and advanced details used in this lab.