WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Git · Advanced · question 46 of 100

How do you split a repository into multiple smaller repositories while preserving history?

📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

There are several scenarios where you may need to split a large Git repository into multiple smaller repositories while preserving the history. Here are a few methods you can use to accomplish this:

Use git filter-branch: This is a powerful and flexible tool that allows you to rewrite the history of a Git repository. One way to use git filter-branch to split a repository is to create a new branch that contains only the files you want to split off, and then use git filter-branch to remove all other files from the branch history.

    # Create a new branch containing only the files to split off
    git checkout -b new-branch
    git filter-branch --subdirectory-filter path/to/files
    
    # Create a new repository for the split-off files
    mkdir new-repo
    cd new-repo
    git init
    git pull /path/to/original-repo new-branch

This will create a new repository with only the files from the path/to/files directory and their full history.

Use git subtree: This is a Git extension that allows you to split a repository into multiple smaller repositories. You can use git subtree to extract a directory from the original repository into a new repository, while preserving the commit history.

    # Create a new repository for the split-off files
    git init new-repo
    cd new-repo
    
    # Extract the directory from the original repository into the new repository
    git subtree split --prefix=path/to/files -b new-branch
    git pull /path/to/original-repo new-branch

This will create a new repository with only the files from the path/to/files directory and their full history.

Use git clone: This method involves cloning the original repository and then removing the unwanted files from the cloned repository.

    # Clone the original repository
    git clone /path/to/original-repo new-repo
    cd new-repo
    
    # Remove the unwanted files from the cloned repository
    git filter-branch --tree-filter 'rm -rf path/to/files' HEAD

This will remove the unwanted files from the cloned repository, while preserving the commit history.

In summary, there are several methods you can use to split a Git repository into multiple smaller repositories while preserving the history. These methods include using git filter-branch, git subtree, and git clone with git filter-branch. Each method has its own strengths and weaknesses, so you should choose the one that best fits your specific situation.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Git interview — then scores it.
📞 Practice Git — free 15 min
📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

All 100 Git questions · All topics