我团队中的每个开发人员都有自己的本地配置。该配置信息存储在一个名为devtargets的文件中。Rb在我们的rake构建任务中使用。不过,我不希望开发人员互相破坏对方的devtargets文件。

我的第一个想法是将该文件放在.gitignore列表中,这样它就不会提交给git。

然后我开始思考:是否有可能提交文件,但忽略对文件的更改?所以,我会提交文件的默认版本,然后当开发人员在他们的本地机器上更改它时,git会忽略这些更改,当你执行git状态或git提交时,它不会显示在更改文件列表中。

这可能吗?这肯定是一个不错的功能……


通常的做法似乎是创建一个devtargets.default.rb并提交它,然后指示每个用户将该文件复制到devtargets。Rb(在.gitignore列表中)。例如,CakePHP对它的数据库配置文件做了同样的事情,该文件在不同机器之间自然会发生变化。


当然,我经常这样做

git update-index --assume-unchanged [<file> ...]

要撤消并重新开始跟踪(如果您忘记了哪些文件被未跟踪,请参阅此问题):

git update-index --no-assume-unchanged [<file> ...]

相关文档:

--[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.

在这种情况下,失败优雅的意思是,如果在你进行拉取时,该文件上游有任何更改(合法更改等),它会说:

$ git pull
…
From https://github.com/x/y
   72a914a..106a261  master     -> origin/master
Updating 72a914a..106a261
error: Your local changes to the following files would be overwritten by merge:
                filename.ext
 

并且拒绝合并。

在这一点上,你可以通过恢复你的本地更改来克服这个问题,这里有一种方法:

 $ git checkout filename.ext

然后再次拉并重新修改您的本地文件,或者可以设置-no-assume-unchanged,您可以在那时进行正常的隐藏和合并等。


对于IntelliJ IDEA用户:如果您想忽略一个文件(或多个文件)的更改,可以将其移动到不同的更改集。

转到本地更改(Cmd + 9) 选择要忽略的文件 F6将它们移动到另一个更改集


这样做的首选方法是使用git update-index——skip-worktree <file>,如下所示:

assume-unchanged is designed for cases where it is expensive to check whether a group of files have been modified; when you set the bit, git (of course) assumes the files corresponding to that portion of the index have not been modified in the working copy. So it avoids a mess of stat calls. This bit is lost whenever the file's entry in the index changes (so, when the file is changed upstream). skip-worktree is more than that: even where git knows that the file has been modified (or needs to be modified by a reset --hard or the like), it will pretend it has not been, using the version from the index instead. This persists until the index is discarded.

要撤销这个,使用git update-index——no-skip-worktree <file>

从git 2.25.1版本开始,这也不再是推荐的方式,引用如下:

Users often try to use the assume-unchanged and skip-worktree bits to tell Git to ignore changes to files that are tracked. This does not work as expected, since Git may still check working tree files against the index when performing certain operations. In general, Git does not provide a way to ignore changes to tracked files, so alternate solutions are recommended. For example, if the file you want to change is some sort of config file, the repository can include a sample config file that can then be copied into the ignored name and modified. The repository can even include a script to treat the sample file as a template, modifying and copying it automatically.


使用Git不可能忽略对跟踪文件的更改。Git常见问题解答如下:

Git doesn’t provide a way to do this. The reason is that if Git needs to overwrite this file, such as during a checkout, it doesn’t know whether the changes to the file are precious and should be kept, or whether they are irrelevant and can safely be destroyed. Therefore, it has to take the safe route and always preserve them. It’s tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don’t work properly for this purpose and shouldn’t be used this way.

如果您的目标是使用配置文件,那么最好的方法是添加一个示例或模板文件,然后让用户将其复制到适当的位置,或者让脚本创建适当的文件。然后,您应该忽略实际配置文件的位置,只签入示例或模板。