0
0
Gitdevops~5 mins

HEAD pointer concept in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you work with Git, HEAD is like a bookmark that shows which version of your project you are currently looking at or working on. It helps Git know where you are in your project's history so you can make changes or look at past versions easily.
When you want to see which branch or commit you are currently working on.
When you want to switch to a different branch or commit in your project.
When you want to create a new branch starting from your current position.
When you want to understand why your changes are not appearing as expected.
When you want to reset your working files to a previous state.
Commands
This command shows the current branch and the state of your working directory. It tells you where HEAD is pointing by showing the branch name.
Terminal
git status
Expected OutputExpected
On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
This command lists all branches and marks the one where HEAD currently points with an asterisk (*).
Terminal
git branch
Expected OutputExpected
* main feature-1 bugfix
This command moves HEAD to point to the 'feature-1' branch, switching your working files to that branch's latest commit.
Terminal
git checkout feature-1
Expected OutputExpected
Switched to branch 'feature-1'
This command shows the latest commit where HEAD currently points, helping you confirm your current position in the project history.
Terminal
git log -1 --oneline
Expected OutputExpected
a1b2c3d Update feature-1 with new changes
-1 - Show only the latest commit
--oneline - Show commit in a short, one-line format
Key Concept

If you remember nothing else, remember: HEAD is the pointer that tells Git which commit or branch you are currently working on.

Common Mistakes
Trying to make changes without knowing which branch HEAD points to.
You might accidentally change the wrong branch or commit, causing confusion or lost work.
Always check your current branch with 'git status' or 'git branch' before making changes.
Assuming HEAD always points to a branch instead of a commit.
HEAD can point directly to a commit (detached HEAD), which means changes might be lost if not handled properly.
Understand when HEAD is detached and create a branch if you want to keep changes.
Summary
Use 'git status' or 'git branch' to see where HEAD is pointing.
HEAD points to the current branch or commit you are working on.
Switch branches with 'git checkout' to move HEAD to a different branch.