如何设置 git 驱动程序以忽略合并时的文件夹?
我有主分支(所有主要代码都驻留在其中)和设计分支(其中 Rails 应用程序的布局由设计团队构建)。他们在公共文件夹中添加了一个名为“photoshop”的文件夹,以将图像来源也置于版本控制之下。但我不希望在合并时将此文件夹复制到主分支,因为,嗯,不需要它。
显然,做到这一点的方法是通过合并驱动程序。因此,我创建了“ignore”驱动程序:
[merge "ignore"]
name = always ignore during merge
driver = ignore.sh %0 %A %B
并在 $PATH 上创建了ignore.sh 文件:
exit 0
我在 public/ 中创建了 .gitattributes 文件,因为 Photoshop 文件夹应该被整体忽略,并且它将出现在 public/ 下:
photoshop merge=ignore
photoshop/ merge=ignore
photoshop/* merge=ignore
photoshop/**/* merge=ignore
如您所见,我尝试了几种不同的模式来忽略整个文件夹,但它不起作用。我相信这是因为 master 分支上没有文件夹,所以不存在冲突,因此 git 不使用忽略驱动程序。有没有一种方法可以实现这一目标,而无需在 master 上创建 public/photoshop 文件夹?
I have the master branch, where all the main code resides, and the design branch, where the layout of the rails application is built by the design team. They have added a folder called "photoshop" to the public folder to keep their sources for the images also under version control. But I don't want this folder to be copied on merge to the master branch because, well, it is not needed.
Apparently, the way to do this is through a merge driver. So, I've created the "ignore" driver:
[merge "ignore"]
name = always ignore during merge
driver = ignore.sh %0 %A %B
And created the ignore.sh file on my $PATH:
exit 0
I've created the .gitattributes file inside public/, because the photoshop folder should be ignored in whole and it is going to appear under public/:
photoshop merge=ignore
photoshop/ merge=ignore
photoshop/* merge=ignore
photoshop/**/* merge=ignore
As you can see, I've tried several different patterns to ignoring the whole folder, but it does not work. I believe this is because there is no folder on the master branch, so there's no conflict and so git doesn't use the ignore driver. Is there a way to achieve this without having to create a public/photoshop folder on master?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如我的其他答案所建议的,这里是解决方案的扩展、通用、工业强度版本。
(是的,我在家很无聊,没有其他更好的事情可做:P)
此脚本将根据您的本地添加一个新的、分离的提交em> 设计分支,因此它不会影响设计存储库或您的设计分支。提交将删除所有所需的文件。然后它执行合并。
对于那些懒得阅读完整代码的人,这些步骤的“核心”可以简化为:
这是完整的脚本:
git-strip-merge
合并前:
合并后:
请注意,“design”分支提示根本不受影响,即使是本地分支,也得益于分离提交技巧。除此之外,两个提交(删除和合并)都是常规提交,具有合适的提交消息和父级。 “master”分支清除了任何不需要的文件。
As suggested by my other answer, here goes the extended, generalized, industrial-strength version of the solution.
(yes, I was bored at home and had nothing else better to do :P)
This script will add a new, detached commit based on your local design branch, so it won't affect neither the design repository or your design branch. The commit will have all desired files removed. Then it performs the merge.
For those too lazy to read the full code, the "core" of these steps can be simplified as:
And here is the full script:
git-strip-merge
Before merge:
After merge:
Note that "design" branch tip wasn't affected at all, even being a local branch, thanks to the detached commit trick. Other than that, both commits (the removal and the merge) are regular commits, with suitable commit messages and parents. And "master" branch is clean of any undesired files.
关于
.gitignore
的一些注意事项:.gitignore
用于忽略工作树中要添加到< em>索引(暂存区)。因此它只有在使用 git add在您的场景中,不需要
.gitignore
,因为您没有任何本地./photoshop
文件夹,因此您永远不会有任何 Photoshop 文件可以添加到您的主分支中。不过,为了以防万一,创建一个也没什么坏处。对于设计团队来说,.gitignore
并不受欢迎,因为他们希望将 Photoshop 文件添加到他们的分支中。因此,由于合并处理提交的数据,并且
./photoshop
文件已经在存储库中,因此您使用合并驱动程序的方法是正确的。问题是......默认情况下,只有在发生冲突的情况下才会触发合并驱动程序。而且,由于
master
分支没有任何./photoshop
文件夹或文件,根本不存在冲突,它们被干净地合并。因此,无论您的路径模式如何,您的合并驱动程序也没有任何效果(顺便说一句,您的第二个驱动程序photoshop/
是正确的)。我不知道 git merge 是否可以配置为触发合并驱动程序,即使对于非冲突文件也是如此,但它值得谷歌搜索。我的回答并不能真正解决你的问题。我只是希望阐明这个主题,解释为什么您使用合并驱动程序和 .gitignore 的尝试失败了。我建议阅读有关(配置)合并驱动程序的更多信息。另外子模块也值得研究。
更新
也许我的其他答案会对您有所帮助:
git-strip-merge
A few notes about
.gitignore
:.gitignore
is for ignoring files from the working tree to be added to index (staging area). So it is only effective when you usegit add <files>
, and it can still be overridden usinggit add --force <files>
. It is meant just as a convenience to prevent unwanted files from being added, but it has no effect on files inside the repositoryIn your scenario,
.gitignore
is not needed, since you don't have any local./photoshop
folder, so you will never have any photoshop files to add to your master branch. It wouldn't hurt, however, to create one just in case. And for the design team.gitignore
is not welcome, since they want photoshop files to be added to their branch.So, since merge deals with committed data, and
./photoshop
files are already inside the repository, your approach of using a a merge driver was correct.The problem is... by default a merge driver is only triggered in case of conflicts. And, since
master
branch does not have any./photoshop
folder or files, there are no conflicts at all, they are cleanly merged. So your merge driver also had no effect, regardless of your path patterns (by the way, your 2nd one,photoshop/
, was the correct one). I don't know if git merge can be configured to trigger a merge driver even for non-conflicting files, but it's worth googling for.My answer is not a true solution to your problem. I just hoped to shed some light on the subject, explaining why your attempts using merge driver and .gitignore have failed. I suggest reading more about (configuring) merge drivers. Also submodules is worth investigating.
UPDATE
Maybe my other answer will help you:
git-strip-merge
您是否尝试在
master
分支中添加.gitignore
文件来忽略任何photopshop
目录内容?然后,从设计到
master
的合并不应在master
中添加该新目录。如果这有效,您仍然需要合并驱动程序,但这次是为了管理
.gitignore
文件的内容。Did you try adding a
.gitignore
file in yourmaster
branch, to ignore anyphotopshop
directory content ?Then a merge from design to
master
shouldn't add that new directory inmaster
.If this works, you still needs a merge driver, but this time to manage the content of the
.gitignore
file.