How to copy only single branch from one git repo to another?
Asked Answered
Y

3

10

I am using the following steps to duplicate all branches to the new-repository.

git clone --bare https://github.com/exampleuser/old-repository.git
# Make a bare clone of the repository

cd old-repository.git
git push --mirror https://github.com/exampleuser/new-repository.git
# Mirror-push to the new repository

Now, I have added two new branches in old-repository.git and want to move only those 2 branches to new-repository.git

What git commands are required to perform it?

Yetty answered 15/3, 2018 at 2:20 Comment(3)
I don't have an exact answer, but what is your motivation for wanting to move only two branches?Criticize
I am moving from old-repo to new-repo. I have already moved all the branches to new-repo except 2 newly created branches in old-repoYetty
Perhaps you can try just pushing those new branches to the other repository, see here.Criticize
M
21

You can add new_repo as a remote for old repo: this is more convenient for pushing:

cd old_repo
git remote add new /path/to/new/repo
git push new newBranch1
git push new newBranch2
Midships answered 15/3, 2018 at 5:49 Comment(0)
Y
6

Clone old branch

git clone --single-branch --branch branch_name github_repo_url

Tell git where your repo is:

git remote add mine your_repo_url

Then, push the branch to your repo with:

git push -u mine
Yetty answered 15/3, 2018 at 7:4 Comment(1)
You don't have to clone your repo, you can directly point to the new one from the old one.Midships
C
0

cd old_repo
git remote add new <New_git_repo_cloning_link>
git push new branchName

Cecilius answered 10/1, 2023 at 12:40 Comment(2)
Isn't this essentially what I put in my own answer five years ago?Midships
Duplicate of already accepted answerElectret

© 2022 - 2024 — McMap. All rights reserved.