Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

github - Git - How to auto push changes in a directory to another branch

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:

Local repo has been wiped when changing code to pull.

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

Local repo the way it should stay after a push. Fetch was used here.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What's the problem with the hook script you wrote ? What were the contents of the branch gh-pages when you created it?

I had created an empty branch gh-pages with below command:

git checkout --orphan gh-pages
git rm -rf .
touch README.txt
git add README.txt
git commit -m "Initial commit"
git push -u origin gh-pages

Then I ran below script as a part of post-receive hook and it worked for me.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...