I am learning git, but I am confused by different ways of staging and committing files. To wrap my head around this I’m using a metaphor of directories: working directory, s
Tweak your understanding of the staging area (also known as the index or cache) and the --cached
option. The documentation for git rm states
--cached
Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
Running down your list gives
rm file
— remove file from the work directory onlygit rm
— remove file from the work directory and from the staging area, but not yet part of the history (repository, “commit directory”)git rm --cached
— remove from staging area but neither the work directory nor the historygit add .
in the presence of modifications, new files, and deleted files — git will record the modifications and new unignored files in the cache. (git add will behave differently with certain options.)The --cached
option to various git commands causes them to operate on the index or at least with respect to the index.
git add
and git rm
take changes from the work directory to the index or cache. Think of these commands as building up your next commit a piece at a time.
After you are happy with what’s in the index, move changes from the index to the repository with git commit
.
Most of the time, what you want is the simple sequence git rm file
followed by git commit
to stop tracking file at the current point in your history.