Tutorial

How to Use “git Remove” Without Deleting the Files

Draft updated on Invalid Date
Default avatar

By Nicholas Cerminara

How to Use “git Remove” Without Deleting the Files

This tutorial is out of date and no longer maintained.

Introduction

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.

Removing a single file with Git without deleting it

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.

  1. git rm --cached filexample.txt

Removing multiple files with Git without deleting them

In a similar fashion, you can do this for multiple files at one time.

git rm --cached file1.txt file2.txt file3.txt

Removing an entire directory without deleting the files

This also works recursively for folders so long that you add the -r flag to the command. Here’s an example:

  1. git rm -r --cached folder

Conclusion

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.

Learn more about us


About the authors
Default avatar
Nicholas Cerminara

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel