Remove an Existing File from a Git Repo

Git

Git Problem Overview


I want git to stop tracking my local development log (log/development.log) in our repositories. Is this possible and how can I do it?

Git Solutions


Solution 1 - Git

Just to give the full answer all at once:

  1. from klemens: You need to add the file to your .gitignore file somewhere above the undesired file in the repo. i.e.

    $ cd $ cat >> .gitignore development.log C-d

  2. from m. narebski: You then need to remove the file from the repo by executing "git rm --cached <file> and then committing this removal"

If you were also hoping to make the repo look as if it had never tracked that file, that is much more complicated and highly discouraged as it not only creates brand new commits for every single commit in your history, thus destroying interoperability in a nasty way between other people who have cloned your repo, but it also leaves every one of those commits untested (assuming you test your commits before making them).

With that caveat in mind, the tool you're looking for if that's your goal is filter-branch. The first example does exactly what I'm describing.

Solution 2 - Git

To stop tracking the file, simply use git rm --cached <filename>.

But as pointed out by the others here, adding the file to .gitignore might prevent you from accidentally adding it again later.

Solution 3 - Git

add the gitignore file.

see http://git-scm.com/docs/gitignore

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSplashlinView Question on Stackoverflow
Solution 1 - GitTim VisherView Answer on Stackoverflow
Solution 2 - GitkusmaView Answer on Stackoverflow
Solution 3 - GitfetzigView Answer on Stackoverflow