英文:
How to copy a git repo to a new repo without syncing to the original
问题
I am attempting to take a remote repo on GHE and make a full identical copy of it in another remote repo on GHE such that cloning either one will provide the exact same local repo with indication that the other one exists. This should include all branches and the full history.
Originally I tried
Git clone --bare original-repo-url
git push --mirror new-repo-url
I was under the impression that the push --mirror somehow syncs the two remote repos but believe this may be wrong. Doing the above would the two remotes be synced? I believe currently I was mistaken and either needed to remove the remote of the local using
git remote rm origin
or just making a new local clone of the new repo so that my local is not linked to the original-repo remote anymore. Is this correct?
英文:
I am attempting to take a remote repo on GHE and make a full identical copy of it in another remote repo on GHE such that cloning either one will provide the exact same local repo with indication that the other one exists. This should include all branches and the full history.
Originally I tried
Git clone --bare original-repo-url
git push --mirror new-repo-url
I was under the impression that the push --mirror somehow syncs the two remote repos but believe this may be wrong. Doing the above would the two remotes be synced? I believe currently I was mistaken and either needed to remove the remote of the local using
git remote rm origin
or just making a new local clone of the new repo so that my local is not linked to the original-repo remote anymore. Is this correct?
答案1
得分: 1
Remotes don't "sync" with each other. All syncing in Git is done by pulling and pushing via clones.
git push --mirror
将所有引用(本地分支、远程分支、标签)作为本地引用推送。
git push --all
仅推送本地分支,会忽略任何没有本地分支的远程分支。
只使用git push --mirror
一次。如果再次执行它,会出现奇怪的情况。
一旦你的新仓库被填充,要么创建新的克隆,要么使用git remote set-url origin <newurl>
来更新现有克隆的远程仓库。
Git仓库之间只通过它们的远程仓库连接在一起。删除远程仓库。
正常使用git clone
。这是原始仓库的完整副本,包括其历史、分支和标签。
克隆会创建一个指向原始仓库的远程仓库,名为"origin"。删除它:git remote rm origin
。这将删除所有远程分支和标签(origin/main、origin/v1.1等)。现在,你的克隆仓库没有远程连接。
从评论中讨论中,让我们走一遍这个过程。
你有一个远程仓库,比如位于git:example.com/repo.git。
你有一个从这个远程仓库克隆的本地仓库。
你想要...
- 复制远程仓库。
- 不让它与原始仓库关联。
- 并让本地仓库指向这个复制品。
要创建不关联原始仓库的远程复制品...
- 创建原始远程仓库的裸仓库克隆:
git clone --bare git:example.com/repo.git
。 - 删除它的原始远程:
git remote rm origin
。 - 将其上传到服务器,就像上传其他文件一样。
现在你有了原始仓库的复制品,没有与原始仓库的关联。假设它在git:example.com/copy.git。
然后,要创建复制品的本地克隆...
- 克隆一个新的本地仓库:
git clone git:example.com/copy.git
。 - 或者将现有本地仓库指向复制品:
git remote set-url origin git:example.com/repo.git
,然后执行git pull
以确保它是最新的。
英文:
Remotes don't "sync" with each other. All syncing in Git is done by pulling and pushing via clones.
git push --mirror
takes all refs (local branches, remote branches, tags) and pushes them as local references.
git push --all
will only push local branches. This will miss any remote branches which don't have local ones.
Only use git push --mirror
once. If you do it again things will get weird.
Once your new repository is populated, either make new clones, or update the remotes on your existing clones with git remote set-url origin <newurl>
.
<strike>
Git repositories are only connected to each other through their remotes. Delete the remote.
git clone
as normal. This is a complete copy of the original repository, its history, branches, and tags.
Cloning creates a remote to the original repo called "origin". Delete it: git remote rm origin
. This will delete all remote branches and tags (origin/main, origin/v1.1, etc). Now your cloned repo has no remote and no connection to the original.
From discussion in the comments, let's walk through this.
You have a remote repo, let's say it's at git:example.com/repo.git.
You have a local repo cloned from this remote.
You want to...
- Make a copy of the remote.
- Which does not refer to the original.
- And have a local repo point at the copy.
To make the unattached remote copy...
- Make a bare clone of the remote:
git clone --bare git:example.com/repo.git
. - Remove its origin remote:
git remote rm origin
. - Upload it to its server like you would any other files.
Now you have a copy of the original with no reference to the original. Let's say it's at git:example.com/copy.git.
Then to have a local clone of the copy...
- Clone a new local repo:
git clone git:example.com/copy.git
- Or point your existing local repo at the copy:
git remote set-url origin git:example.com/repo.git
andgit pull
to make sure it's up to date.
</strike>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论