HEAD State Operations
Switching HEAD States
Attached HEAD
In an attached HEAD state, Git points to the latest commit of a specific branch.
## Switch to a branch (attached HEAD)
git checkout main
git checkout feature-branch
Detached HEAD
A detached HEAD occurs when you checkout a specific commit directly.
## Create a detached HEAD
git checkout <commit-hash>
HEAD Movement Strategies
Navigating Commits
gitGraph
commit
commit
commit
branch feature
checkout feature
commit
commit
HEAD Movement Commands
Command |
Operation |
HEAD State |
git checkout |
Move to branch/commit |
Attached/Detached |
git switch |
Branch switching |
Attached |
git reset |
Modify commit history |
Affects HEAD |
Advanced HEAD Manipulation
Relative Commit References
## Move HEAD to previous commits
git checkout HEAD~1 ## One commit back
git checkout HEAD~3 ## Three commits back
Creating Branches from HEAD
## Create new branch from current HEAD
git checkout -b new-feature
Handling Detached HEAD
Warning and Best Practices
## If in detached HEAD, create a branch to save work
git checkout -b save-detached-work
LabEx Pro Tip: HEAD Management
At LabEx, we recommend understanding HEAD states to prevent accidental commit loss and maintain clean repository history.
Common HEAD Scenarios
- Exploring Historical Commits
- Temporary Investigations
- Experimental Branching
- Rollback Operations
Key Takeaways
- HEAD can be in attached or detached states
- Multiple methods exist for HEAD manipulation
- Always create a branch when working in detached HEAD
- Understand the implications of HEAD movements