A phrase that you often hear in software development is, “Take the time to build the software right the first time.” Honestly, this doesn’t happen. Commonly it takes more than a few tries to get it right. It’s one of the major reasons Agile Software Development has become such a common catchphrase in business now. (Side bar: http://pragdave.me/blog/2014/0
One of the common pitfalls that occur in a lot of companies, frequently startups, is that everything and the kitchen sink get thrown into the same code base. It tends to remain that way because many aren’t sure what the steps are to dig themselves out of this mountain of spaghetti. Fortunately, if you are using git I’m here to help you out of this mess. In git there are a couple of features you can leverage to help yourself out.
The first is a method for splitting your single repository into multiple repositories. If you don’t care about the history of the project you can simply copy the folder out and create a new repository. But if you don’t want to lose the history then you will need another method for doing so. This is where the git-filter-branch command comes in. (If you would like to read more about it: http://git-scm.com/docs/git-fi
Long story short the git-filter-branch command lets you rewrite history. So what this will allow us to do is collect the revisions that pertain to the directory we are looking to split out of our repository and ditch all the rest of them. Assuming we have a repository called
awesome with a few sub-directories:
awesome/ sorta/ most/ truly/
After a bit of development you realize your project is most/ truly/ awesome/ and there’s no room for sorta/ anymore, but you know that sorta/ could be awesome one day so you want to keep it and the revision history around. It’s actually much more simple than you might think. Just follow the following steps:
Step 1. Clone the awesome repository and change to it. (Assuming your project is hosted on Github and your user is `distil`, the command would look like this.)
$ git clone <a href="https://github.com/distil/awesome.git" target="_blank">https://github.com/distil/awes<wbr />ome.git</a> && cd awesome/
Step 2. Get rid of everything except the sorta/ sub-directory and its history.
$ git filter-branch --prune-empty --subdirectory-filter sorta/ master
Step 3. Replace the existing remote origin with the new repository. (Assuming you’ve created it on Github already.)
$ git remote set-url origin <a href="https://github.com/distil/sorta.git" target="_blank">https://github.com/distil/sort<wbr />a.git</a>
Step 4. Push to the new repository
$ git push -u origin master
If you want to copy over other branches just replace `master` in Step 2 with the branch name and everything else stays the same. Make sure you have the latest code checked out for the branch; as you’ll need the files locally for the command to work.
That’s it. You now have a new repository named `sorta` and you can delete the sorta/ directory from your awesome repository just like you would any other directory.
Come back later to get round two. How to keep the sorta/ directory as it’s own repository and keep using it in awesome. Perhaps even share it in your other repository called `sweet`.
About the Author
Chris Altman, Distil's Senior Software Architect, joined Distil as our Software Architect in 2013. He has a passion for solving problems; whether it is building a dependable, maintainable, scalable, and secure system; or making the lives of those around him more enjoyable. Chris is always excited to share the knowledge he has gained working for small startups, as well as, Fortune 500 companies.Follow on Twitter More Content by Chris Altman