git commit takes the snapshot by
- looking at the index (where you have added the file),
- not by looking at the working tree (where you go on modifying stuff, including adding or deleting files)
See "What's the difference between HEAD, working tree and index, in Git?"
In your case, after deleting the file (but after adding it to the index), a git status
would give you:
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: go.mod
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: go.mod
The file is both:
- ready to be part of the next commit
- locally deleted
A git restore -- <myFile>
is enough to restore it locally.
The working tree (or working directory) is the tree of actual checked out files.
The working tree normally contains the contents of the HEAD commit’s tree, plus any local changes that you have made but not yet committed.
The idea is to prepare your next commit, instead of blindly putty all your current modification into one giant commits.
It is better to make small coherent commits instead of a giant one, for getting a logical history, and making future git bisect
easier.
You can even stage (add to index) part of a file (interactive staging)
The OP adds:
Imagine that commit does the work of both the actual commit and add.
Let's call it the imaginary commit.
You can still do this little by little work using the imaginary commit
First: that command (which adds all and commit) does exist:
git commit -am "Let's add everything"
Second, to do "little by little", you must use git add, then commit.
A commit takes everything in the index.