Try an idea without mixing it into main. A branch is a movable label on a commit, not a second folder.
The branch name used for this lesson. It is not special to Git. Other repositories use other names.
A separate pointer. Committing here moves this branch forward. Main stays where it was.
Usually points to the branch you have checked out. That tells Git where your next commit belongs.
Both commits remain in history. Switching changes which committed snapshot you see in the working tree. This model assumes no uncommitted edits.
HEAD → main → A
hello
A → B
main → A
feature/greeting → B
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.
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
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.
You commit B on feature/greeting. Where does main point?
Choose an answer.
Switch between the two branches in your practice repository, then inspect git log --oneline --all --decorate. Can you explain why both commits still exist?
Concepts checked against the official Git book and command documentation. The terminal transcript above is a real local run, not copied output.