How to make Git forget a tracked file now in .gitignore

How to make Git forget a tracked file now in .gitignore

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:

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:

git rm -r --cached [directory]

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

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 developers 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. 🙂


How to make Git forget a tracked file now in .gitignore was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Johnny Simpson

How to make Git forget a tracked file now in .gitignore

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:

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:

git rm -r --cached [directory]

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

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 developers 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. :)


How to make Git forget a tracked file now in .gitignore was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Johnny Simpson


Print Share Comment Cite Upload Translate Updates
APA

Johnny Simpson | Sciencx (2022-05-11T11:23:26+00:00) How to make Git forget a tracked file now in .gitignore. Retrieved from https://www.scien.cx/2022/05/11/how-to-make-git-forget-a-tracked-file-now-in-gitignore/

MLA
" » How to make Git forget a tracked file now in .gitignore." Johnny Simpson | Sciencx - Wednesday May 11, 2022, https://www.scien.cx/2022/05/11/how-to-make-git-forget-a-tracked-file-now-in-gitignore/
HARVARD
Johnny Simpson | Sciencx Wednesday May 11, 2022 » How to make Git forget a tracked file now in .gitignore., viewed ,<https://www.scien.cx/2022/05/11/how-to-make-git-forget-a-tracked-file-now-in-gitignore/>
VANCOUVER
Johnny Simpson | Sciencx - » How to make Git forget a tracked file now in .gitignore. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/11/how-to-make-git-forget-a-tracked-file-now-in-gitignore/
CHICAGO
" » How to make Git forget a tracked file now in .gitignore." Johnny Simpson | Sciencx - Accessed . https://www.scien.cx/2022/05/11/how-to-make-git-forget-a-tracked-file-now-in-gitignore/
IEEE
" » How to make Git forget a tracked file now in .gitignore." Johnny Simpson | Sciencx [Online]. Available: https://www.scien.cx/2022/05/11/how-to-make-git-forget-a-tracked-file-now-in-gitignore/. [Accessed: ]
rf:citation
» How to make Git forget a tracked file now in .gitignore | Johnny Simpson | Sciencx | https://www.scien.cx/2022/05/11/how-to-make-git-forget-a-tracked-file-now-in-gitignore/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.