Branching in Git
This article belongs to the Git Basics to Advanced course, which is currently being revisited with updated architectural patterns and content. Content may be refined shortly.
Branching allows developers to work on new features or fixes without affecting the main branch.
# Setting up a demo project
mkdir project
cd project
git init
echo newfile > file
git add file
git commit -m "new file added"
git log
1. Creating and Switching Branches
In Git, a branch is simply a lightweight, movable pointer to a commit hash. Creating or switching branches is instantaneous in O(1) time.
git branch newfeature # Create a new branch
git status
git branch # List all branches (* marks active branch)
git checkout newfeature # Switch to the new branch
Shortcut to create and switch in one command:
git checkout -b newfeature
2. Interactive Branching & Switching Explorer
Step through the interactive timeline below to see how branch pointers advance and how HEAD tracks your active branch:
Branching and Switching Explorer
Step 1 of 4: Initial Commit on master
$ git status Repository currently has commit C1 with master and HEAD pointing to C1.
3. Visualizing Branches & Commits
To inspect branches and commit topology in your terminal:
git log --decorate --graph --oneline --all
Note:
HEADis where Git is currently pointing in your repository, and a branch is a named pointer pointing to a commit snapshot.
4. Detached HEAD
Normally, HEAD points to a branch name (e.g., HEAD -> master), but it can also point directly to a specific commit hash, putting Git into a “detached HEAD” state.
For example, checking out an earlier commit:
git checkout d6afca4 # HEAD is now detached


Explore the detached HEAD lifecycle interactively:
Detached HEAD Lifecycle
Step 1 of 4: Attached HEAD on master
$ git log --oneline Normal state: HEAD points to the master branch pointer, which points to commit C3.
What Happens When You Commit in Detached HEAD?
If you make modifications and commit them while detached:
git add .
git commit -m "Changes made on Detached Head"
git log --graph --oneline --all


If you switch back to master without creating a branch, this commit has no branch pointing to it and becomes an orphaned commit that may eventually be garbage collected:
git checkout master
To keep work created while detached, attach a branch before or after switching:
git branch my-saved-feature # Points a new branch at the detached commit
5. Deleting a Branch
git branch -d newfeature # Safe delete (only deletes if already merged)
git branch -D newfeature # Force delete (deletes even if unmerged changes exist)
git push origin --delete newfeature # Delete remote branch on GitHub
6. Git Tags
Tags point to specific commits and are used as permanent, fixed release labels (such as v1.0.0 or v2.1.0):
git tag v1.0.0 # Lightweight tag on current commit
git tag -a v1.0.0 -m "Release version 1.0.0" # Annotated tag
git push origin --tags # Push tags to remote