Split a repository in two
A code repository typically has multiple directories. For example, you could have separated your project's features into appropriately named directories within your repo. Occasionally, you might need to reorganize (or refactor) your repo's code. For example, you might want to move a directory from an existing project's repository into a new repository of its own.
When refactoring, it is good practice to retain in the new project all the source history from the old project. The procedures on this page explain how to move a directory from one repository into another and retain the history.
Using Git to split-out a directory
Before you begin to split-out a directory, you should decide on a name for your new project. In this example, you'll be refactoring with the following:
Repository | Description |
|---|---|
| An existing Bitbucket repository created just to practice with. |
| The directory in |
| A new repository for holding the contents of |
To run this example, log into Bitbucket Cloud and do the following:
Create new Git repository called
freshrepo.
This repository is under your account.On your local system, open a command-line window.
Clone the splitpractice repository to your local system into the clone
freshrepo.git clone git@bitbucket.org:tutorials/splitpractice.git freshrepoCloning with this command creates a
freshrepodirectory on your local system that contains thesplitpracticerepository.Change directory into the root of the
freshrepo.List the contents of directory.
You should see the following:bigdir lildir lildir2At this point,
freshrepois a git repository that is linked back to thesplitpracticerepository on Bitbucket.Remove the link back to the Bitbucket remote
splitpracticerepository.git remote rm originYou can verify the command worked by listing the contents of the
freshrepo/.git/configfile.Remove anything from the repository that is not in the
bigdirdirectory.git filter-branch --index-filter 'git rm --cached -r lildir lildir2' -- --allThis command goes through the history and files and removes anything that is not in
bigdir.Link the local
freshreporepository to thefreshreporemote repository on Bitbucket.git remote add origin https://tutorials@bitbucket.org/tutorials/freshrepo.gitYou can get the URL to add from the repo's Clone option on Bitbucket. This examples uses HTTPS but you could also use SSH.
Push to the newly refactored repo back to Bitbucket.
git push origin main
Now, view your freshrepo repository on Bitbucket. You'll find source files and their history show only the bigdir directory activity. The history for the "lil" directories. How do you know? Compare the original commit of splitpractice against your freshrepo repository. Notice, however, the history for source file 1 was retained by the move.
The new freshrepo/bigdir is totally separate from the original splitpractice/bigdir directory. If you were splitting a repository in a production environment, you would probably remove the splitptractice/bigdir folder from splitpractice.
Was this helpful?