CH.10 · PART 3

One history.
Two directions.

Try an idea without mixing it into main. A branch is a movable label on a commit, not a second folder.

branchswitchHEAD
Main stays at A while the feature branch moves to BABmainfeature/greetingHEAD → current branch

Three labels, one commit graph

1

main

The branch name used for this lesson. It is not special to Git. Other repositories use other names.

2

feature/greeting

A separate pointer. Committing here moves this branch forward. Main stays where it was.

3

HEAD

Usually points to the branch you have checked out. That tells Git where your next commit belongs.

Switch the snapshot

interactive model, not a real shell

Both commits remain in history. Switching changes which committed snapshot you see in the working tree. This model assumes no uncommitted edits.

Current branch

HEAD → main → A

greeting.txt

hello

History

A → B
main → A
feature/greeting → B

Run it on your computer

Use a new empty directory, not an existing project. Run git init -b main, set your Git name and email if needed, then create greeting.txt containing hello. Stage and commit it. Make the branch, add a second line welcome, then stage and commit again.

git init -b main
git add greeting.txt
git commit -m "Start greeting"
git switch -c feature/greeting
# Edit greeting.txt: add welcome on its own line.
git add greeting.txt
git commit -m "Add welcome"
git branch
git log --oneline --all --decorate
git switch main
# Open greeting.txt. It contains only hello.
git switch feature/greeting
# Open greeting.txt again. Welcome returns.

git branch feature/greeting creates a branch but does not switch to it. git switch -c feature/greeting does both. git switch requires Git 2.23 or newer.

See the real recorded run

Recorded locally with Git 2.34.1 in a temporary Linux directory. Name and email were configured for a demo identity. The file edits used shell printf. Paths, hashes and wording can differ on your computer. Commands that print no output succeeded silently.


$ git --version 
git version 2.34.1

$ git init -b main 
Initialized empty Git repository in /tmp/ggw/demo/.git/

$ git add greeting.txt 

$ git commit -m Start greeting 
[main (root-commit) af9f623] Start greeting
 1 file changed, 1 insertion(+)
 create mode 100644 greeting.txt

$ git switch -c feature/greeting 
Switched to a new branch 'feature/greeting'

$ git add greeting.txt 

$ git commit -m Add welcome 
[feature/greeting d1ebb55] Add welcome
 1 file changed, 1 insertion(+)

$ git branch 
* feature/greeting
  main

$ git log --oneline --all --decorate 
d1ebb55 (HEAD -> feature/greeting) Add welcome
af9f623 (main) Start greeting

$ git switch main 
Switched to branch 'main'

$ cat greeting.txt 
hello

$ git switch feature/greeting 
Switched to branch 'feature/greeting'

$ cat greeting.txt 
hello
welcome

Before you switch

Check git status. A branch does not automatically save uncommitted changes. Git may carry compatible edits across branches, or refuse to switch if an edit would be overwritten. Commit work you want to keep, or stash it deliberately. Do not use a force flag to get past a warning.

Switching also does not publish your branch online. Remotes come later in Chapter 13.

Quick check

You commit B on feature/greeting. Where does main point?

Choose an answer.

Keep going

Switch between the two branches in your practice repository, then inspect git log --oneline --all --decorate. Can you explain why both commits still exist?

Sources

Concepts checked against the official Git book and command documentation. The terminal transcript above is a real local run, not copied output.

Git book: Branches in a Nutshell · git switch documentation

Git, Visually · Part 3 · English