Skip to content

Create and switch branches

Branching · Article 3 of 9 · 2 min read

Branches let you try an idea without touching the work that already functions.

The short version

A branch is a movable name that points at a commit. Creating one is instant and costs almost nothing.

Details

Your current branch is where new commits go. Switching branches changes the files in your folder to match that branch.

Try it yourself

git switch -c add-greeting
echo "Hello" > greeting.txt
git add greeting.txt
git commit -m "Add greeting"
git switch main
ls

Back on main, greeting.txt is gone. Switch to add-greeting and it returns.

Explain it to the duck

Where did greeting.txt go when you switched back to main?

Tested with: Git 2.51.