This tutorial is out of date and no longer maintained.
If you use Git, you’re probably adding files all the time as you develop your project out further and further. Sometimes though, you might need to actually remove a file from the Git repository but not from your local files.
For example, say you forgot to add a file to .gitignore
but you don’t want to remove it from your local development environment. This could be from a mistake or you didn’t realize that a new package or something created a bunch of nasty log files you don’t want in the repo. Either way, this is actually very easy to do.
This method will remove a single file from your Git repository without deleting the file from your local environment. Then when you run git push
, the files will be removed in the remote repo.
In a similar fashion, you can do this for multiple files at one time.
git rm --cached file1.txt file2.txt file3.txt
This also works recursively for folders so long that you add the -r
flag to the command. Here’s an example:
Continue your learning with how to use .gitignore
and the rare scenarios where you may need to rewrite Git history to remove files with sensitive information.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!