Disjoint Branches in Git

2011-12-30 12:18

Great services like GitHub encourage to share projects and collaborate on them publicly. But not every piece of code feels like it deserves its own repository. Thus it’s quite reasonable to keep a “miscellaneous” repo which collects smaller, often unrelated hacks.

But how to set up such a repository and what structure should it have? Possible options include separate branches or separate folders within single branch. Personally, I prefer the former approach, as it keeps both the commit history and working directory cleaner. It also makes it rather trivial to promote a project into its own repo.
I speak from experience here, since I did exactly this with my repository of presentation slides. So far, it serves me well.

It’s not hard to arrange a new Git repository in such manner. The idea is to keep the master branch either completely empty, or only store common stuff there – such as a README file:

  1. $ git init
  2. $ echo "This is my repo with miscellaneous hacks." > README
  3. $ git add . && git commit -m "Initial commit"

The actual content will be kept in separate branches, with no relation to each other and to the master one. Such entities are sometimes referred to as root branches. We create them as usual – for example via git checkout:

  1. $ git checkout -b foo

However, this is not nearly enough. We don’t want to base the new branch upon the content from master, but we still have it in the working directory. And even if we were to clean it up manually (using a spell such as ls | xargs rm -r to make sure the .git subdirectory is preserved), the removal would have to be registered as a commit in the new branch. Certainly, it would go against our goal to make it independent from master.

But the working copy is just one thing. In order to have truly independent, root branch we also need to disconnect its history from everything else in the repo. Otherwise, any changesets added before the branch was created would carry over and appear in its log.
Fortunately, making the history clear is very easy – although somewhat scary. We need to reach out to internal .git directory and remove the index file:

  1. $ rm .git/index

Don’t worry, this doesn’t touch any actual data, which is mostly inside .git/objects directory. What we removed is a “table of contents” for current branch, making it pristine clear – just like the master right after git init.

As a nice side effect, the whole content of working directory is now unknown to Git. Once we removed the index, every file and directory has became untracked. Now it’s possible to remove all of them in one go using git clean:

  1. $ git clean -xdf

And that’s it. We now have a branch that has nothing in common with rest of the repository. If we need more, we can simply repeat those three steps, starting from a clean working copy (not necessarily from master branch).

Tags: , , ,
Author: Xion, posted under Computer Science & IT »

5 comments for post “Disjoint Branches in Git”.
  1. uolot:
    December 30th, 2011 o 13:11

    bez urazy xion, ale Twój angielski jest jakiś taki toporny, źle to się czyta… jestem kolejnym czytelnikiem, który usuwa Cię z czytnika RSS – powodzenia w podbijaniu zachodniej blogosfery!

  2. Comb:
    December 30th, 2011 o 14:28

    A moim zdaniem angielski xiona nie jest toporny, jest poprawny i dość bogaty jeśli chodzi o słownictwo. Problem w tym, że rzeczywiście nie czyta się go tak dobrze, jak wcześniej polskiej wersji. Tekst wydaje się trochę za “ciężki” i “sztywny” stylistycznie, mógłby być trochę żywszy i hmm… prostszy? Z własnych doświadczeń wiem, że nie wystarczy dobra znajomość angielskiego do pisania strawnych tekstów w tym języku. Wymaga to odmiennego podejścia niż układanie sobie tekstu w głowie i potem tłumaczenia go na angielski. Jakiego? Sam jeszcze do tego nie doszedłem :)

  3. Prawie nie uzywam gita:
    December 31st, 2011 o 15:26

    Very, very complicated workaround for creating a new repo. But what do I know?

  4. Tomasz Wesołowski:
    January 10th, 2012 o 17:08

    This can be done simpler since git 1.7.2.
    See http://stackoverflow.com/a/4288660/399317

  5. Xion:
    January 10th, 2012 o 19:13

    That’s pretty nice, as it avoids the arguably questionably practice of messing with .git directory’s contents. Guess that’s why –orphan was introduced to `git checkout` :)

Comments are disabled.

© 2017 Karol Kuczmarski "Xion". Layout by Urszulka. Powered by WordPress with QuickLaTeX.com.