简体   繁体   English

忽略对跟踪文件的更改

[英]Ignore changes to a tracked file

I want to change a tracked file for development only, but keep the tracked version unchanged.我只想为开发更改跟踪文件,但保持跟踪版本不变。

Most "solutions" for this suggest这个建议的大多数“解决方案”

git update-index --assume-unchanged

but it's totally useless, because the file will still be changed by checkout or reset.但这完全没用,因为文件仍然会通过结帐或重置而更改。 Is there a better solution that survives checkout and reset commands?是否有更好的解决方案可以在结帐和重置命令中幸存下来?

Quick Fix快速解决

This is what I think you're trying to do, change a file, but ignore it when committing.这就是我认为您正在尝试做的事情,更改文件,但在提交时忽略它。

git update-index --skip-worktree my-file

Here is a good answer regarding the difference between assume-unchanged and skip-worktree .这是关于assume-unchangedskip-worktree之间差异的一个很好的答案。

Git will still warn if you try to merge changes into my-file .如果您尝试将更改合并到my-file Git 仍会发出警告。 Then you will have to "unskip" the file, merge it and "re-skip" it.然后你将不得不“取消跳过”文件,合并它并“重新跳过”它。

git update-index --no-skip-worktree my-file
# merge here
git update-index --skip-worktree my-file

There can also be problems if you modify the file, then switch to a branch where that file has been changed.如果您修改文件,然后切换到该文件已更改的分支,也可能会出现问题。 You may have to do some fancy "skip/unskip" operations to get around that.您可能需要做一些奇特的“跳过/取消跳过”操作来解决这个问题。

Long Term Fix长期修复

In the long term, you probably want to separate your "local" changes into a second file.从长远来看,您可能希望将“本地”更改分离到第二个文件中。 For example, if the file you want to change is a config file, create a "default" config file that you check into the repository.例如,如果您要更改的文件是配置文件,请创建一个“默认”配置文件,并将其签入存储库。 Then, allow a second "overrides" config file that is optional and put that file in your .gitignore .然后,允许第二个可选的“覆盖”配置文件并将该文件放入您的.gitignore

Then, in your application, read the default config file and then check if the overrides file exists.然后,在您的应用程序中,读取默认配置文件,然后检查覆盖文件是否存在。 If it does, merge that data with the data from the default file.如果是,则将该数据与来自默认文件的数据合并。

This example is for a config file, but you can use that technique for other kinds of overrides if needed.此示例适用于配置文件,但如果需要,您可以将该技术用于其他类型的覆盖。

Since in one of your comments you are working directly in the master branch, the real problem to fix is working in master .由于在您的评论之一中您直接在master分支中工作,因此真正要解决的问题是在master工作。

You'll want to create a Topic Branch in Git:你需要在 Git 中创建一个主题分支:

git checkout -b some_feature_or_bug_fix master

Then make the necessary changes to the config file, and commit as normal.然后对配置文件进行必要的更改,并正常提交。

The nice thing here is you don't affect files in master .这里的好处是你不会影响master文件。 If you need to pull in updated code, just git fetch and git merge origin/master into your topic branch.如果您需要拉入更新的代码,只需git fetchgit merge origin/master到您的主题分支。 Then you can deal with upstream changes to the config file as normal Git merges.然后,您可以像正常的 Git 合并一样处理对配置文件的上游更改。

Prior to merging your topic branch into master you can do an interactive squash to reduce your commits and merge commits into one nice, clean commit.在将您的主题分支合并到master之前,您可以进行交互式压缩以减少提交并将提交合并为一个漂亮、干净的提交。 Or do a git merge --squash to merge your topic branch into master .或者执行git merge --squash将您的主题分支合并到master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM