Question

How Can I Remove a File from Git History Without Deleting It Locally?

I accidentally committed and pushed a large file to my Git repository that I don’t want to keep in the repository’s history. However, I still need the file to remain on my local machine.

Is there a way to remove the file from the Git history while keeping it intact on my local machine?


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
October 18, 2024

Hey there! 👋

You’ll want to untrack the file while still keeping it in your local working directory. Run this command:

git rm --cached path/to/your/file

This removes the file from the Git index (the staging area) but leaves it untouched in your local file system. More details on the git rm command can be found in the official Git documentation.

To prevent Git from tracking this file in the future, add it to your .gitignore file:

echo "path/to/your/file" >> .gitignore

Learn more about how .gitignore works and its syntax here.

Now commit this change to remove the file from being tracked:

git add .gitignore
git commit -m "Stop tracking large/sensitive file"

If you want to remove the file from previous commits (history), you can use the filter-branch or BFG Repo-Cleaner. Here’s an example with filter-branch:

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path/to/your/file' --prune-empty --tag-name-filter cat -- --all

This will remove the file from all past commits. Be cautious with this step as it rewrites history. More on rewriting Git history.

After rewriting history, push the changes:

git push origin --force

Again, force-pushing can overwrite shared history, so be careful. Here’s more info on force-push.


Also, if you want a deeper dive into Git and GitHub, check out this free ebook on Git: 📘 Introduction to Git and GitHub

Hope this helps! Let me know if you have any more questions!

- Bobby

Try DigitalOcean for free

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

Sign up

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.