Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

How does HEAD~1 work in Git?

HEAD~1 is a special commit identifier in git; it stands for the previous commit (not the one we just made, but the one before that). Can you use that identifier to check out the previous commit in this repository?

Git seems to be the most challenging course for me so far.

I have not seen this command be used yet, I tried the follow:

git commit HEAD~1 and different variations. I can't seem to get it.

Anyone?

2 Answers

Hey Jonathan Mussogit checkout HEAD~1 is what you're looking for. It will put you in "detached head" state which means you're not part of any branch. git checkout master will bring you back.

On question 9 of 11 in an old-fashioned git together, I am using every variation of the HEAD-1 identifier possible, but it still gives me an error that says I am using the wrong identifier. I have the git command right, so I'm not sure what the issue is. My code is identical to yours above.

git checkout HEAD-1

I only have my iPad right now, but I tried it on the app and also through my web browser. Same issue.

?

Never mind. I figured it out. You guys should definitely make a note though that it isn't a hyphen or a dash in between the identifier and it's index.

Even with my nerd glasses on I just barely noticed after staring at it for hours.

It does seem to make sense that to go back a commit, you'd write HEAD -1 (i.e. "head minus one"). Unfortunately, that's just one of the pecularities of git's implementation! Glad you figured it out Thomas Wager :zap:

Tim Burgess
Tim Burgess
2,552 Points

I wouldn't be too fussed about not understanding HEAD~1. You could use git for many months without needing to know it. And if you are using Github, they have a much nicer web interface to use for looking at the history of code changes.