Stop tracking a file in git - without having it de

2019-02-27 11:19发布

问题:

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:

  • git assume-unchanged vs skip-worktree - the general advice for assume-unchanged should be taken with a grain of salt (on pull, reset --hard etc)
  • git assume unchanged vs skip worktree - ignoring a symbolic link - see this answer for core.sparseCheckout

回答1:

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.



回答2:

I had kind of the same problem, I used git rm --cached <file> and it worked locally but once I made a push to the remote server it deleted also the files, and I did not want that.Then I found git update-index --assume-unchanged <file> and it ignores the local changes and does not delete it from the remote server, worked like a charm!



标签: git gitignore