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

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

问题描述

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

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.

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

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

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

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

推荐答案

当然,我不时使用

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

相关文档:

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

--[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 将失败(优雅地)以防它需要修改索引中的这个文件,例如合并提交时;因此,如果上游更改了假定未跟踪的文件,您将需要手动处理这种情况.

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
 

并且会拒绝合并.

此时,您可以通过还原本地更改来克服此问题,这是一种方法:

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

 $ git checkout filename.ext

然后再次拉取并重新修改您的本地文件,或者可以设置--no-assume-unchanged,此时您可以进行正常的存储和合并等.

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.

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

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