Git rm

Delete File Already In Repo

We can simply use the git rm command to removed the file. It will delete the file from the file system for us. The file isn't exactly deleted from the repo immediately. We have to then commit the changes.

#Stage the file for delete
git rm a.txt
#file commit the delete
git commit -m "deleted a.txt"

We can also delete the file directly from the file system(working copy). Git will detect the deleted file.

 

#Stage the file for delete
git rm a.txt
#file commit the delete
git commit -m "deleted a.txt"

 


"Git rm" is used to removed a file. This command cannot removed untracked files. Use a regular rm command for that.

remove the file from the working directory and stage it for removal from the repository. The file will be removed from the repository on the next commit.

#where myfile.txt is a file that has already been committed
git rm myfile.txt

In case we removed a file. We can recover it if we haven't committed the changes yet with

git checkout HEAD -- myfile.txt

Remove a file that has been staged for commit but not yet committed
 

git rm --cached myfile.txt

 

Cookbook Category: