8

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?

Steven Lu
  • 2,150
  • 1
  • 24
  • 33
  • See [my related answer](http://stackoverflow.com/a/29616287/946850) and a [writeup](http://krlmlr.github.io/git-subbranch). – krlmlr Apr 13 '15 at 22:51

3 Answers3

6

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, I used a combination of things based on your idea. I do like the submodule breakout! Thanks for the help. – Steven Lu Aug 22 '12 at 04:48
  • Note to self: if you had some content already versioned in `master` that you wish to use to initialize `gh-branch`: http://stackoverflow.com/a/10591668/6309 – VonC Apr 16 '14 at 06:42
  • 1
    A+ for keeping this answer up to date – Steven Lu May 27 '20 at 22:16
1
git checkout --orphan branch_name
git rm -rf .
git checkout master -- public

Is public in the root dir of master?

bcr
  • 1,328
  • 11
  • 27
  • Yes, `public` is in the root dir of master, however `-r` does not seem to be a known switch to `git checkout` – Steven Lu Aug 21 '12 at 04:53
0
cp -r public/* ./
git add .
git stash
git checkout gh-pages
git stash apply
zloydadka
  • 160
  • 3
  • Wouldn't this create a a branch based on `master`? I'd need that branch to completely be cleared and only have the `public/` content. – Steven Lu Aug 21 '12 at 04:39