Git mirror a repo to specific branch

11,210

Solution 1

Create the repo on your server. Elsewhere (not in the server repo), clone just the branch with

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; git push --tags -u mine

"mine" is the shorthand name of your repo, it could be any string, replacing the default "origin".

This will gather the entire history leading up to branch_name, but no commits that are not ancestral to it.

Solution 2

  1. clone old branch to local
git clone --bare git_url --single-branch --branch  old_branch_name local_dir
  1. change dir
cd local_dir
  1. add new remote
git remote add  new_origin new_git_url 
  1. push to new remote
git push new_origin  old_branch_name 
Share:
11,210
Admin
Author by

Admin

Updated on June 28, 2022

Comments

  • Admin
    Admin almost 2 years

    our company try to fork a github project to our own git server,then we can add our own features on it. We just want to checkout a specific branch, and keep all branches and tags up to this branch, then copy(mirror ?) to our git server.

  • Jeff Puckett
    Jeff Puckett almost 8 years
    Good looking answer. But does this also grab the tags specific to the branch?
  • stolenmoment
    stolenmoment almost 8 years
    Ah! My favorite git mistake: you also need to "git push -- tags" to get the tags into the new report.
  • Admin
    Admin almost 8 years
    @stolenmoment, thanks for this great answer. But how can I mirror all the repo status up to the branch that I want? Means, if I want to mirror the branch-0.7, at this commit point, there will be branch-0.1, branch-0.2...(including tags of course), how can I do this? The purpose is I want to copy all the repo information up to the specific commit or the branch I want.