我可以“git commit”吗?一个文件并忽略其内容更改?
我团队中的每个开发人员都有自己的本地配置。该配置信息存储在名为 devtargets.rb 的文件中,该文件在我们的 rake 构建任务中使用。不过,我不希望开发人员破坏彼此的 devtargets 文件。
我的第一个想法是将该文件放入 .gitignore
列表中,这样它就不会提交到 git。
然后我开始想:是否可以提交文件,但忽略对文件的更改?因此,我会提交文件的默认版本,然后当开发人员在本地计算机上更改它时,git 将忽略这些更改,并且当您执行 git status 或 git commit 时,它不会显示在已更改文件列表中。
这可能吗?这肯定是一个很好的功能......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当然,我有时会使用
“撤消并再次开始跟踪”来执行此操作(如果您忘记了哪些文件未跟踪,请参阅此问题):
相关文档:
在这种情况下,优雅地失败意味着,当您进行拉取时,如果该文件的上游有任何更改(合法更改等),它会说:
并将拒绝合并。
此时,您可以通过恢复本地更改来克服这个问题,这是一种方法:
然后再次拉取并重新修改本地文件,或者可以设置
–no-assume-unchanged
,您可以这样做此时进行正常的存储和合并等。Sure, I do exactly this from time to time using
To undo and start tracking again (if you forgot what files were untracked, see this question):
Relevant documentation:
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:
and will refuse to merge.
At that point, you can overcome this by either reverting your local changes, here’s one way:
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 update-index --skip-worktree,如
要撤消此操作,请使用 git update-index --no-skip-worktree
从 git 版本 2.25.1 开始,这也不再是推荐的方式,引用:
If you are in control of the repository, prefer the method from this answer. If not, read on.
The preferred way to do this is to use
git update-index --skip-worktree <file>
, as explained in this answer:To undo this, use
git update-index --no-skip-worktree <file>
Since git version 2.25.1, this is no longer the recommended way either, quoting:
常见的做法似乎是创建一个 devtargets.default.rb 并提交它,然后指示每个用户将该文件复制到 devtargets.rb (位于 . gitignore 列表)。例如,CakePHP 对其数据库配置文件执行相同的操作,该文件自然会随着计算机的不同而变化。
Common practice seems to be to create a
devtargets.default.rb
and commit it, and then instruct each user to copy that file todevtargets.rb
(which is on the .gitignore list). For example, CakePHP does the same for its database configuration file which naturally changes from machine to machine.使用 Git 无法忽略对跟踪文件的更改。 Git 常见问题解答对此进行了解释:
如果您的目标是使用配置文件,那么最好的办法是添加示例或模板文件,然后让用户将其复制到位或让脚本创建适当的文件。然后,您应该忽略实际配置文件的位置,只签入示例或模板。
It is not possible to ignore changes to a tracked file with Git. The Git FAQ explains this:
If your goal is to work with a configuration file, then the best thing to do is add an example or template file and then either have the user copy it into place or have a script create the appropriate file. You should then ignore the location of the actual configuration file and only check in the example or the template.
对于 IntelliJ IDEA 用户:如果您想忽略一个或多个文件的更改,您可以将其移动到不同的
更改集
。本地更改
(Cmd + 9
)F6
将它们移动到另一个更改设置
For IntelliJ IDEA users: If you want to ignore changes for a file (or files) you can move it to different
Change Set
.Local Changes
(Cmd + 9
)F6
to move them to anotherChange Set