0
0
Gitdevops~20 mins

Detached HEAD state in Git - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Detached HEAD Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What happens in Git when you enter a detached HEAD state?

In Git, what does it mean when you are in a detached HEAD state?

AYou have checked out a specific commit, not a branch, so commits won't update any branch.
BGit has lost track of your repository and you cannot commit.
CYou are on a branch and can commit changes normally.
DYou are in a merge conflict state and must resolve conflicts before committing.
Attempts:
2 left
💡 Hint

Think about what HEAD points to when detached.

💻 Command Output
intermediate
2:00remaining
Output of git status in detached HEAD state

What is the typical output of git status when you have checked out a commit directly (detached HEAD)?

Git
git checkout 1a2b3c4d

git status
A
HEAD detached at 1a2b3c4d
nothing to commit, working tree clean
Bfatal: You are not currently on a branch.
C
You are currently rebasing branch 'master' on 'origin/master'.
All conflicts fixed but you are still merging.
D
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Attempts:
2 left
💡 Hint

Detached HEAD shows the commit hash in the status.

🔀 Workflow
advanced
2:30remaining
How to save work done in detached HEAD state

You made commits while in a detached HEAD state. How do you save these commits to a branch so they are not lost?

ARun <code>git reset --hard HEAD~1</code> to save commits.
BRun <code>git checkout master</code> to automatically save commits to master.
CRun <code>git branch new-branch</code> to create a branch at the current commit.
DRun <code>git merge detached</code> to merge detached commits.
Attempts:
2 left
💡 Hint

Think about how to create a branch from the current commit.

Troubleshoot
advanced
2:00remaining
Why does git push fail in detached HEAD state?

You are in a detached HEAD state and try to push your commits with git push origin HEAD. The push fails. Why?

ABecause your remote repository is down.
BBecause HEAD is not a branch, Git cannot push it without a branch name.
CBecause you have uncommitted changes.
DBecause you need to run <code>git fetch</code> first.
Attempts:
2 left
💡 Hint

Consider what HEAD points to in detached state and how push works.

Best Practice
expert
3:00remaining
Best practice to avoid losing commits in detached HEAD

What is the best practice to avoid losing commits made in a detached HEAD state?

AUse <code>git stash</code> to save commits made in detached HEAD state.
BReset HEAD to master after committing in detached HEAD state.
CNever commit in detached HEAD state; only commit on branches.
DAlways create a new branch immediately after making commits in detached HEAD state.
Attempts:
2 left
💡 Hint

Think about how to keep commits safe and accessible.