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 freshrepo
Cloning with this command creates a
freshrepo
directory on your local system that contains thesplitpractice
repository.Change directory into the root of the
freshrepo
.List the contents of directory.
You should see the following:bigdir lildir lildir2
At this point,
freshrepo
is a git repository that is linked back to thesplitpractice
repository on Bitbucket.Remove the link back to the Bitbucket remote
splitpractice
repository.git remote rm origin
You can verify the command worked by listing the contents of the
freshrepo/.git/config
file.Remove anything from the repository that is not in the
bigdir
directory.git filter-branch --index-filter 'git rm --cached -r lildir lildir2' -- --all
This command goes through the history and files and removes anything that is not in
bigdir
.Link the local
freshrepo
repository to thefreshrepo
remote repository on Bitbucket.git remote add origin https://tutorials@bitbucket.org/tutorials/freshrepo.git
You 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?