我可以'提交'一个文件并忽略它的内容更改吗? [英] Can I 'git commit' a file and ignore its content changes?

查看:94
本文介绍了我可以'提交'一个文件并忽略它的内容更改吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

然后我开始想知道:是否可以提交文件,但忽略对文件的更改?因此,我会提交该文件的默认版本,然后当开发人员在其本地计算机上更改它时,git会忽略这些更改,并且在执行git status或git commit时它不会显示在已更改文件的列表中。



这可能吗?这肯定是一个很好的功能......

解决方案

当然,我不时使用

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

撤销并重新开始追踪(如果您忘记了未追踪的文件, )

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

相关文档


- [no- ]假定 - 不变

当指定此标志时,为路径记录的对象名称不会更新。相反,该选项设置/取消路径的假定不变位。当假设不变位打开时,用户承诺不更改文件,并允许Git假定工作树文件与索引中记录的内容匹配。如果你想改变工作树文件,你需要取消设置该位以告诉Git。在一个非常缓慢的 lstat(2)系统调用(例如cifs)的文件系统上处理大项目时,这有时会很有帮助。



如果需要在索引中修改此文件,Git将失败(正常),例如合并时提交;因此,如果假设未跟踪文件在上游发生更改,则需要手动处理该情况。


在这种情况下优雅地失败意思是说,如果您在进行拉动操作时上传到该文件(合法更改等)的任何更改,它会说:

  $ git pull 
...
从https://github.com/x/y
72a914a..106a261 master - > origin / master
正在更新72a914a..106a261
错误:对以下文件的本地更改将被合并覆盖:
filename.ext

并拒绝合并。



此时,您可以通过恢复本地更改,这里有一个方法:

  $ git checkout filename.ext 

然后再次拉动并重新修改你的本地文件,或者设置 -no-assume-unchanged 和你可以在这一点进行正常的藏匿和合并等。


Every developer on my team has their own local configuration. That configuration information is stored in a file called devtargets.rb which is used in our rake build tasks. I don't want developers to clobber each other's devtargets file, though.

My first thought was to put that file in the .gitignore list so that it is not committed to git.

Then I started wondering: is it possible to commit the file, but ignore changes to the file? So, I would commit a default version of the file and then when a developer changes it on their local machine, git would ignore the changes and it wouldn't show up in the list of changed files when you do a git status or git commit.

Is that possible? It would certainly be a nice feature...

解决方案

Sure, I do exactly this from time to time using

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

To undo and start tracking again (if you forgot what files were untracked, see this question):

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

Relevant documentation:

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

Fail gracefully in this case means, if there are any changes upstream to that file (legitimate changes, etc.) when you do a pull, it will say:

$ 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

and will refuse to merge.

At that point, you can overcome this by either reverting your local changes, here’s one way:

 $ git checkout filename.ext

then pull again and re-modify your local file, or could set –no-assume-unchanged and you can do normal stash and merge, etc. at that point.

这篇关于我可以'提交'一个文件并忽略它的内容更改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆