我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?


当前回答

所有分支机构

从Git项目下载所有分支的脚本

安装:

sudo git clone https://github.com/marceloviana/allBranches.git && sudo cp -rfv allBranches/allBranches.sh /usr/bin/allBranches && sudo chmod +x /usr/bin/allBranches && sudo rm -rf allBranches

准备好的现在只需调用命令(allBranches)并告诉Git项目目录,您希望下载所有分支

Use

示例1:

~$ allBranches /var/www/myproject1/

示例2:

~$ allBranches /var/www/myproject2/

示例3(如果已经在项目目录中):

~$ allBranches ./

or

~$ allBranches .

查看结果:

git branch

参考:

存储库allBranches GitHub:https://github.com/marceloviana/allBranches

其他回答

我试图找出如何删除我在本地删除的远程分支。起源不是我的,我不想经历重新克隆一切的麻烦。

这对我有用:

假设您需要在本地重新创建分支:

git checkout -b recreated-branch-name
git branch -a (to list remote branches)
git rebase remotes/remote-origin/recreated-branch-name

因此,如果我从gituser/master分支到sjp,然后将其分支到sjp/mynewbranch,则如下所示:

$ git checkout -b mynewbranch

$ git branch -a
  master
  remotes/sjp/master
  remotes/sjp/mynewbranch

$ git fetch (habit to always do before)

$ git rebase remotes/sjp/mynewbranch

执行远程存储库的裸克隆,将内容保存到.git目录

git clone --bare remote-repo-url.git localdirname/.git

(使用gitclone--bare或gitinit--bare创建的裸git存储库是一个存储存储库,它没有工作目录,您无法在其中创建或修改文件。)

将目录更改为本地目录

cd localdirname

使您的git存储库可修改

git config --bool core.bare false

还原工作目录

git reset --hard

列出所有分支机构

git branch -al

为什么你只看到“大师”

gitclone下载所有远程分支,但仍将其视为“远程”,即使文件位于新存储库中。有一个例外,那就是克隆过程从名为“master”的远程分支创建了名为“master”的本地分支。默认情况下,gitbranch只显示本地分支,这就是为什么您只看到“master”。

gitbranch-a显示所有分支,包括远程分支。


如何获得本地分支机构

如果你真的想在一个分支上工作,你可能需要一个“本地”版本的分支。要从远程分支创建本地分支(不需要签出它们,从而更改工作目录的内容),你可以这样做:

git branch branchone origin/branchone
git branch branchtwo origin/branchtwo
git branch branchthree origin/branchthree

在本例中,branchone是基于origin/branchone创建的本地分支的名称;如果您希望创建具有不同名称的本地分支,可以执行以下操作:

git branch localbranchname origin/branchone

创建本地分支后,可以使用git分支查看它(记住,不需要-a来查看本地分支)。

这个解决方案使我能够将一个存储库“复制”到另一个:

git merge path/to/source.git --mirror
cd source.git
git remote remove origin
git remote add origin path/to/target.git
git push origin --all
git push origin --tags

在目标存储库中,我可以看到与原始存储库相同的分支和标记。

要创建存储在git主机(github/bitbucket/etc)中的所有分支+引用+标记+等的“完整”备份,请运行:

mkdir -p -- myapp-mirror
cd myapp-mirror
git clone --mirror https://git.myco.com/group/myapp.git .git
git config --bool core.bare false
git config --bool core.logAllRefUpdates true
git reset --hard # restore working directory

这是根据我从其他答案中学到的一切整理而成的。

然后,您可以使用此本地回购镜像转换到不同的SCM系统/git主机,也可以将其作为备份。它作为搜索工具也很有用,因为大多数git主机只搜索每个repo的“main”分支上的代码,如果您使用git log-s“specialVar”,您将看到所有分支上的所有代码。

注意:如果您想在日常工作中使用此回购,请运行:

git config --unset remote.origin.mirror

警告:如果你试图在日常工作中使用它,你可能会遇到奇怪的问题。如果您的ide/编辑器正在进行一些自动提取,您的本地主控器可能会更新,因为您做了gitclone--mirror。然后这些文件会出现在git暂存区。事实上,我有一种情况,我在当地的一个特色分支。。该分支没有提交,repo中的所有文件都显示在暂存区域中。只是疯子。