简体   繁体   中英

Fully fork a git repo?

I've not quite managed to piece together what I want to know from the multitude of answers on here and blog posts, so I'm asking this question:

How do I fork a git repo such that I get all the branches & tags of the upstream?

What I've tried is:

git init
git remote add origin <ORIGIN_URL> # i.e. my repo
git remote add upstream <UPSTREAM_URL> # i.e. the repo I want to fork
git fetch upstream
git push --all origin

But that last line fails when I would expect it to work. I get this:

$ git push --all origin
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to <ORIGIN_URL>

Any ideas?

EDIT:

Maybe you can think of this question as "What does GitHub actually do when you click the fork button?", as that's the behaviour I am trying to replicate.

Try a mirror:

git clone --mirror

This way, the remote (named origin ) is created without a namespace for branches. IOW, the refs/heads/master in the mirror corresponds exactly (and is bound to) the refs/heads/master in the origin repository.

Now just use this repository instead of original upstream repository. If you ever want to update the mirror, do something like this in it:

cd upstream-mirror.git && git --bare fetch upstream

Warning: if you ever push into your mirror, modifying the upstreams branches, the upstream repo and your mirror will diverge. and push (without -f option) will fail, to prevent that.

Mirror is actually an exact mirror of the upstream repo, with you, as its owner, having an option to modify it.

This is what I've come up with as the best way to do what I wanted which seems to work and gets a similar behaviour to what GitHub does:

git clone --mirror <UPSTREAM> myrepo
cd myrepo
git remote rename origin upstream
git remote add origin <MY_ORIGIN>
git push --all
git push --tags

And then tweak the .git/config to change the upstream remote to not mirror anymore as that's certainly not what I want now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM