FAQ: How to Backtrack - git checkout

This community-built FAQ covers the “git checkout” exercise from the lesson “How to Backtrack”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Git

FAQs on the exercise git checkout

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Im having a difficult time trying to figuring out how im using the :
_git diff _ command.

it works until I modify the text of the scene-5.txt Once I change the text and run the git diff I get nothing… any Ideas?

42%20AM

The command - “git checkout HEAD filename” does not discard the changes in the working directory.
can someone explain this?

7 Likes

I had to close and re-open the file before it would show the changes were discarded:

  1. Click the “X” next to scene-5.txt
  2. Click Open File under Code Editor
  3. Select scene-5.txt
9 Likes

Hi. After using the git checkout command, the changes are not show in the working directory. This is despite using the process outlined by tensor2. Any suggestions?

Close and re-open the file to show the changes.

Is there any reason the lesson is having us specify HEAD with the checkout command? Can’t it be omitted since the HEAD is targeted by default?

1 Like

Where am I supposed to see the changes after the ‘git checkout HEAD scene-5.txt’?

Try re-opening the file (use the wee folder icon near the tab with the file name). The file itself should have changed but like most text editors an open document is buffered or stored in a temporary folder somewhere so changing it in the background won’t update the text unless it’s reopened (when it’s loaded from the file again).

That worked. Thanks alot for your help!

the command is:
git diff filename

What does the HEAD part of this command do? why do we specify HEAD?

Also, why doesn’t this work unless we specify a specific file? Why shouldn’t it change our whole working directory?

HEAD usually refers to most recent commit on your current branch (it can point elsewhere but only if you deliberately make it so). In this case you can just treat it as the last commit you made.

You can reset the entire directory but you tend not to use checkout for just that purpose (unless changing to historical commits but even that does not remove local changes). git reset tends to work better for the entire repo and checkout for single files or changing to historical commits. By default it won’t remove all recent changes (or newly tracked files) and you’d need to force it but that only affects tracked files anyway. Git really tries to stop accidental overwrites of local changes, you have to be very explicit about it.

Long story short, this is a valid usage of checkout that is worth knowing. If you’ve made numerous changes that you 100% want to remove I’d tend to use reset with certain flags instead.

thanks, but why are we specifying HEAD? By specifying HEAD, are we saying “rollback this file to the way it was in this (HEAD) commit”?

Ah sorry if that wasn’t clear, yes that’s right. You could change the file to any commit by specifying the exact SHA, e.g. git checkout 3ff8xa11 file.txt.

Instead of having to find and type out part of the sha for your current commit, HEAD is just more convenient.

1 Like

Why is the command called so peculiarly?

git checkout HEAD changes.txt

Why “checkout”? Why “HEAD”? Why not, say

git rollback changes.txt