One thing about Git that I'm trying to understand is how it deals with files which aren't tracked, or which weren't tracked but now are committed.
Here's an example:
echo "one" > one.txt
git add one.txt
git commit -m "#1"
So, "one.txt" is in the first commit. Now, I'll create two more files, but only add one of them (and I'll tag this next commit as "#2" so that we can return to it):
echo "two" > two.txt
echo "three" > three.txt
git add two.txt
git commit -m "#2"
git tag "#2"
So, "two.txt" is in the 2nd commit, and "three.txt" is just this extra file, hanging around. Then, I checkout HEAD~ and, sure enough, Git removes the file "two.txt", because that didn't exist back then...
git checkout HEAD~
ls
___ one.txt three.txt
Okay.. back to the end of the branch, and only add the third file. So, we're only tracking it. Then let's step backward one commit...
git checkout "#2"
git add three.txt
git checkout HEAD~
ls
___ one.txt three.txt
Ummm... okay... so just tracking a file isn't enough to have Git manage whether it's there or not. So, we go to the end of the branch, again, and commit, then back up (twice, this time, to get back to commit #1) and what do we see?
git checkout "#2"
git commit -m "#3"
git checkout HEAD~~
ls
___ one.txt
This time, Git has deleted the file "three.txt". So, my question is: Can someone describe just how Git decided to do that? It doesn't seem to be simply whether a filename appears in any tree object in the respository, because I can create a new three.txt (a filename which is being tracked and has a committed version in the respository, and then do more checkouts, and Git leaves it alone, again, like before.
Can someone explain how Git decides what is okay to delete when doing checkouts?