使用与远程存储库相同的 git 创建本地存储库(本地 git 服务器与远程相同)

Create local respository using git same as remote respository (Local git server same as remote)

我是 GIT
的新手 我有一个远程 git 存储库。我想创建一个与远程相同的本地存储库(所有分支和标签都与远程相同)。
我想在我的本地存储库上测试我的 git 命令,然后我可以将它推送到远程。
许多 post 我看到人们告诉我要克隆或镜像,但这对我不起作用。

local rep <---> remote rep -> all branches <===> -> all branches -> all tags <===> -> all tags

现在我想将我的本地存储库用作服务器

git clone "D:\localrep\" 应具有远程服务器的所有功能(分支、标签和历史记录)

正如我在评论中告诉您的那样,git clone 不会(据我所知)自动为您在本地创建所有分支。 您可以使用 git branch.

检查本地存储库中的所有分支
$ git branch
* master

这里这个git 仓库只有一个分支:master 分支。如果您使用 git clone.
克隆它,它看起来会像这样 如果你想检查你的本地存储库知道哪些远程分支存在,你可以 运行 git branch -a。远程分支将是这种形式:

$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/other_branch
  remotes/origin/branch_two

带有"remotes/"的是远程仓库中的分支。所以在这里您看到远程存储库 origin 拥有 3 个分支(带有 remotes/origin/<branch_name> 的行):master、other_branch 和 branch_two。 如果您也想在本地使用它们,则必须创建它们,例如 git checkout -b

git checkout -b <new_local_branch> <remote_repository>/<remote_branch_name>

您将 <new_local_branch> 替换为您希望它在本地存储库中具有的名称,并将 <remote_repository>/<remote_branch_name> 替换为 git branch -a 输出中 remotes/ 之后的名称。
例如,要在本地创建分支 other_branch 作为具有相同名称的远程分支的副本:

git checkout -b other_branch origin/other_branch

然后你会看到 git branch 的输出将是

$ git branch
 master
* other_branch

如果你想自动执行它,你可以根据 git branch -a 的输出创建一个命令,如下所示:

git branch -a | grep "remotes/origin" | egrep -v "origin/(master|HEAD)$" | \
   sed "s:remotes/\([^/]\+/\(.\+\)\)$:git checkout -b  :" | bash

这将在本地创建除主分支之外的所有分支。如果还有其他分支,则不必创建,将它们添加到 egrep 的正则表达式中的“()”之间,用“|”

分隔