如何在GitHub上创建分支特定的文件 [英] How to create branch-specific files on GitHub

查看:77
本文介绍了如何在GitHub上创建分支特定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要具有相同名称的分支专用文件.我需要能够从开发分支合并到母版,而无需对此文件进行任何更改.

例如:
假设我想要两个不同的readme.md文件.我想要一个内容:MASTER,而另一个DEV.但是,如果我尝试这样做,那么在创建请求请求时,GitHub将尝试合并此文件,这正是我的问题.我不希望GitHub在每次更改时都合并此文件.

解决此问题的最佳方法是什么?

解决方案

让我们说project是要合并的分支的名称,而README.md是保存分支特定信息的文件的名称.

我建议采取以下步骤:

  1. 合并项目分支,但请确保未提交更改并且不快速进行更改

    $ git merge --no-commit --no-ff project
    

  2. 取消暂存README.md文件并签出其当前分支版本

    $ git checkout HEAD -- README.md
    

  3. 完成合并

    $ git commit
    

此外,有必要安装合并驱动程序,以在发生合并冲突时保留文件的分支特定版本.在这种情况下,您将不需要手动解决分支特定文件中的冲突.

这种合并驱动程序通常称为ours,并定义为:

$ git config --global merge.ours.driver true

现在,您可以在.gitattributes文件中指定何时使用此合并.

在我们的情况下,需要将以下规则添加到.gitattributes并提交:

README.md merge=ours

I would like to have branch-specific files with the same name. I need to be able to merge from my development branch to master without changes made in this file.

For example:
Let's assume that I would like to have two different readme.md files. In one I would like to have content: MASTER and in another DEV. But if I try to do it, while creating pull-request GitHub will try to merge this file, which is exactly my problem. I don't want GitHub to merge this file each time I make changes.

What would be the best way to solve this problem?

解决方案

Let's say project is a name of the branch to be merged, and README.md is a name of the file that keep branch specific information.

I would suggest the following steps:

  1. Merge project branch, but make sure changes are not committed, and not fast-forwarded

    $ git merge --no-commit --no-ff project
    

  2. Unstage README.md file and checkout its current branch version

    $ git checkout HEAD -- README.md
    

  3. Complete merge

    $ git commit
    

Also, it makes sense to install merge driver that will keep branch specific version of a file in case of merge conflict. In such case, you will never need to resolve conflicts in branch specific files manually.

Such merge driver is usually called ours and defined as:

$ git config --global merge.ours.driver true

Now, you can specify in .gitattributes file, when this merger should be used.

In our case, it is needed to add the following rule to .gitattributes and commit it:

README.md merge=ours

这篇关于如何在GitHub上创建分支特定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆