git rm: removing files from a repository
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
If you simply delete a file (rm old.txt), git sees it as a change that still has to be committed. git rm does both steps at once: it deletes the file and puts the deletion into the index.
git rm old.txt
git commit -m "Remove an old file"
A deleted file doesn't vanish without a trace: it stays in the history, and you can get it back from an old commit.
Useful variants:
git rm --cached secret.env # stop tracking, but keep the file on disk
git mv old.py new.py # rename (on your own computer)
--cached is a lifesaver if a secret or a big file got into the repository by accident. Don't forget to add it to .gitignore straight away.
Task. Remove the file old.txt from the repository with git rm and commit with the message remove old file. The file app.py must stay.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.