Move Branches Seamlessly Between Repositories
约 228 字小于 1 分钟
2025-04-13
Sometimes we need to move branches between repositories. A practical way is through git bundle
:
Let's say we have two repositories, repo1
and repo2
, and we want to move a branch named feature-branch
from repo1
to repo2
. It might contain totally unrelated commits, but we want to keep the history of feature-branch
intact.
- repo1
- feature-branch
- main
- repo2
- main
Create a Bundle
Open a terminal and navigate to
repo1
. Create a bundle file that contains thefeature-branch
:cd /path/to/repo1 git bundle create feature-branch.bundle feature-branch
Move the Bundle to Somewhere You Can Access (Optional)
Navigate to
repo2
cd /path/to/repo2
Fetch the Bundle
git fetch /path/to/feature-branch.bundle feature-branch:feature-branch
提示
As for the syntax
feature-branch:feature-branch
, the branch name before the colon is the remote branch name, while the one after the colon is the local branch name. You should always consider filling both of them.
Now you have the feature-branch
in repo2
with all its history intact. It's safe to delete the bundle and the original branch in repo1
.