Git: Beginner to Advanced
Branching
Work on an idea without changing the main line.
Lesson 4 of 12
Reading: about 7 min
Lesson + practice: about 15 min
Difficulty: Intermediate
On this page5 sections
Work on an idea without changing the main line. Follow the animated example, then use the sections below to review each idea.
Visual walkthrough
Use the player controls or jump menu to inspect each step. The diagram changes as the example moves forward.
The player shows one focused example. The sections below explain the ideas behind each topic.
A branch lets you try an idea without touching the version that already works. You can commit freely on the branch, switch back to main at any time, and only bring the work across when it is ready.
This lesson uses a small project: a study planner with a README.md and an app.js. You will add a search feature on its own branch while main carries on, which builds the same graph the walkthrough above animates. Commit IDs in your own repository will differ from the ones shown here.
Branch references and HEAD
A branch is not a copy of your files. It is a name that points at one commit, stored in a tiny file inside .git. When you commit on a branch, Git moves that name forward to the new commit.
HEAD answers the question “where am I?”. Normally it points at a branch name, and the branch points at a commit. That is how Git knows which branch to move when you commit.
See where you are
git branch lists your branches and marks the current one with *. The .git/HEAD file shows the same thing from Git's side: HEAD refers to main. git rev-parse prints the commit that main points at.
git branch
cat .git/HEAD
git rev-parse --short main$ git branch
* main
$ cat .git/HEAD
ref: refs/heads/main
$ git rev-parse --short main
66db61fCreate and switch branches
git switch -c creates a branch at your current commit and moves you onto it in one step. Give it a name that says what the work is for, such as search or fix-login-typo.
Create a branch and move onto it
After the switch, HEAD refers to search. Both main and search point at the same commit, so your files have not changed yet.
git switch -c search
cat .git/HEAD
git branch$ git switch -c search
Switched to a new branch 'search'
$ cat .git/HEAD
ref: refs/heads/search
$ git branch
main
* searchCommit on the branch
The commit goes onto search. --all --decorate shows every branch and where it points: search has moved to the new commit, while main has stayed behind.
git add search.js
git commit -m "Add search"
git log --oneline --all --decorate$ git add search.js
$ git commit -m "Add search"
[search a0617f9] Add search
1 file changed, 1 insertion(+)
create mode 100644 search.js
$ git log --oneline --all --decorate
a0617f9 (HEAD -> search) Add search
66db61f (main) Describe the project
5b0bb3a Add README and app| Command | What it does |
|---|---|
| git branch | List branches; * marks the current one |
| git branch name | Create a branch, but stay where you are |
| git switch name | Move to an existing branch |
| git switch -c name | Create a branch and move to it |
| git checkout -b name | Older equivalent of git switch -c, still common in tutorials |
$ git switch main
Switched to branch 'main'
M app.jsIf the switch would overwrite your edit, Git refuses instead, so nothing is lost:
$ git switch search
error: Your local changes to the following files would be overwritten by checkout:
README.md
Please commit your changes or stash them before you switch branches.
AbortingBefore switching, run git status. Commit the work, or put it aside with git stash (covered in Advanced tools), so each branch keeps only its own changes.
Diverging histories
Switch back to main and the search work disappears from your folder. It is not lost: it lives on the search branch. Git shows you the files of the commit you are on.
Return to main
search.js is not listed because it was only committed on search.
git switch main
ls$ git switch main
Switched to branch 'main'
$ ls
README.md
app.jsNow commit something on main too. Each branch has a commit the other does not have, so their histories have diverged.
See two lines of work
Read the graph from the top. Each * is a commit. The | and / lines show Add search branching off after Describe the project, while main gained Add start instructions. git branch -v shows the latest commit on each branch.
git commit -am "Add start instructions"
git log --oneline --graph --all
git branch -v$ git commit -am "Add start instructions"
[main 48c0968] Add start instructions
1 file changed, 1 insertion(+)
$ git log --oneline --graph --all
* 48c0968 Add start instructions
| * a0617f9 Add search
|/
* 66db61f Describe the project
* 5b0bb3a Add README and app
$ git branch -v
* main 48c0968 Add start instructions
search a0617f9 Add searchDiverged branches are normal. The next lesson, Merging, shows how to bring them back together.
Feature branch practice
Try the whole cycle in any practice repository. It takes about five minutes:
- Run
git switch -c practiceto start a branch. - Create or edit a file, then
git addandgit commitit. - Run
git log --oneline --all --decorateand check that onlypracticemoved. - Run
git switch mainand confirm your change is not in the folder. - Run
git switch practiceand see it come back.
Rename and delete branches
-m renames a branch. -d deletes a branch only when its work is already merged into your current branch, so the empty typo branch goes. Git refuses to delete add-search because its commit exists nowhere else, which protects you from losing work. The exact wording varies slightly between Git versions.
git branch -m search add-search
git branch typo
git branch -d typo
git branch -d add-search$ git branch -m search add-search
$ git branch typo
$ git branch -d typo
Deleted branch typo (was 48c0968).
$ git branch -d add-search
error: the branch 'add-search' is not fully merged.
If you are sure you want to delete it, run 'git branch -D add-search'You are on main and run git switch -c fix. Which commit does fix point to, and which branch are you on?
Reveal answer
fix points to the same commit as main, because a new branch starts where you are. You are now on fix: HEAD refers to it, and your next commit will move fix, not main.
You commit on a branch called search, then switch to main. Your new file has vanished. Is it lost?
Reveal answer
No. The file is in the commit on search. On main you see that branch's files. Run git switch search and the file is back.
Your progress
Reveal the check answer or reach the end to complete this lesson.