Git Branching
Understand Git branches, HEAD, switching branches, deleting branches, and detached HEAD with practical beginner examples.
What a Branch Really Is
A branch in Git is a movable pointer to a commit. Instead of copying your whole project into a new folder, Git lets you create a lightweight branch and continue work there.
This is one of Git’s biggest strengths. You can build a feature, fix a bug, or test an idea without disturbing the main line of development.
Tip: A branch is not a second copy of the project. It is a label that points to a specific place in commit history.
Why Branching Matters
Imagine the main branch contains stable application code. You want to add a login page, but the work may take several commits. Instead of changing main directly, you create a feature branch.
Benefits of branching:
- isolate work in progress
- reduce risk to stable code
- make code reviews easier
- support multiple parallel changes
In real teams, short-lived feature branches are common because they keep the shared branch clean.
Viewing Branches
To list local branches:
git branch
The current branch is marked with *.
To see both local and remote branches:
git branch -a
A typical result might be:
* main
feature/login
remotes/origin/main
Creating a Branch
Create a new branch called feature/login:
git branch feature/login
This creates the branch, but you stay on your current branch until you switch.
To create and switch in one step, use:
git switch -c feature/login
Older tutorials may use git checkout -b feature/login, which still works, but git switch is clearer for branch changes.
Switching Between Branches
Move to an existing branch:
git switch main
Switch back:
git switch feature/login
When you switch branches, Git updates your working directory to match the selected branch’s latest commit.
Real Example
git switch -c feature/navbar
echo "Add navigation" >> README.md
git add README.md
git commit -m "Document navbar plan"
git switch main
After switching back to main, that change is no longer visible there because it belongs to the feature branch.
Understanding HEAD
HEAD is Git’s way of saying “your current position.” Most of the time, HEAD points to the branch you are currently on.
If you are on main, HEAD points to main, and main points to the latest commit on that branch.
You can inspect recent history with:
git log --oneline --decorate -5
You may see output like:
7fa12cd (HEAD -> feature/login) Add login form markup
1ab93e0 (main) Create app skeleton
That means HEAD currently points to feature/login.
Deleting a Branch
After a branch has been merged and is no longer needed, delete it to keep the repository tidy.
Delete a fully merged branch safely:
git branch -d feature/login
Force delete a branch that has unmerged commits:
git branch -D feature/login
Note: Use
-Dcarefully. It can remove a branch pointer before its work is merged elsewhere.
Detached HEAD Explained
A detached HEAD happens when HEAD points directly to a commit instead of a branch.
For example:
git checkout 1ab93e0
Now you are no longer “on” main or feature/login. You are looking at a specific past commit.
This can be useful for inspection, testing, or reproducing an old state. But if you make commits here, they are not attached to a branch unless you create one.
Safe Way to Continue from Detached HEAD
If you decide to keep that work, create a branch immediately:
git switch -c investigate-old-release
That gives the new commits a proper branch name and keeps them easy to find.
Good Branch Naming Habits
Teams usually prefer simple, descriptive names such as:
feature/login-pagefix/pipeline-timeoutdocs/git-tutorialhotfix/rollback-script
A good branch name helps everyone understand the purpose of the work.
A Practical Branch Workflow
Here is a realistic beginner workflow:
git switch main
git pull
git switch -c feature/add-footer
echo "Footer content" >> index.html
git add index.html
git commit -m "Add footer to homepage"
git switch main
At this point, main stays stable while the new work lives on feature/add-footer until it is reviewed and merged.
What You Should Remember
Branches are lightweight and powerful. Use them to isolate work, protect stable code, and make collaboration easier. HEAD represents your current location in history. Most of the time it points to a branch, but during detached HEAD it points directly to a commit.
If you remember one best practice, remember this: create a branch before doing meaningful new work.
Test Your Understanding
Let's see how well you understood the concepts! These exercises will help reinforce what you just learned.
Exercise 1: Creating isolated work
Scenario: main is stable and you want to build a new navbar without disturbing it.
Question: Which command both creates and switches to the feature branch in one step?
Exercise 1: Creating isolated work
Choose the one-step command for creating and switching to a new branch.
Exercise 2: Reading HEAD correctly
Question: In git log --oneline --decorate, you see (HEAD -> feature/login) next to the latest commit. What does that mean?
Exercise 2: Reading HEAD correctly
Interpret a decorated log entry where HEAD points to a branch.
Exercise 3: Escaping detached HEAD safely
Scenario: You checked out an old commit to investigate a bug and now want to keep new work you started there.
Question: What is the safe next step from the lesson?
Exercise 3: Escaping detached HEAD safely
Choose the action that preserves new work created from a detached HEAD state.