简体   繁体   English

git工作流:我可以阻止某个文件合并到另一个分支但仍保持在版本控制之下吗?

[英]git workflow: Can I prevent a certain file from being merged to another branch but still keep it under version control?

I have a repository with two branches: live and stage. 我有一个包含两个分支的存储库:live和stage。 The repository holds code for a web based application. 存储库包含基于Web的应用程序的代码。 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). 我想为舞台和现场分支维护两个不同的.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. 我是git的新手,所以可能有一个简单的解释,但我想有一个工作流程,当我在现场和我拉动更改(简单的git merge stage )时,我没有覆盖现有的.htaccess在现场分支。 Is there any way to configure this? 有没有办法配置这个? Am I missing some big concept in git? 我在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. 同样地,还有一大块(但可能很多).html文件,我想跟踪变化但不将它们合并回实时。 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. 为了清楚起见,我希望live和stage能够跟踪这些文件中的更改,但知道在进行合并时不要合并这些特定的更改。

I found an answer on another question on Stack Overflow, credit goes to fcurella: 我找到了关于Stack Overflow的另一个问题答案 ,信用转到fcurella:

Let's say you want to exclude the file config.php 假设您要排除文件config.php

On branch A: 在分支A上:

  1. Create a file named .gitattributes in the same dir, with this line: config.php merge=ours . 在同一个目录中创建一个名为.gitattributes的文件,使用以下行: config.php merge=ours This tells git what strategy to use when merging the file. 这告诉git在合并文件时使用什么策略。 In this case it always keep your version, ie. 在这种情况下,它始终保持您的版本,即。 the version on the branch you are merging into. 您正在合并的分支上的版本。

  2. Add the .gitattributes file and commit 添加.gitattributes文件并提交

On branch B: repeat steps 1-2 在分支B上:重复步骤1-2

Try merging now. 现在尝试合并。 Your file should be left untouched. 您的文件应保持不变。

This seems like a more scalable solution. 这似乎是一个更具伸缩性的解决方案

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文件的完整性,您可以从live中签出此文件:

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: 现在,您的工作树具有除stage .htaccess之外的所有阶段更改,因此您可以完成合并提交:

git commit -m "Pulled changes from stage" -a

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

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