Git: Move a Folder From One Repository to Another Without Losing Git History
Move folder from one repo to another

In one of my projects, initially, we added the data collector module to the primary repository that contains the source code of the main app. Yes, that was a mistake, we realized later. After a few weeks of development, the data collector module grew in size and the complexity increased when we added multiple sources for data collection. Hence, we decided to move the data collector module to its own repository. In the beginning, we thought we could just cut and paste the top-level folder of the data collector module to the new repository. This sounds very simple, right? But, wait, what about the history of changes in the data-collector module? If we cut and paste the folder, we’ll completely lose the commit history. Wouldn’t it be great if we can move a folder (with sub-folders and files) of the data collector module to the new repository along with the full revision history?

Git commit history is extremely useful. It serves as additional documentation of code with context. This article presents a step-by-step guide to move a folder from one Git repository to another with full revision history intact.

Move Folder From One Git Repository to Another With Revision History

Let us designate the main app repository as source and the data collector repository as target. The target repository may already exist or it can be a brand new repository. The guide remains applicable in both scenarios of the target repository. Let us designate the folder that we are going to move as folder-to-move. Here are the steps:

Step 1: In a temporary directory, clone the source repository

git clone <repo_url>/<source>.git
cd <source>

Step 2: Remove the link to the remote repository



git remote rm origin

Step 3: Remove files history, except for the folder we are going to move. After this step, only the content of the folder-to-move folder will be available in the root directory.

git filter-branch --subdirectory-filter <folder-to-move> -- --all

Step 4: Clean unwanted stuff

git reset --hard
git gc --aggressive 
git prune
git clean -fd

Step 5 (Optional): Move files and folders to a new parent folder and commit

# Move all files and folders to a new parent folder
mkdir <parent_folder_name>
mv * <parent_folder_name>

# Commit, but do not push
git add .
git commit -m "Move files to a directory"

Step 6: In the same temporary directory, clone the target repository



git clone <repo_url>/<target>.git
cd <target>

Step 7: Add a local remote to the source repository cloned before with a name (e.g. source). The first parameter is the name of the remote and the second parameter is the relative path to the source repository.

# Command: git remote add name url
git remote add <source> ../<source>

Step 8: Pull files and history from source into the target. Please use the remote name used in Step 7.

git pull <source> master --allow-unrelated-histories

Step 9: Remove the local remote added in Step 7

git remote rm <source>

Step 10: Commit changes in the target repository and push

git commit -m "Move folder"
git push

Finally, delete both the cloned repositories from the temporary directory. After completing the above steps, files in the folder-to-move folder of the source repository will be available in the target repository with the revision history preserved.

Git: Move a Folder From One Repository to Another Without Losing Git History
Be The First

Join our list to get instant access to new articles and weekly newsletter.

Tagged on:         
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x