Git 取消跟踪文件和重新跟踪文件


有时候需要让 git 忽略某个文件的更新,比如配置文件。配置文件里经常有各种涉及隐私的东西,一般只提交一个模板上去,如果把自己的配置提交上去了……被盗用的例子也不少了(比如这个这个这个

解决方法也很简单,让 git 忽略这个文件的变更就行了

git update-index --assume-unchanged your_file_path


git update-index --no-assume-unchanged your_file_path

--[no-]assume-unchanged When this flag is specified, the object names recorded for the paths are not updated. Instead, this option sets/unsets the "assume unchanged" bit for the paths. When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.