move directory to another repository

How to Move Directory to Another Git Repository

Often you may need to move a directory from one git repository to another. If you directly copy the folder to another git repository then all the commit history of your directory will be lost in the process and won’t be transferred to the new repository. In this article, we will learn how to move directory to another git repository while preserving its commit history.

How to Move Directory to Another Git Repository

Here are the steps to move directory to new repository, along with its commit history. Let us say you want copy directory data from primary-repo to secondary-repo.

1. Clone Repo

First we will clone repo which contains the directory to be transferred.

$ git clone 

2. Change Directory

Next, change directory to newly cloned repository.


3. Rewrite Git History

Next, you can use filter-branch command to rewrite git history by updating its branch history. Here is the syntax for the command.

$ git filter-branch --prune-empty --subdirectory-filter SUB_DIRECTORY_NAME BRANCH_NAME 

In the above command,

  • SUB_DIRECTORY_NAME – relative path of directory whose commit history you want to copy to new repo. It is basically the folder that you want to move.
  • BRANCH_NAME – Name of the branch from which directory is filtered. For example, master, develop, working, etc. This is required because the directory may have commit history in multiple branches. This will tell git to pick commit history from the appropriate branch.

So if you want to copy the folder /home/ubuntu/data run the following command. At this point, git would have separated the commit history for your directory, from the required branch. Please note, once you run this command, git will retain only the directory that you want to move and remove all other contents from you repository. Even in this case, each file will be as per the last commit in the BRANCH_NAME you specified.

4. Create New Repository

Next, create new repository on GitHub, Gitlab or any other cloud-based Git provider.

Set the remote URL of new repository as the origin of your local directory.

$ git remote set-url origin 

Next, verify that the git origin URLs have been updated.

$ git remote -v 

You will see something like the following.

# Verify new remote URL
> origin (fetch)
> origin (push)

Finally, push all the files to new repository.

$ git push -u origin BRANCH_NAME 

In this article, we have learnt how to move folder to new repository. You can also use the same trick on an existing repository. In this case, you will need to create a new branch in existing repository and set that URL as the origin of your folder, so that when you push the folder, it goes to the new branch in remote repository. Thereafter, you can merge it with the master of your remote repository to integrate this folder.

If you simply copy/move the folder to a new location and and push it to a new/existing repository, its commit history will be lost. Using the above steps, you can isolate the past commits of a directory and include them in the new destination repo of the directory.

Also read:

How to Move Directory to Another Partition in Linux
How to Search PDF Files in Linux
How to Schedule Task in Python
How to Import Python Modules by String Name
Call Python Function by String Name

Leave a Reply

Your email address will not be published.