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:
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:
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:
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:
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).
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!
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 :)
Very, very complicated workaround for creating a new repo. But what do I know?
This can be done simpler since git 1.7.2.
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` :)