Course lesson

Explore Old Commits with a Detached HEAD, and then Recover

To poke around in old code, we can checkout the hash of an old commit with git checkout [HASH] - but then we'll be in a "detached HEAD" state.

Duration
1 min
Access
Free
Transcript
Retained from source evidence

To poke around in old code, we can checkout the hash of an old commit with git checkout [HASH] - but then we'll be in a "detached HEAD" state.

Detached HEAD just means that HEAD is not pointing to a branch. That's problematic if we want to save the work that we do in that state - so we first have to make a new branch where we are with git checkout -b my-new-branch

Then, any changes we make can be committed to that new branched and saved.

Terminal
# checkout the hash of an old commit
git checkout [HASH]

# we'll be in a "detached HEAD" state
# Save the work by creating a new branch
git checkout -b my-new-branch

✏️ Edit on GitHub