27

I have a git repository (at github.com) with two branches: master and gh-pages. I would like to have the gh-pages branch in a subdirectory, so that I don't need to switch branches every time.

repo/
    (content of the master branch)
    gh-pages/
            (content of the gh-pages branch)

Is that possible ?

Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
  • It is usually simpler to use a separate repository, but you might find git.git’s [contrib/workdir/git-new-workdir](http://git.kernel.org/?p=git/git.git;a=blob;f=contrib/workdir/git-new-workdir;h=75e8b258177f7f04dadcac125f2bf7ebea4d0f81;hb=HEAD) useful. – Chris Johnsen Feb 23 '11 at 05:37
  • an actual direct answer to the question: http://stackoverflow.com/questions/4750520/git-branch-gh-pages/4993758#4993758 – cregox Mar 24 '11 at 15:00

6 Answers6

26

You may be looking for the subtree merging option.

It will let you checkout an unrelated branch into a subdirectory of another and then merge back and forth between them. You would still have to checkout gh-pages and merge in changes from the main repo before pushes would go live on GitHub, however.

You could also check gh-pages out as a submodule of your master branch if that suits you better.

Scott Chacon
  • 2,756
  • 19
  • 14
9

You can have both branches checked out at the same time in different directories.

Setup:

git worktree add -b gh-pages ../gh-pages origin/gh-pages

Then,

  • you have in . the master branch and in ../gh-pages the branch gh-pages.
  • you can work in both directories with the usual git pull and git push actions
  • you save disk space, because the clone is stored once (and not twice)
  • you can switch branches at each of the directories. Only constaint: The checked out branches must be different at each time. Meaning, you cannot have patch-1 checked out in both directories.

Notes:

koppor
  • 19,079
  • 15
  • 119
  • 161
  • There way too many files in that directory, how can I use it? Should I just copy the `deploy.sh` file? – fiatjaf Jan 03 '17 at 13:08
  • Yes, only `deploy.sh`. See below the heading **configuration** in the provided README.md file: https://github.com/X1011/git-directory-deploy#configuration – koppor Jan 18 '17 at 07:07
6

That's not how things are designed to work. You could theoretically clone the repository within a subdirectory of your original clone and mark that directory as excluded from the higher level repository, but wouldn't it be much simpler to just check it out in a completely different directory instead of a subdirectory?

That is to say...

/repo/master/(clone on master branch)

and then another clone that's on the other branch

/repo/gh-pages/(clone on gh-pages branch)
Amber
  • 507,862
  • 82
  • 626
  • 550
3

Generally with version control it's not a good idea to combine multiple projects into a single repository. For instance, what if someone would like to fork your repository, but not host their copy at GitHub? Then the gh-pages directory would be completely useless to them. Even if they did host theirs at GitHub, the gh-pages directory could very well still be irrelevant to them.

I realize that the GitHub way of doing this goes against this advice, somewhat (after all, even though they are on different branches, they're still in the same repo). However, the branches in this case are completely unrelated (they don't share any history) so from a practical perspective, it's as if they were in separate repositories. If someone clones your repo and doesn't want the gh-pages branch, they can delete it and it will have zero effect on master.

Dan Moulding
  • 211,373
  • 23
  • 97
  • 98
0

I've tried the git subtree trick, but it requires that master have all the gh-pages content committed to it, which is not ideal in this case. Much better and simpler is to

  1. Create a new directory at ./repo/gh-pages/
  2. Put a line on .gitignore for that (gh-pages)
  3. cd gh-pages/, git init and git checkout -b gh-pages, creating an independent git remote there

You can also git clone directly to ./gh-pages/ with only the branch you want.

fiatjaf
  • 11,479
  • 5
  • 56
  • 72
-3

Branches in git are pointers to commits (that move), and so having a branch as a subdirectory is not possible.

To be fair, git co gh-pages is not much harder than cd ../gh-pages

lprsd
  • 84,407
  • 47
  • 135
  • 168
  • It's not about being harder, but having both there at the same time. It's perfectly reasonable to have a gh-pages branch that is completely disjoint from master, and so convenient to have them side by side to move files. – Jan Segre Sep 13 '15 at 17:42