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
280 views
in Technique[技术] by (71.8m points)

What's the easiest way to deploy a folder to a branch in git?

I have a folder in my master branch named public/, what would be the easiest way to copy its contents to the root directory in a different branch, such as gh-pages?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

One really nice trick illustrated in Generate GitHub pages in a submodule by Blind Gaenger (Bernd Jünger) is to declare a branch as a submodule for another branch!

So if your public content was in its own orphan branch (like gh-pages content is usually kept in its own orphan branch), then you could:

  • declare public as a submodule of your main repo in master branch
  • declare the same public as a submodule of your main repo in gh-pages

So any modification that you make in submodule public when you are in master branch can be updated (git submodule update) once you switch to gh-pages branch!

I am not saying this is a solution for your current setup, because it involves removing public from your main content, and adding it back in its own branch (ie, the other solutions might be more straightforward), but it is an interesting way to share a common directory:

cd yourRepo
git symbolic-ref HEAD refs/heads/public
rm .git/index
mv public ..
git clean -fdx
mv ../public/* .
git add .
git commit -m "public content in orphan branch public"
git push origin public

But once you have created an orphan branch to include that (public) content, the trick to add that branch content as a submodule for other branch is:

$ git checkout master
$ git submodule add -b public git@github.com:user/repo.git public
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   .gitmodules
#    new file:   public
#
$ git commit -m "added public as submodule"
$ git push

And you can repeat that step for your gh-pages branch.

That will create a directory "public", present both when you checkout master or gh-pages.
In both cases (both checkout), a git submodule update will be enough to get back the latest evolutions done on public submodule.
And as usual when it comes to submodules, once you do modifications in public directory, make sure you are on its 'public' branch, commit and push, then go back one directory above, and add-commit again.


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

Now you can select a source in your repository settings and GitHub Pages will look for your content there.

So the submodule trick is no longer necessary to make both type of content visible through the same branch.


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

...