When should I use rm, git rm, git rm
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, staging directory, commit directory.
rm
a file from my working directory, it only removes it only from my working directory. It that correct? git rm
a file from my working directory, it removes it from all three directories. Correct? git rm --cached
a file, it is removes the file from my staged and commit directories, but leave it in my working directory? git add .
, then git status
shows staged files that have been added, deleted, and updated. What happens when I commit? Are the deleted files removed from the Commit directory? If I roll back to that commit later, will those deleted files reappear? Any help to better understand these concepts would be appreciated -thanks!
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 only git 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 history git 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.