Git - 如何自动将目录中的更改推送到另一个分支 [英] Git - How to auto push changes in a directory to another branch

查看:1917
本文介绍了Git - 如何自动将目录中的更改推送到另一个分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完成问题重写



所以我以为我很简单直接地解释这个问题,但似乎我过于简单,所以这里是所有的额外细节。希望这有助于大家看到这也不是一个重复。






我有一个存储自动化将提交从分支中的一个目录推送到另一个分支的过程;



这是我的项目的完整结构:

  [PROJECT MASTER BRANCH] 
| - gh-pages(目录)
| - css(目录)
| - index.html b | - readme.md(file)

[PROJECT gh-pages BRANCH]
| - (空的)

我希望做的是创建一个钩子,它将自动处理来自主分支的gh-pages目录中的更改,并复制/克隆/替换它们期限是正确使用)由gh-pages分支,项目网站分支。下面是一个剩下所有其他文件的示例:

  [PROJECT MASTER BRANCH] 
| - gh-pages目录)< ===下面的更新[A]
| | - css(directory)
| | | - style.css(file)
| | - index.html(file)

[PROJECT gh-pages BRANCH]
| - css(directory)<===下面的更新[B]
| | - style.css(file)
| - index.html(file)



我完全是新到这个级别的Git Hub。我通常只是坚持基本和永远不使用终端/外壳。因此,为了澄清我希望做什么,我想:




  • 只需在[Master Branch]工作。我需要对[gh-pages Branch]进行所有更改我在[Master Branch]的gh-pages目录中执行

  • 通过添加一个简单的文件


这里是一些接收后的hook代码,我试过使用(我是从研究几个东西)但不起作用:

 #!/ bin / bash 
while read oldrev newrev refname
do
branch = $(git rev-parse --symbolic --abbrev-ref $ refname)
if [master==$ branch];然后
git checkout gh-pages
git checkout master - gh-pages
git add gh-pages
git commit -m从'master'分支更新项目网站。
fi
done

注意
在我的评论中提到:这不是重复。这不是要求如何推,而是如何解决在我做正常推送时自动运行的其他命令。这些命令将执行我的OP中提到的额外工作。



UPDATE

我已将这些箭头添加到我的代码我参考下面:< ===



[A] 这里应该发生的是,Git应递归读取主分支gh-pages目录,并且只从那里复制已经更新的内容(或者一切,如果这更容易)到gh-pages分支中。



strong>因此,如果master中的gh-pages目录具有index.html文件,并且带有style.css文件的css文件夹应该只复制该结构而不是gh-pages目录本身。下面是一个复制gh-pages目录的坏钩子的例子:

  [PROJECT gh-pages BRANCH] 
| - gh-pages(Directory)< ===不支持到HAPPEN
| | - css(directory)
| | | - style.css(file)
| | - index.html(file)

此外,钩子不应该复制任何其他文件,是在gh页内。即使主分支中有多个其他文件发生更改,也只能复制gh-pages目录文件。



[C] NEW CODE - 错误:

 #!/ bin / bash 
branch = $(git rev -parse --abbrev-ref HEAD)
if [master==$ branch];然后
git fetch&& git checkout gh-pages
git checkout master - gh-pages / *
git add -A
git commit -m从'master'分支更新项目网站。
git push -u origin gh-pages
fi

两个原因。 1)如果repo在后面提交它无法处理,它会错误出来;如果使用pull而不是fetch,则本地repo将被擦除,如下所示:





2)整个gh-pages目录被复制到gh-pages分支

解决方案

你真的不需要这种复杂的方法。



gh-pages 分支之后的子模块中添加自己的repo作为子模块(因为 a子模块可以跟随分支的最新提交

  git checkout master 
git rm -r gh-pages#首先保存你的数据
git submodule add -b gh-pages - / remote / url / of / your / own / repo
git commit -mADd gh-pages branch as submodule
git push

这样,你可以在你的主分支或 gh-pages 文件夹(实际上是一个子模块)中更改文件。



每当你在 gh-pages 文件夹中进行更改时,不要忘记在那里提交文件夹,以便记录新的 gitlink 索引中的特殊条目)代表 gh-pages 子模块的新SHA1。

  cd myrepo / gh-pages 
#修改文件
git add。
git commit -mmodify gh-pages files
cd ..#回到myrepo文件夹
git add gh-pages
git commit -mrecord gh-pages new SHA1
git push

使用git 2.7+ ,您可以设置:

  cd / path / main / repo 
git config push.recurseSubmodules on-demand

然后单个 git push gh-pages 子模块推送到 gh-pages 分支。



稍后,单个 git克隆您的主要repo 和其 gh-pages 分支(在 gh-pages 文件夹)。



这样,你总是可以看到两个内容。








2016年8月更新:更简单的GitHub网页发布 现在可以将您的网页文件保存在





您不再需要子模块方法了,因为您的寻呼机可以位于同一分支中的子文件夹中。


Complete question rewrite

So I thought I was explaining this question very simply and direct but it seems I oversimplified to much, so here is all the extra details. Hopefully this helps everyone see this is also not a duplicate.


I have a repository (project) where I would like to automate the process of pushing commits from one directory in a branch to another branch; something I have not come across yet on SO.

Here is my project with its complete structure:

[PROJECT MASTER BRANCH]
|- gh-pages    (directory)
|- css         (directory)
|- index.html  (file)
|- readme.md   (file)

[PROJECT gh-pages BRANCH]
|- (empty at the moment)

What I am hoping to do is create a hook that will automatically handle changes in my gh-pages directory from the master branch, and copy/ clone/ replace them (whichever term is correct to use) into by gh-pages branch, the projects website branch. Here is an example with all other files left out:

[PROJECT MASTER BRANCH]
|- gh-pages         (directory)   <=== SEE UPDATE BELOW [A]
|  |- css           (directory)
|  |  |- style.css  (file)
|  |- index.html    (file)

[PROJECT gh-pages BRANCH]
|- css           (directory)   <=== SEE UPDATE BELOW [B]
|  |- style.css  (file)
|- index.html    (file)

I am completely new to this level of Git Hub. I normally just stick to the basics and never use the terminal/ shell. So in summary to clarify what I am hoping to do, I would like to:

  • Only have to work in the [Master Branch]. All changes I need to make to [gh-pages Branch] I do in the gh-pages directory of [Master Branch]
  • Preferable accomplish this by adding a simple file which seems to be a post-receive hook?

Here is some post-receive hook code that I tried using (I made it from studying a few things) but it doesn't work:

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then
        git checkout gh-pages
        git checkout master -- gh-pages
        git add gh-pages
        git commit -m "Updating project website from 'master' branch."
    fi
done

NOTE
As mentioned in my comment: This is not a duplicate. This is not asking how to push but rather how to tack on other commands that auto run when I do a normal push. These commands would do the extra work mentioned in my OP.

UPDATE
I have added these arrows to parts of my code I refer to below: <===

[A] What should happen here is that Git should recursively read the master branches gh-pages directory and only copy from that what has updated (or everything if that is easier) into the gh-pages branch.

[B] So if the gh-pages directory in master has an index.html file and a css folder with a style.css file is should only copy over that structure not the gh-pages directory itself. Below is an example of a bad hook that copies the gh-pages directory too:

[PROJECT gh-pages BRANCH]
|- gh-pages         (Directory)   <=== NOT SUPPOSED TO HAPPEN
|  |- css           (directory)
|  |  |- style.css  (file)
|  |- index.html    (file)

Also, the hook should not copy over any other files but what is inside the gh-pages. Even if several other files changed in the master branch only the gh-pages directory files should be copied over.

[C] NEW CODE - This works but causes an error:

#!/bin/bash
branch=$(git rev-parse --abbrev-ref HEAD)
if [ "master" == "$branch" ]; then
    git fetch && git checkout gh-pages
    git checkout master -- gh-pages/*
    git add -A
    git commit -m "Updating project website from 'master' branch."
    git push -u origin gh-pages
fi

This wont work for two reasons. 1) If the repo is behind on commits it cant handle that, it will error out; if a pull is used instead of a fetch the local repo gets wiped like so:

If I leave fetch the local repo stays the way it should:

2) The whole gh-pages directory gets copied over to the gh-pages branch still and not just the files inside it.

解决方案

You really don't need this complex approach.

Simply add your own repo as a submodule (of itself!), submodule following the gh-pages branch (since a submodule can follow the latest commit of a branch)!

git checkout master
git rm -r gh-pages # save your data first
git submodule add -b gh-pages -- /remote/url/of/your/own/repo
git commit -m "ADd gh-pages branch as submodule"
git push

That way, you can change files either in your main branch, or in the gh-pages folder (which is actually a submodule)

Whenever you are making changes in gh-pages folder, don't forget to commit there, and in the main folder of your repo in order to record the new gitlink (special entry in the index) reprsenting the new SHA1 of the gh-pages submodule.

cd myrepo/gh-pages
# modify files
git add .
git commit -m "modify gh-pages files"
cd .. # back to myrepo folder
git add gh-pages
git commit -m "record gh-pages new SHA1"
git push

With git 2.7+, you can set:

cd /path/to/main/repo
git config push.recurseSubmodules on-demand

Then a single git push from your main repo will also push the gh-pages submodule to the gh-pages branch.

Later, a single git clone would clone both your main repo and its gh-pages branch (in the gh-pages submodule folder).

That way, you always have both content visible.

And you don't need a complex "synchronization/copy" mechanism.


Update August 2016: Simpler GitHub Pages publishing now allows to keep your page files in a subfolder of the same branch (no more gh-pages needed):

You don't need the submodule approach anymore, since your pagers can be in a subfolder within the same branch.

这篇关于Git - 如何自动将目录中的更改推送到另一个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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