Git checkout HEAD without filename


I am currently working on FullStack Engineer>Git and GitHub, Part1>How to backtrack in Git> Poem Fiasco project.

For the first question, "The file road-not-taken.txt doesn’t look right at all! Perhaps a rival poet snuck in and changed it while you were getting coffee. Good thing you’ve been committing often. * Checkout the HEAD version of road-not-taken.txt to discard changes made to the working directory. ", I did git checkout HEAD in the beginning, which gave me the following lines.

M oven-bird.txt
M road-not-taken.txt

I understand now that I should have added the filename after HEAD. However, I am wondering what the meaning of the result I previously got is. what does the letter M signify here and why is it listing these files? Thanks a lot in advance for any help!

I’m assuming the command didn’t actually change your working directory at all. What you’re seeing is similar to what you’d see with git status namely the differences between the last commit and the working directory (M typically signifies modified in this case).

Although git switch is now suggested to change branches it’s the same output you’d get as if you tried to change the same branch using checkout.

For example whilst on the master branch (with two example files that have been changed since last commit)-

git checkout master
M    file.x
M    file2.y
Already on 'master'

I’m surprised there isn’t an equivalent warning, e.g. “Already at HEAD” or a warning about local changes being overwritten so stash or commit them. Had a brief play with this and you can force the checkout for all files with git checkout -f HEAD so the behaviour isn’t too surprising but the lack of error message is. Perhaps I’m missing something but hopefully that covered your initial question anyway.

1 Like