Git Branch
A branch is an independent line of development. You create one to build a feature or try an idea without disturbing the main code; if it works out you merge it back, and if not you simply delete it.
Technically, a branch is tiny: a file containing the hash of one commit. When you commit on a branch, Git moves that pointer forward to the new commit. HEAD points to the branch you are currently on.
main
|
A --- B --- C HEAD -> login
\ |
D --- E loginCreating and switching
| Command | Does |
|---|---|
git branch |
list local branches (* marks the current one) |
git branch login |
create a branch at the current commit (does not switch) |
git switch login |
switch to an existing branch |
git switch -c login |
create and switch in one step |
git checkout -b login |
the older equivalent of switch -c |
mkdir site && cd site && git init -q
echo "<h1>Home</h1>" > index.html && git add . && git commit -q -m "Add home page"
git branch login
git branch
git switch login
git branch
echo "<form>login</form>" > login.html
git add login.html && git commit -q -m "Add login form"
git log --oneline --all login
* main
Switched to branch 'login'
* login
main
f2ed816 Add login form
0bbdda0 Add home page
login now has one commit that main doesn't have.
Switching changes your files
When you switch branches, Git rewrites the working folder to match the branch's latest commit. Files that exist only on the other branch disappear (they are safe in the history) and come back when you switch again:
mkdir site && cd site && git init -q
echo "<h1>Home</h1>" > index.html && git add . && git commit -q -m "Add home page"
git switch -q -c login
echo "<form>login</form>" > login.html && git add . && git commit -q -m "Add login form"
ls
git switch -q main
ls
git switch -q login
lsindex.html
login.html
index.html
index.html
login.html
Note: If you have uncommitted changes that would be overwritten, Git refuses to switch. Commit them, stash them (
git stash), or discard them first.
Seeing branches in the log
git log --oneline --graph --all draws every branch. git branch -v shows each branch's latest commit:
mkdir site && cd site && git init -q
echo "home" > index.html && git add . && git commit -q -m "Add home page"
git switch -q -c login
echo "login" > login.html && git add . && git commit -q -m "Add login form"
git switch -q main
echo "footer" >> index.html && git commit -q -am "Add footer"
git branch -v
git log --oneline --graph --all login d041fbd Add login form
* main b3888e2 Add footer
* d041fbd Add login form
| * b3888e2 Add footer
|/
* ff139b4 Add home page
The two branches have diverged: each has a commit the other lacks. Merging (next lesson) brings them back together.
Renaming and deleting
mkdir site && cd site && git init -q
echo "home" > index.html && git add . && git commit -q -m "Add home page"
git branch experiment
git branch -m experiment dark-theme
git branch
git switch -q dark-theme
echo "dark" > theme.css && git add . && git commit -q -m "Try dark theme"
git switch -q main
git branch -d dark-theme
echo "exit status: $?"
git branch -D dark-theme
git branch dark-theme
* main
error: the branch 'dark-theme' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D dark-theme'
hint: Disable this message with "git config set advice.forceDeleteBranch false"
exit status: 1
Deleted branch dark-theme (was 5f209ca).
* main
-d refuses to delete a branch whose commits are not merged anywhere, which protects you from losing work. -D forces the deletion. (Even then the commits are recoverable for a while through the reflog.)
Naming branches
Names like feature/search, fix/login-crash or docs/readme describe the work and group nicely in tools. Avoid spaces, and keep main for the stable code.
Test yourself
What is a Git branch, technically?
- A full copy of the project folder
- A movable pointer to a commit
- A zip archive of changes
- A tag that never moves
Which command creates a branch called search and switches to it?
- git branch search
- git switch search
- git switch -c search
- git merge search
git branch -d old fails with "not fully merged". What does that mean?
- The branch doesn't exist
- The branch has commits that would be lost; use -D to force
- You are on that branch
- The remote is offline