Git and GitHubgit 2.54 · GitHub · macOS, Linux or Windows
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
26 small wins to finish your pathNext lesson →

Branches

A branch is a name that points at one commit, and Git moves that name forward every time you commit while you are on it.

Last updated: 12 Oct, 2026 · git 2.54

Up to here the project has had one line of history. A branch gives it a second one. The usual reason is an idea you are not sure about: a different prompt, a chunk size of 800 instead of 500, a loader that strips headings. Each idea gets its own branch, and the branch is either merged or deleted when you know the answer. The commits on main never move while you try.

Creating a branch with git branch · from the Git and GitHub series · 7:08 to 8:02
A new branch is a second name for the same commit. Two commits are drawn in a chain. The name main and the new name chunk-800 both point at the newest commit, and HEAD points at main, which is the branch you are on. Creating a branch copies no files; it writes one small file holding the identifier of that commit.

Syntax: the same command lists branches, creates one, and deletes one.

bash
git branch                 # list them, with a star on the one you are on
git branch <name>          # create a branch pointing at the current commit
git branch -d <name>       # delete a branch that has been merged
Project files used on this pageThis lesson builds on a project from earlier lessons. The code below imports this file. Click a file to see its code, or follow the link to the lesson that wrote it. To run the code yourself, keep it in the same folder.
View the code here
.gitconfig
[user]
	name = Reader
	email = reader@example.com

Creating a branch for one experiment

The repository below is a throwaway one in a temporary folder, and every example on this page starts from it. It holds one commit. The experiment is a bigger chunk size, so the branch is named after it, and a name that says what the idea was is worth the ten seconds.

ExampleThe throwaway repository, and the one branch it starts with
mkdir ask-docs && cd ask-docs && git init -q -b main
export GIT_AUTHOR_DATE="2026-10-12T10:00:00+00:00" GIT_COMMITTER_DATE="2026-10-12T10:00:00+00:00"
printf 'You are a support assistant.\n' > prompt.txt
printf 'chunk_size = 500\n' > loader.py
git add . && git commit -q -m "Add the prompt and the loader"
git branch

git branch on its own lists the branches and marks the one you are on with a star. A repository that has never been branched has exactly one name in it.

Creating the branch

ExampleA second name for the same commit
git branch chunk-800
git branch

git branch chunk-800 printed nothing and created the name. The listing has two lines now, and the star has not moved: the commits you make next still go to main.

Reading which commit each name points at

ExampleThe commit behind each branch name
git branch -v
git branch --show-current
  • -v adds the commit each name points at. The same short identifier and the same subject appear on both lines, because both names are pointing at the one commit there is.
  • git branch --show-current prints the one name you are on, which is useful inside scripts and before a merge.

Creating a branch copies nothing. Git writes a small file under .git/refs/heads holding the identifier of the commit, which is why branches are instant whatever the size of the project. It also leaves you where you were: you are still on main, and the next page moves you across.

Why the name stays behind

A branch moves only when you commit on it. Commit on main after creating chunk-800, and the two names come apart.

ExampleOne commit on main, and the experiment stays where it was
git branch chunk-800
printf 'You are a support assistant. Answer from the documents.\n' > prompt.txt
git commit -q -am "Tell the model to answer from the documents"
git branch -v

git branch -v now shows two different commits. main moved to the new one and chunk-800 stayed on the old one, because a branch moves only when you commit on it.

Seeing both lines of history at once

ExampleThe history of every branch, not only this one
git log --oneline --all --graph --decorate
  • --all adds the commits of every branch, not only the one you are on, and --decorate prints the names in brackets, so (HEAD -> main) says where you are standing.
  • The graph is still a straight line, because the experiment has no commits of its own yet. It starts to fork the moment you switch to it and commit.
The same repository after each branch has its own commit. From the shared first commit, main goes one way with the commit that tells the model to answer from the documents, and chunk-800 goes the other way with the commit that raises the chunk size. The two lines of history have no commit in common after the point where they split.

One branch per experiment

  • Name it after the idea. chunk-800, shorter-prompt, strip-headings. A reader of git branch then knows what was tried.
  • Keep it short-lived. An experiment that runs for weeks drifts far from main and the merge gets painful.
  • Throw it away without guilt. git branch -d deletes a merged branch and git branch -D deletes one whose commits were never merged. The experiment that failed costs nothing to delete, and main never saw it.

A branch compared with a copy of the folder

ask-docs-v2 copieda branch
Disk usedthe whole project againone small file
Made inas long as the copy takesan instant
Historynone, it starts emptythe full history, shared
Bringing it backcopy files by handgit merge
Knowing what differscompare by eyegit diff main chunk-800
Throwing it awaydelete and hopegit branch -D

Where you create a branch

  • An experiment you may not keep. One branch, one idea, measured against main.
  • A feature that takes a few days. Nobody else sees half-finished work, and main stays runnable.
  • A fix while a feature is in progress. Branch from main, fix, merge, and go back to the feature branch.
Watch out. git branch chunk-800 creates the branch and leaves you where you are. Commits you make next still go to the branch you were on, which is the most common way work ends up on main by accident. Check with git branch --show-current, or create and move in one step with the command on the next page.
Try it yourself
  • Create three branches with different names and run git branch -v. All three point at the same commit.
  • Run git branch -d chunk-800 on a branch with no commits of its own. Git deletes it without complaining, because nothing would be lost.
  • Run cat .git/refs/heads/chunk-800 and compare what it holds with the output of git log --oneline -n 1.

This is what real progress feels like.