git 工作流:我可以防止某个文件被合并到另一个分支,但仍将其置于版本控制之下吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4515679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git workflow: Can I prevent a certain file from being merged to another branch but still keep it under version control?
提问by Ankur
I have a repository with two branches: live and stage. The repository holds code for a web based application. I would like to maintain two different .htaccess files for the stage and live branches, but still keep them version controlled, since the nature of serving a stage site is a little bit different (right now for instance, I want the caches to be timed differently).
我有一个有两个分支的存储库:live 和 stage。存储库保存基于 Web 的应用程序的代码。我想为舞台和实时分支维护两个不同的 .htaccess 文件,但仍然保持它们的版本控制,因为为舞台站点提供服务的性质有点不同(例如,现在我希望缓存被计时不同)。
I'm a novice with git so there is probably an easy explanation, but I'd like to have a workflow where when I was in live and I pulled changes (simple git merge stage
), I didn't overwrite the existing .htaccess in the live branch. Is there any way to configure this? Am I missing some big concept in git?
我是 git 的新手,所以可能有一个简单的解释,但我想要一个工作流程,当我在现场并进行更改(简单git merge stage
)时,我没有在现场覆盖现有的 .htaccess分支。有没有办法配置这个?我在 git 中遗漏了一些大概念吗?
Likewise, there is also a chunk of a (but possibly many) .html files where I would like to keep track of changes but not merge them back into live. Just to be clear I want live and stage to got keep track of changes in these files, but know not to merge those certain changes when doing merges.
同样,还有一大块(但可能很多).html 文件,我想在其中跟踪更改但不将它们合并回实时文件。为了清楚起见,我希望 live 和 stage 能够跟踪这些文件中的更改,但知道在进行合并时不要合并那些特定的更改。
回答by Ankur
I found an answer on another questionon Stack Overflow, credit goes to fcurella:
我在 Stack Overflow上的另一个问题上找到了答案,归功于 fcurella:
Let's say you want to exclude the file
config.php
On branch A:
Create a file named
.gitattributes
in the same dir, with this line:config.php merge=ours
. This tells git what strategy to use when merging the file. In this case it always keep your version, ie. the version on the branch you are merging into.Add the
.gitattributes
file and commitOn branch B: repeat steps 1-2
Try merging now. Your file should be left untouched.
假设您要排除该文件
config.php
在分支 A:
创建一个
.gitattributes
在同一目录中命名的文件,包含以下行:config.php merge=ours
. 这告诉 git 在合并文件时使用什么策略。在这种情况下,它始终保留您的版本,即。您要合并到的分支上的版本。添加
.gitattributes
文件并提交在分支 B:重复步骤 1-2
现在尝试合并。您的文件应该保持不变。
This seems like a more scalable solution.
这似乎是一个更具可扩展性的解决方案。
回答by Hakop Palyan
Ankur, try this:
Ankur,试试这个:
Assuming you're checked out on the live branch, run:
假设您已在实时分支上签出,请运行:
git merge --no-commit --no-ff stage
This will merge the two branches, but instead of making a new commit, will fail and leave these changes in your index file and working directory. Now since you want to keep your .htaccess
file intact on the live branch, you can checkout this file from live:
这将合并两个分支,但不会进行新的提交,而是会失败并将这些更改保留在您的索引文件和工作目录中。现在,由于您希望.htaccess
在实时分支上保持文件完整,您可以从实时检出此文件:
git checkout live .htaccess
Now your working tree has all the changes from stage with the exception of .htaccess
, and so you can finalize the merge commit:
现在,您的工作树具有除 之外的所有阶段更改.htaccess
,因此您可以完成合并提交:
git commit -m "Pulled changes from stage" -a