Migrating Part of a Code Repository While Retaining the Full Commit History

jefftian

Jeff

Posted on January 9, 2024

Migrating Part of a Code Repository While Retaining the Full Commit History

The purpose of this post is to explain how to migrate code with commit history using the tool git-filter-repo.

Background

Recently, I undertook a task that required migrating a specific folder from one project into a new code repository to preserve the commit history. After some effort, I finally discovered the git-filter-repo tool, which perfectly accomplished this job. I am documenting this experience here.

Install git-filter-repo

To start, install git-filter-repo. The easiest way to do this on Mac is by using brew.

brew install git-filter-repo
Enter fullscreen mode Exit fullscreen mode

It is an open-source tool, and the source code can be found at https://github.com/newren/git-filter-repo

Checkout a new branch

Create a new branch based on main branch in the original code repository for the partial migration.

git checkout -b feature/JIRA-123-github-migration
Enter fullscreen mode Exit fullscreen mode

Filter the directories and files that need to be migrated

If there are these files or directories that need to be migrated:

  • File Lambda.sln
  • File Lambda.Worker.sln.DotSettings.use
  • Directory Worker
  • Directory Worker.Tests

If you'd like to migrate them to folder ~/repos/NewLambda, then specify the files or directories that need to be migrated and the target folder using the --path and --target options, respectively.

git filter-repo --path Lambda.sln --path Lambda.Worker.sln.DotSettings.user --path Worker --path Worker.Tests --target ~/repos/NewLambda
Enter fullscreen mode Exit fullscreen mode

Check result

Confirm the migration results by checking if the directories have been successfully migrated and if the project files not in the migration scope are excluded. You can execute ls under ~/repos/NewLambda folder.

Verify the completeness of the commit history migration by using the command git log --oneline in the target directory, ~/repos/NewLambda.

Push the migrated to remote

If the migration is intended for GitHub, create a new repository on GitHub and add the remote origin to the local target directory.

Push the migrated files to the new repository on GitHub.

git push --set-upstream origin $(git branch --show-current)
Enter fullscreen mode Exit fullscreen mode

Submit a PR

Finally, submit a pull request from the migrated branch to the main branch for team members to review.

All commits are preserved after partially migrated

These steps are to migrate a code repository and retain the commit history using git-filter-repo.

đź’– đź’Ş đź™… đźš©
jefftian
Jeff

Posted on January 9, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related