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

splitpractice

An existing Bitbucket repository created just to practice with.

bigdir

The directory in splitpractice you split out.

freshrepo

A new repository for holding the contents of bigdir. You'll create this using the directions below.

To run this example, log into Bitbucket Cloud and do the following:

  1. Create new Git repository called freshrepo.
    This repository is under your account. 

  2. On your local system, open a command-line window.

  3. Clone the splitpractice repository to your local system into the clone freshrepo.

    1 git clone git@bitbucket.org:tutorials/splitpractice.git freshrepo

    Cloning with this command creates a freshrepo directory on your local system that contains the splitpractice repository.

  4. Change directory into the root of the freshrepo.

  5. List the contents of directory.
    You should see the following:

    1 bigdir lildir lildir2

    At this point, freshrepo is a git repository that is linked back to the splitpractice repository on Bitbucket.

  6. Remove the link back to the Bitbucket remote splitpractice repository.

    1 git remote rm origin

    You can verify the command worked by listing the contents of the freshrepo/.git/config file.

  7. Remove anything from the repository that is not in the bigdir directory.

    1 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.

  8. Link the local freshrepo repository to the freshrepo remote repository on Bitbucket.

    1 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.

  9. Push to the newly refactored repo back to Bitbucket.

    1 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.  

Last modified on Jun 9, 2021
Cached at 6:35 PM on Oct 27, 2021 |

Additional Help