How to migrate git repository with history and branches

Assume that you want to migrate git@example.com:someone/old-repository to git@example.com:someone/new-repository.git.

Migrate all branches and all tags

run the following command:

# clone the old repository
git clone git@git.example.com:someone/old-repository
cd old-repository

# add a new remote called "tmp"
git remote add tmp git@git.example.com:someone/new-repository.git

# checkout all branches
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
   git branch --track ${branch#remotes/origin/} $branch
done

# push to new repository. "--all" means all branch
git push tmp --all

# push all tags
git push --tags

Migrate only one branch

If you only need to migrate one branch, replace git push tmp --all with the following command:

# check out the branch you want to migrate. Here we migrate the "dev" branch.
git checkout dev
# push only one branch
git push tmp dev
Posted on 2022-03-29