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

ぐ巨炮叔叔 提交于 2019-12-10 10:55:38

问题


I want to have a /doc folder in master and easily merge/sync it to the root of a gh-pages branch. What is the easiest way to do this?


回答1:


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?").

That way, when you are in your master branch, you see a folder gh-branch which represents the gh-branch content.

You could then:

  • version doc only in the gh-branch
  • have a symlink doc in your master branch, linked to gh-branch/doc

That way, you maintain the doc/ content only in one place.




回答2:


#!/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.

Since the tree's already committed, the only thing this adds is the (~200 byte) commit object itself.



来源:https://stackoverflow.com/questions/23962446/can-i-merge-sync-a-sub-folder-from-one-branch-to-the-root-of-another-in-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!