Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a GitHub repo with two branches that contain different directories on my local machine?

Tags:

git

github

jekyll

I have a jekyll site that I would like to have hosted on GitHub. Since jekyll plugins aren't supported on GitHub I was wanting to have my mater branch only contain the files inside _site and make a development branch that has everything else in it.

Would this be possible? If so how could I do it? I am not the best with git.

thanks

like image 838
edhedges Avatar asked Jul 26 '12 21:07

edhedges


People also ask

Can Git work with two branches at once?

You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.

How many branches can a GitHub repo have?

There is no hard limit on the number of branches, tags, remote-tracking names, and other references. (All of Git's name-to-hash-ID map entries are refs or references: branch names are just refs whose full name starts with refs/heads/ ). These are not always stored in separate files.

Can you have many different branches at the same time?

Git offers a feature referred to as a worktree, and what it does is allow you to have multiple branches running at the same time. It does this by creating a new directory for you with a copy of your git repository that is synced between the two directories where they are stored.


1 Answers

It seems that all you want is to have a development branch and a master branch which will contain a mirror of the content of some folder, say _site.

Let's do this! Okay. I assume you have a repository with a development branch which contains all staff and _site folder you want to "export".

Lets create a commit which contains just the content of the _site folder.

echo 'Fill with a meaningful description' | git commit-tree development^{tree}:_site creates a commit and outputs its id. It was 47651a42.... It will be different on your machine.

Note that development^{tree}:_site is a revision (sha1 sum) of the tree which corresponds to the _site folder in the root of the last commit on the development branch.

And now make master branch point to this commit: git update-ref refs/heads/master 47651a42

Now git log master show the following on my machine

commit 47651a42e6800f399c4352d0416f4ca96895f099
Author: Aleksandr Priymak <[email protected]>
Date:   Fri Jul 27 05:27:43 2012 +0400

    first commit

If you checkout this branch you get the content of the _site folder! That's simple. There is only one thing left. 47651a42 commit doesn't have any parents so you will need to add -f to your git push command to push updated master. The other way around is to actually specify the parent. To do this use this command

echo 'Fill with a meaningful description' | git commit-tree dev^{tree}:_site -p $(cat .git/refs/heads/master)

You can do this using the following one-liner

git update-ref refs/heads/master $(echo 'Add commit message here!' | git commit-tree dev^{tree}:_site -p $(cat .git/refs/heads/master))
like image 157
Alexandr Priymak Avatar answered Oct 20 '22 17:10

Alexandr Priymak