DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How to Make Git Forget a Tracked File Now in .gitignore

How to Make Git Forget a Tracked File Now in .gitignore

Let's look at how to remove a tracked file from git after it has been added to .gitignore

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Jun. 18, 22 · Web Dev Zone · Code Snippet
Like (7)
Save
Tweet
4.64K Views

Join the DZone community and get the full member experience.

Join For Free

When we track a file in git, it can sometimes get cached and remain tracked, even if we add it to our .gitignore file. This is simply because .gitignore prevents files from being added to Git's tracking system, but it will not actively remove those that are already tracked. This can lead to issues when you have something you no longer want to be tracked, but can't seem to remove it from your git repository.

Fortunately, there is an easy way to fix this. git has a built-in rm function which lets us remove cached or tracked changes. To run it, you can use the following command to remove a specific file, where [filename] can be removed with the file you wish to stop tracking:

Shell
 
git rm --cached [filename]

Similarly, if an entire directory needs to be removed, use the -r flag which means recursive, to remove an entire directory and everything within it from tracking:

Shell
 
git rm -r --cached [directory]

After running this command, you can then add amend your commit and push it to your remote:

Shell
 
git add .
git commit -m "Removed tracked files which shouldn't be tracked"
git push

NOTE this will not remove files from your local computer, but it will remove tracking of files from your git repository. It will also remove the files from other developer's computers or servers upon your next git pull. Be careful with this command, as I accidentally deleted all images on FJOLT using it a couple of weeks ago. :)

Git

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Steps to Create a Successful Agile Release Plan
  • API Security Weekly: Issue 165
  • Introduction to Word Vectors
  • Monolith vs Microservices Architecture: To Split or Not to Split?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo