Suppose I create (but do not commit) a file file.txt
, and then type git checkout HEAD
or git checkout HEAD .
. I thought git checkout
basically overwrote your current working files with the snapshot at the commit you give it, so I would have thought this would delete file.txt
. But it doesn't. Why?

- 588
- 5
- 14

- 4,769
- 6
- 43
- 67
-
1Well, because your assumption was wrong. `git checkout` does not affect untracked files. – Sergio Tulentsev Aug 27 '18 at 17:01
-
1Git only manages tracked files, and it works fairly hard to avoid letting you lose data (which is critical). – coreyward Aug 27 '18 at 17:03
-
@SergioTulentsev I don't think that's really a good way to phrase it. "Untracked" just means "not in the staging area right now". `git checkout` will still modify files that differ from how they were in the last commit, even if you haven't staged those changes. – Jack M Aug 27 '18 at 17:06
-
@JackM no, that's not what "untracked" means in git. – Sergio Tulentsev Aug 27 '18 at 17:09
-
@SergioTulentsev It is according to the "Pro Git" book and [this answer](https://stackoverflow.com/a/45292179/815612): "No! Tracked files are files that are in the index *right now*". – Jack M Aug 27 '18 at 17:11
-
@JackM [the git book](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository) disagrees: "Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged. In short, tracked files are files that Git knows about. Untracked files are everything else — any files in your working directory that were not in your last snapshot and are not in your staging area." – Sergio Tulentsev Aug 27 '18 at 17:14
-
@JackM: hah, even simpler, create a new file and do `git status`. You'll see the new file in the "Untracked files" section. – Sergio Tulentsev Aug 27 '18 at 17:25
-
1The Git book isn't really disagreeing: the index is normally populated *via* `git checkout`, so what's in the index *right now* are those that were in the snapshot. But if you *change* the index (with `git rm --cached` or `git add`, for instance), that changes the set of tracked files. Note also that `git checkout
` is a very different command from `git checkout `. Some (including myself) believe it should use a different spelling, i.e., not start with `git checkout` at all. -
@torek: yeah, I had a hard time explaining to my newbie friend why `git checkout` behaves so wildly differently :) – Sergio Tulentsev Aug 27 '18 at 20:49
-
*"git checkout
is a very different command from git checkout "* Oh, that's cool. I'll just be over here, crying. -
@JackM to remove untracked files you can use `git clean`, i've corrected my answer below. – Andrei Prigorshnev Aug 30 '18 at 10:18
2 Answers
git checkout
doesn't overwrite your working copy by-design.
It works in the same way as git reset --hard
but with the important difference - git checkout
is working-directory safe, so it doesn't overwrite existing changes in your working directory. Actually, it’s a bit smarter — it tries to do a trivial merge in the working directory.
So, if you want to discard all of your changes and just get the snapshot from HEAD use git reset --hard HEAD
or just git reset --hard
instead.
But even git reset --hard
doesn't remove your untracked files. To remove untracked files:
- Run
git clean --dry-run
. It just tells you what will be removed. Do it because cleaning is a dangerous command. - Run
git clean --force
to eventually remove your untracked files.
You can find more details about git checkout
and git reset
here and about cleaning here.

- 588
- 5
- 14
file.txt
, being untracked, is "invisible" to Git. If there is another file named file.txt
in the commit you check out, it can be overwritten as a side effect of the check out, but Git won't go out of its way to removed untracked files.

- 497,756
- 71
- 530
- 681
-
So if I delete or modify a file that was in the previous commit, and checkout to that commit, that file is reverted, but this only as a "side effect"? – Jack M Aug 27 '18 at 17:05
-
@JackM Actually, what you'll get in this case is checkout error. Git will force you to do something about the pending change, because it can't know what were your intentions here. Did you simply forget to commit? – Sergio Tulentsev Aug 27 '18 at 17:07
-
@SergioTulentsev That's not true. Git gives me no error, and reverts modified files to their previous state. – Jack M Aug 27 '18 at 17:08
-
@JackM: when doing `git checkout .` or `git checkout `? I was describing the latter. – Sergio Tulentsev Aug 27 '18 at 17:11
-
@SergioTulentsev `git checkout HEAD .` - if I just do `git checkout HEAD` it doesn't do anything, but it doesn't give me an obvious error either (it prints out a message about the modification to the file, though, which I guess is what you mean). – Jack M Aug 27 '18 at 17:15
-