简体   繁体   English

带有所有分支/标签的远程git存储库的完整本地副本

[英]Full local copy of remote git repository with all branches/tags

I would like to know, how you would proceed to make a full local copy of a remote git repository, that can be used in-place of the original repository. 我想知道,您将如何继续制作远程git存储库的完整本地副本,该副本可以在原始存储库中使用。

All guides I found so far, describe how to copy a remote git repository to another remote one. 到目前为止,我发现的所有指南都描述了如何将远程git存储库复制到另一个远程git存储库。

One such guide would be: 这样的指南之一是:

How to move a full Git repository : 如何移动完整的Git存储库

$ git clone https://github.com/Sample/sample.git
$ cd sample.git
$ git checkout remotes/origin/HEAD
$ git checkout remotes/origin/develop
$ git checkout remotes/origin/master
$ git checkout remotes/origin/micros-integration
$ git checkout remotes/origin/release/0.4.8
$ git checkout remotes/origin/release/0.6.0
$ git checkout remotes/origin/remove-keygen
$ git fetch --tags
$ git remote rm origin
$ git remote add origin https://github.com/Target/target.git
$ git push origin --all
$ git push --tags

Yet another one: 还有一个:

Mirroring a git repository without a local copy : 镜像没有本地副本的git仓库

$ git clone --mirror git@github.com/Sample/sample.git
$ cd upstream-repository.git
$ git push --mirror git@github.com/Target/target.git

So how would you proceed to create a local copy without being linked to the original repository? 那么,如何在不链接到原始存储库的情况下继续创建本地副本? And what should be done, if you want to only maintain specific branches and remove everything else including all history (pull-requests, code changes,...)? 如果只想维护特定的分支并删除所有其他内容,包括所有历史记录(拉请求,代码更改等),应该怎么做?

To copy a repo including all branches and tags (and other refs), use 要复制包含所有分支和标签(和其他引用)的仓库,请使用

git clone --mirror <repo-url>

To unlink your copy from the original, cd into the repo directory and 要将副本与原始副本断开链接,请使用cd进入repo目录,然后

git remote remove origin

If you only want to preserve a subset of branches, the best procedure depends on specifics; 如果您只想保留分支的子集,则最佳过程取决于具体细节。 you might be able to restrict what you clone, but exactly how to do it depends on what you want to keep. 您也许可以限制克隆的内容,但是确切的操作方法取决于您要保留的内容。

As a general solution, after cloning you can delete the branches (and other refs) you don't want from your clone, and then run gc 作为一般解决方案,克隆后,您可以从克隆中删除不需要的分支(和其他引用),然后运行gc

git branch -D unwanted-branch
git tag -D unwanted-tag
git gc --aggressive --prune=all

gc will not remove anything reachable from any ref (branch, tag, etc) - or any reflog, but a fresh clone shouldn't have any reflog baggage. gc不会从任何引用(分支,标签等)或任何引用日志中删除任何可访问的内容,但是新克隆的副本不应包含任何引用日志包。 You can verify what refs exist with 您可以验证哪些引用存在

git for-each-ref

If gc is giving you trouble, another option is to clone the clone 如果gc给您带来麻烦,另一种选择是克隆克隆

git clone --mirror file://localhost/path/to/first/clone

which should not be expected to transfer unnecessary objects. 不应期望它们转移不必要的对象。 (But again, anything reachable will be copied unless your clone command limits that copying, such as through --single-branch or shallow history options.) (但是同样,任何可达的内容都会被复制,除非您的克隆命令限制了该复制,例如通过--single-branch或浅历史记录选项。)

With either method, an object is kept if it is reachable, which could include the commits that make up a branch you've deleted (eg if that branch has been merged into a branch you haven't deleted). 无论使用哪种方法,都会保留对象的可访问性,其中可能包括构成已删除分支的提交(例如,如果该分支已合并到尚未删除的分支中)。 So if you have specific requirements to limit history beyond what is done by the above procedures, you may need more sophisticated tools (like shallow histories) and the procedure may depend on a lot of specifics. 因此,如果您有特定的要求来限制历史记录,而不是通过上述过程来完成,则可能需要更复杂的工具(如浅历史记录),并且该过程可能取决于很多细节。

Also note that a pull request is not a git concept; 另请注意,拉取请求不是git概念; PR's are an invention of environments that host git remoets. PR是托管git remoets的环境的发明。 You might have merge commits resulting from PR's, and those are treated just like any other history. 您可能有PR产生的合并提交,并且这些合并提交的处理方式与其他任何历史记录一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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