Code migration to another repository while preserving Git history.
Having multiple projects in a single repository is not a good idea. It's better to have them in separate repositories, as this makes it easier to manage permissions, set up clear CI/CD pipelines, and handle the code more efficiently.
If you move code from one folder to another repository, it's good to keep the Git history, so new team members can easily see past changes. Git can help us safely migrate the code to another repo with the filter-repo tool. Let's follow the steps below.
Prerequisites:
Git Installed: Ensure you have Git installed on your machine.
Install filter-repo and Python: Install filter-repo Tool and Python.
Access Permissions: You have read and write access to both repositories.
Backup: It's recommended to backup repositories or work in copies to prevent data loss.
Repository Details:
Source Repository: repo-1
Branch: feature/repo-1Branch
Destination Repository: repo-2
Branch: feature/repo-2Branch
Folder to Move: Replace <folder-to-move> with the actual path of the folder you want to move within repo-1.
Install filter-repo and Python.
- Download and install Python for Windows from this link. Make sure Python is added to your Environment variables Path while installing or later.
- Confirm that Python is added to your PATH and that you can run either the command python --version or python3 --version from your Git command line. In my case, the executable name is 'python'.
- Clone and install the git filer repo. Link to Clone git-filter-repo from GitHub.git clone https://github.com/newren/git-filter-repo.git
- Run the command git --exec-path to see your Git exe directory.
- From the git-filter-repo repo's root directory, copy the file git-filter-repo (about 169KB) into your Git exe directory.
- Check the installation in your command line where you use Git, and type the command git filter repo. If it works, you should get the message "No arguments specified.”
- If you get no message or an error message similar to "No such file or directory", then edit the git-filter-repo file that you copied into your Git exe directory. It's likely that your exe file has python3 instead of python in the shebang line. Change the first line from "python3" to "python" (as screenshot below).
- Check the git filter-repo command again. It should work now. :)
Steps to Move the Folder with Commit History.
- First, clone the destination repository(repo-2) and navigate into it. git clone https://your-url-to-repo-2 cd repo-2
- Add the Source Repository (repo-1) as a Remote source repository.git remote add source https://your-url-to-repo-1
Fetch the specific source branch from the source repository. git fetch source <feature/repo-1Branch>
Verify the branch is fetched properly. git branch --all You should see something like: remotes/source/feature/repo-1Branch If it’s listed, you can proceed with the git filter-repo command.
- Use git filter-repo to Isolate the Folder to migrate.Replace <folder-to-move> with the actual folder path you want to move (e.g., src/myfolder).git filter-repo --path <folder-to-move> --refs source/feature/repo-1Branch --force You can use the --force option with git filter-repo, but proceed with caution. The --force option bypasses the safeguard that prevents the filter-repo operation on a non-fresh clone. This can lead to changes being made to an existing repository, potentially affecting your repository history. It's recommended to back up your current repository before using --force to avoid any accidental data loss.
Switch to your destination branch. git checkout <feature/repo-2Branch> If the branch doesn't exist locally or in a remote repository, please use the command below:git checkout -b feature/repo-2Branch
Now, merge the source branch into your current branch. git merge --allow-unrelated-histories source/feature/repo-1Branch Note: The --allow-unrelated-histories flag is necessary because the repositories have different histories.
Push the changes to the Destination(repo-2) Repository. git push --set-upstream origin feature/repo-2Branch
Migrate code to custom destination path.
To migrate a folder along with its commit history from one repository to another and place it in a custom destination path, you will need to combine git filter-repo with the --path option and then use git mv to adjust the location of the folder in the destination repository.
Full Example Workflow:
Clone the destination repository and navigate into it as we did in the above steps, if you are doing it on the same repo, just fetch the source branch.
git fetch source <feature/repo-1Branch>
Replace <folder-to-move> with the actual folder path you want to move.
git filter-repo --path <folder-to-move> --refs source/feature/repo-1Branch
Checkout to branch, If the branch doesn't exist locally or in a remote repository, please use the -b flag in the command below:
git checkout -b feature/repo-2Branch
Now, merge the source branch into your current branch.
git merge --allow-unrelated-histories source/feature/repo-1Branch
Move the folders using the command below.
git mv <source/path/to/folder> <Destinatuion/path>
Commit the changes.
git commit -m “folder moved to required location”
Push the changes to the remote repository.
git push --set-upstream origin feature/repo-2Branch
Troubleshooting known issues:
when you are trying to add a new remote source. But the error message remote source already exists means that a remote with the name source has already been added to your local repository or you have to update it.
Here’s how to manage your remotes and resolve this issue:
List the existing remotes:
To see the remotes that have already been added to your local repository, use the following command:
git remote -v
This will list all remotes in the format:
source <remote-url> (fetch)
source <remote-url> (push)
Update an existing remote:
If you find that the remote source already exists and points to the wrong URL, you can update the URL with the following command:
git remote set-url source https://your-url-to-repo-1
Remove the existing remote and re-add a new remote (if necessary):
git remote remove source
After this, you can add the source new remote again:
git remote add source https://your-url-to-repo-1
Fetch from the new remote:
git fetch source
Comments
Post a Comment