Git - How to automatically push directory changes to another branch

Complete a restart of the question

So, I thought I was explaining this question very simply and directly, but it seems that I am simplifying a lot, so here are some more details. I hope this helps everyone understand that this is not a duplicate either.


I have a repository (project) where I would like to automate the process of pushing commits from one directory to branches to another branch; what I have not met on SO.

Here is my project with its full 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 hope to do is create a hook that automatically processes the changes in my gh-pages directory from the main branch and copies / clones / replaces them (depending on which term is suitable for use) into the gh-pages branch website projects. Below is an example with all other files:

 [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 usually just stick to the basics and never use a terminal / shell. Therefore, to clarify what I hope to do, I would like to:

  • You need to work only in [Main Branch]. All the changes that I need to make to the [gh-pages Branch] I do in the gh-pages directory [Master Branch]
  • Is it advisable to accomplish this by adding a simple file, which seems to be sending after receiving?

Here is some code that I tried to use after receiving (I did this from learning 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 does not ask how to click, but rather how to use other commands that automatically run when I do a normal push. These commands will do the extra work mentioned in my OP.

UPDATE
I added these arrows to parts of my code that I am talking about below: <===

[A] What should happen here is that Git should recursively read the gh-pages main branches directory and copy only what is updated (or all, if it's easier) to the gh-pages branch.

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

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

In addition, the hook should not copy any files other than what is inside the gh pages. Even if several other files have been modified in the main branch, gh-page copy files must be copied.

[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 does not work for two reasons. 1) If the repo lags behind fixing, he can not cope with it, he will fail; if attraction is used instead of pushing, the local repo is wiped like this:

The local repo was wiped when changing the code for pulling.

If I leave the selection, the local repo will remain as it should:

Local repo, how it should remain after the push. Fetch was used here.

2) The entire directory of gh-pages is copied to the gh-pages branch, and not just the files inside it.

+4
source share
2 answers

You really don't need this complicated approach.

Just add your own repo as a submodule (by itself!), A submodule following the gh-pages branch (since the submodule can follow the last commit of the 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 

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

Whenever you make changes to the gh-pages folder, be sure to commit it there and in the main folder of your repo to record a new gitlink (a special entry in the index ) prescribing a new SHA1 submodule of gh-pages .

 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 install:

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

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

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

This way you always have visible content.

And you do not need a complex synchronization / copy mechanism.


August 2016 Update: Simplified Publishing GitHub Pages now allows you to store your page files in subfolders (no more than gh-pages ):

Now you can select the source in the settings of your repository, and the GitHub pages will search for your content there.

You no longer need a sub-modular approach, as your pagers can be located in a subfolder within the same branch.

+1
source

What problem with hook script did you write? What was the content of the gh page branch when it was created?

I created an empty branch of gh pages with the command below:

 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 the below script as part of the post-receive hook, and this 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 
+3
source

Source: https://habr.com/ru/post/1244261/


All Articles