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

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

问题描述

完整的问题重写

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

<小时>

我有一个存储库(项目),我想在其中自动化将提交从一个分支中的一个目录推送到另一个分支的过程;一些我还没有遇到过的东西.

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

[项目主分支]|- gh-pages (目录)|- css(目录)|- index.html(文件)|- readme.md(文件)[PROJECT gh-pages BRANCH]|-(目前为空)

我希望做的是创建一个钩子,它会自动处理我的 gh-pages 目录中来自 master 分支的更改,并将它们(使用正确的术语)复制/克隆/替换为 gh-pages 分支,项目网站分支.这是一个省略所有其他文件的示例:

[项目主分支]|- gh-pages(目录)<=== 请参阅下面的更新 [A]||- css(目录)|||- style.css(文件)||- index.html(文件)[PROJECT gh-pages BRANCH]|- css(目录)<=== 请参阅下面的更新 [B]||- style.css(文件)|- index.html(文件)

我对这个级别的 Git Hub 完全陌生.我通常只坚持基础知识,从不使用终端/外壳.因此,总而言之,为了澄清我希望做的事情,我想:

  • 只需要在[Master Branch]工作.我需要对 [gh-pages Branch] 进行的所有更改都在 [Master Branch] 的 gh-pages 目录中进行
  • 最好通过添加一个似乎是 post-receive hook 的简单文件来实现这一点?

这是我尝试使用的一些接收后钩子代码(我通过研究一些东西制作了它)但它不起作用:

#!/bin/bash而读取 oldrev newrev refname做分支=$(git rev-parse --symbolic --abbrev-ref $refname)if [ "master" == "$branch" ];然后git checkout gh-pagesgit checkout master -- gh-pagesgit add gh-pagesgit commit -m "从'master' 分支更新项目网站."菲完毕

注意
正如我在评论中提到的:这不是重复的.这不是问如何推送,而是如何添加在我执行正常推送时自动运行的其他命令.这些命令将完成我的 OP 中提到的额外工作.

更新
我已将这些箭头添加到我在下面引用的代码部分:<===

[A] 这里应该发生的事情是 Git 应该递归读取主分支 gh-pages 目录,并且只将更新的内容(或所有更容易的内容)复制到 gh-页面分支.

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

[PROJECT gh-pages BRANCH]|- gh-pages(目录)<=== 不应该发生||- css(目录)|||- style.css(文件)||- index.html(文件)

此外,钩子不应该复制任何其他文件,而是复制 gh-pages 中的内容.即使在 master 分支中更改了其他几个文件,也应该只复制 gh-pages 目录文件.

[C] 新代码 - 这有效但会导致错误:

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

这行不通有两个原因.1) 如果 repo 延迟提交,它无法处理,它会出错;如果使用拉取而不是提取,则本地存储库会像这样被擦除:

如果我离开 fetch,本地存储库将保持原样:

2) 整个 gh-pages 目录仍然被复制到 gh-pages 分支,而不仅仅是其中的文件.

解决方案

您真的不需要这种复杂的方法.

只需将您自己的存储库添加为子模块(本身!),子模块跟随 gh-pages 分支(因为

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

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天全站免登陆