リポジトリを 2 つに分割する
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:
||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
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
git clone email@example.com:tutorials/splitpractice.git freshrepo
Cloning with this command creates a
freshrepodirectory on your local system that contains the
- Change directory into the root of the
List the contents of directory.
You should see the following:
bigdir lildir lildir2
At this point,
freshrepois a git repository that is linked back to the
splitpracticerepository on Bitbucket.
Remove the link back to the Bitbucket remote
git remote rm origin
You can verify the command worked by listing the contents of the
Remove anything from the repository that is not in the
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
Link the local
freshreporepository to the
freshreporemote repository on Bitbucket.
git remote add origin https://firstname.lastname@example.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 master
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.
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
Using Mercurial 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 use
freshrepo for the new repository name. You'll be refactoring the hgsplitpractice repository an existing public repository on Bitbucket.
Enable the convert extension
The Mercurial convert extension is a relatively easy way to convert a directory. Before you can do use it, you need to enable the extension. Enable the extension by doing the following:
- Open a terminal on your local system.
- Edit the
Add the following to the configuration:
Create the new repository on Bitbucket and clone the old locally
You'll need to create the new repository on Bitbucket and clone the old locally by doing the following:
- Open Bitbucket in your browser and log into your account.
- Create a new Mercurial repository called
This is the remote repo where your new repository will end up.
- Open a terminal on your local system.
hgsplitpracticerepository to your local system.
hg clone https://bitbucket.org/tutorials/hgsplitpractice
Split the original repository locally
The conversion extension takes a
mapfile option. The
mapfile can contain any of the following directives:
# comment line include /path/to/file exclude /path/to/file rename from/file to/file
hgsplitpractice requires calling the
convert extension and passing it a
- Navigate to the parent directory of the repository.
- Create a file called
Add the following directives:
All the file names are relative to the repository root.
Convert the repository.
hg convert -s hg -d hg --filemap mymapfile hgsplitpractice hgfreshrepo
Push the new repository back to Bitbucket
- Change directory to the new
Update the repository:
List the repo contents to verify the conversion:
$ ls bigdir 1 2 3 4 5 6 7 8 9
- Create a
.hg/hgrcfile in the repository.
Add the URL for your remote
hgfreshrepoto the configuration:
[paths] default = https://email@example.com/tutorials/hgfreshrepo
The URL depends on which protocol you are using to connect. In this case, the protocol is HTTPS instead of SSH.
- Commit the new repository.
- Push the changes up to Bitbucket.
Now, view your
hgfreshrepo repository on Bitbucket. You'll find source files and their history show the full history. How do you know? Compare the original commit of
hgsplitpractice against your
hgfreshrepo repository. Notice, however, the history for source file 1 was retained by the move.
hgfreshrepo/bigdir is totally separate from the original
hgsplitpractice/bigdir directory. If you were splitting a repository in a production environment, you would probably remove the
hgsplitpractice/bigdir folder from