By william09sir
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?
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!
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.