Search code examples
gitgitignore

Stop tracking a file in git - without having it deleted either locally or on pull


I want to ignore a file/directory tree that was previously tracked - ignore it forever but have it not being deleted on a pull - just have it ignored on the repository the pull happened. Is this possible ? Why not (if not) ? How should I proceed ?

NB: the accepted answer in Remove a file from a Git repository without deleting it from the local filesystem - namely git rm --cached path - will result in the file being deleted on a pull. Nasty. Not only that but "it will delete your file if you check out a revision from before the deletion and then check out a revision from after the deletion" (see this comment) See this question for other interesting comments/answers that do not address my issue however.

I am perfectly aware of the --assume-unchanged flag but this is not what I want - I want to tell to git (and all repository clones) "hey, stop tracking this file/tree" but not "delete this file/tree" - so I want to --assume-unchanged globally as it were. If it is not possible (why ?) I need a workaround.

For the record I am trying to bootstrap git to use it to keep/share history and I want to be able to stop tracking files/directories at will - without having them deleted.

Related:


Solution

  • This is not possible. To prevent new changes from being tracked, you need to remove the file from the repository (using git rm or git rm --cached). This adds a change to the commit that removes the file. When that change is then applied in other repositories, the remove action is performed, resulting in the file being removed.