Tutorial

How to Use “git Remove” Without Deleting the Files

Draft updated on Invalid Date
author

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 our products

About the author(s)

Category:
Tutorial
Tags:

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment
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!

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

Please complete your information!

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.