简体   繁体   English

我可以在git中将一个子文件夹从一个分支合并/同步到另一个分支的根吗?

[英]Can I merge/sync a sub-folder from one branch to the root of another in git?

I want to have a /doc folder in master and easily merge/sync it to the root of a gh-pages branch. 我想在master中有一个/ doc文件夹,并轻松地将其合并/同步到gh-pages分支的根目录。 What is the easiest way to do this? 最简单的方法是什么?

One nice trick would be to declare the branch gh-branch as a submodule in your master branch (also more detailed in " How to add a git repo as a submodule of itself? "). 一个不错的技巧是在您的master分支中将分支gh-branch声明为子模块 (在“ 如何添加git repo作为自身的子模块? ”中也有详细介绍)。

That way, when you are in your master branch, you see a folder gh-branch which represents the gh-branch content. 这样,当您在master分支中时,会看到一个代表gh-branch内容的文件夹gh-branch

You could then: 然后,您可以:

  • version doc only in the gh-branch 仅在gh-branch中的版本doc
  • have a symlink doc in your master branch, linked to gh-branch/doc 在您的master分支中有一个symlink doc ,链接到gh-branch/doc

That way, you maintain the doc/ content only in one place. 这样,您仅可将doc/内容维护在一个地方。

#!/bin/sh

# if there's a docbranch tree and it matches master:doc,
# then get out now, there's nothing to do:

current=`git rev-parse -q --verify docbranch:` \
&& test $current = "`git rev-parse -q --verify master:doc`" \
&& exit 0

# update the docbranch ref to a new commit ...    
git update-ref refs/heads/docbranch $(
        # ... which has master:doc, parented on docbranch if that exists yet
        git commit-tree ${current:+-p docbranch} -m "updating" master:doc
)

You can use that as your post-commit hook and chmod +x it, every time you commit anything, if the master doc directory has changed it will commit it to the head of docbranch . 您可以将其用作提交后的钩子,并对其进行chmod +x ,每当您提交任何内容时,如果master doc目录已更改,它将把它提交到docbranch的头部。

Since the tree's already committed, the only thing this adds is the (~200 byte) commit object itself. 由于树已经提交,因此添加的唯一内容是(〜200字节)提交对象本身。

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

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