Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 527
meallhour Avatar asked Mar 15 '18 02:03

meallhour


People also ask

How do I clone only one branch from the repo?

You can clone a specific branch from a Git repository using the git clone –single-branch –branch command. This command retrieves all the files and metadata associated with one branch. To retrieve other branches, you'll need to fetch them later on.

Can I clone only one branch git?

There are two ways to clone a specific branch. You can either: Clone the repository, fetch all branches, and checkout to a specific branch immediately. Clone the repository and fetch only a single branch.

How do I pull a specific branch in git?

To pull changes from a specific branch in Git, first, launch the “Git Bash” on your system. Next, move to the Git local repository using the “cd” command. After that, execute the “$ git pull origin main” command to pull all changes from the origin and main branch and merge them in the local checked-out branch.


2 Answers

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
like image 193
VonC Avatar answered Oct 19 '22 01:10

VonC


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
like image 31
meallhour Avatar answered Oct 19 '22 01:10

meallhour