Skip to content
jsworkbench.
Learning path
Foundations18 min

Branches

Understand the idea

A branch is a movable label on a commit. Switching branches moves the label, it does not copy the files.

The idea

A is a name that points at one commit. says which name you are using. When you commit, that name moves. git branch topic creates the note. git switch topic moves HEAD onto it. The files on disk become that commit's snapshot.

What changes

Creating a branch does not change the until you switch and the snapshots differ. Committing on topic moves only topic. main stays where it was, which is why the graph forks. Checking out a raw hash detaches HEAD: the next commit has a parent, but no branch name points at it until you create one.

Labels versus copies
ActionWhat movesWhat stays
git branch topicA new label appears on the current commitThe files
git switch topicHEAD and the working treeOther branch labels
git commit on topicThe topic labelmain

The command

git branch topic, then git switch topic, then write, add, and commit. Switch back with git switch main. The graph shows topic on the new commit and main on the old one. git switch -c experiment creates and switches in one step. git checkout accepts the same idea.

The mistake

Check your graph

topic and main should point at different commits, and HEAD should sit on the branch you switched to. The files in git show topic match the commit, not a private copy of the folder.

Your repository

Create a branch, commit on it, and switch back. Watch the label and HEAD move.

  1. Step 1

    Name the current commit

    A branch is a label. This command puts topic on the same commit as main. It does not copy files.

  2. Step 2

    Switch onto that label

    HEAD now uses topic. If topic already exists, run git switch topic instead.

  3. Step 3

    Edit on this branch

    Create notes.txt in the editor and set the text to: topic work

  4. Step 4

    Stage and commit on topic

    Only the topic label moves. main stays on the previous commit.

  5. Step 5

    Store the topic snapshot

    The graph forks here. topic points at the new commit.

  6. Step 6

    Return to main

    The working tree becomes main’s snapshot. notes.txt from topic is no longer the checked-out file.

  7. Step 7

    Read both labels

    The graph shows topic and main on different commits.

The editor edits the working tree. Start a repository and the files appear here.

Follow the steps. Each command step can be placed in the terminal. Edit files in the editor, then stage them.
Supported command reference
  • init
  • add <path> | add .
  • commit -m "message" | commit --amend -m "message"
  • status
  • diff | diff --staged
  • log | log --oneline | log --graph | log --oneline --graph
  • show [ref]
  • branch | branch <name> | branch -d <name>
  • checkout <ref> | checkout -b <name> | switch <name> | switch -c <name>
  • merge <branch>
  • rebase <branch> | rebase -i <branch> | rebase --edit-todo <pick|drop|reword> <hash> [message] | rebase --continue | rebase --abort
  • cherry-pick <ref>
  • reset --soft <ref> | reset --mixed <ref> | reset --hard <ref>
  • revert <ref>
  • stash | stash list | stash pop | stash apply
  • restore <path> | restore --staged <path>
  • remote | remote add <name> <repo-slug>
  • clone <repo-slug> | git clone <repo-slug> | git fork <repo-slug>
  • fetch [remote] | pull [remote] [branch] | push [remote] [branch] | push --force [remote] [branch]
  • blame <path>
  • reflog
  • tag | tag <name> [ref] | tag -d <name>
  • bisect start | bisect good [ref] | bisect bad [ref] | bisect reset
  • config <key> <value> | config --list
  • write <path> <text> (workbench: edit the working tree)
  • issue new "title" | pr new <source> <target> "title" | review "comment" | review --request-changes "comment"

Commit graph

No commits yet. git init creates the first one.

Quiz

A quick check for understanding. Retry as often as you like; your learning path stays open.

01What does a branch store?
02What is detached HEAD?

Ready to call this one understood?

Mark this lesson complete to earn 100 XP. You can always revisit it.

Sign in to save progress
Next lesson