我在GitHub上远程追踪我的master
和development
分支。我该如何同时克隆这两个分支呢?
看到这个问题的其中一个答案,我注意到可以将其缩短:
for branch in `git branch -r | grep -v 'HEAD\|master'`; do
git branch --track ${branch##*/} $branch;
done
但是要注意,如果其中一个远程分支的名称为admin_master,它不会被下载!
grep
,以改善过滤器以避免误报。 - tripleee我编写了这些小的PowerShell函数,以便能够检出所有在origin远程的Git分支。
Function git-GetAllRemoteBranches {
iex "git branch -r" <# get all remote branches #> `
| % { $_ -Match "origin\/(?'name'\S+)" } <# select only names of the branches #> `
| % { Out-Null; $matches['name'] } <# write does names #>
}
Function git-CheckoutAllBranches {
git-GetAllRemoteBranches `
| % { iex "git checkout $_" } <# execute ' git checkout <branch>' #>
}
更多的Git功能可以在我的 Git 设置存储库中找到。
git clone --mirror
用于原始仓库很有效。
git clone --mirror /path/to/original.git
git remote set-url origin /path/to/new-repo.git
git push -u origin
要创建一个包含git主机(github/bitbucket等)中存储的所有分支、引用、标签等的“完全”备份,请运行:
To create a "full" backup of all branches+refs+tags+etc stored in your git host (github/bitbucket/etc), run:
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主机仅在每个存储库的“main”分支上搜索代码,如果您运行 git log -S"specialVar"
,则会看到所有分支上的所有代码。
注意:如果您想在日常工作中使用此存储库,请运行:
git config --unset remote.origin.mirror
警告:如果您尝试在日常工作中使用此功能,可能会遇到奇怪的问题。如果您的IDE/编辑器正在进行某些自动提取操作,则您的本地master
可能会更新,因为您执行了git clone --mirror
。然后这些文件将出现在您的git暂存区中。我曾经遇到过这样的情况,即我在本地功能分支上..该分支没有提交,并且仓库中的所有文件都出现在暂存区中。真是疯狂。
git reset --hard
看起来很可怕。这到底是要做什么?需要解释一下。例如,这个命令的主要思想/要点是什么?请通过编辑您的答案进行回复,而不是在评论中回复(不要包含“编辑:”,“更新:”或类似内容 - 答案应该看起来像今天写的)。 - Peter Mortensengit reset --hard
只是丢弃自上次提交以来的任何更改,然后将 HEAD 设置为该提交。真的一点也不可怕。参考:https://git-scm.com/docs/git-reset - Scopperloitgit checkout --recurse-submodules
而不是重置呢?这样做不是更全面备份整个仓库,而不仅仅是重置单个仓库吗? - Andry--recurse-submodules
,你也可以将相同的 cli 选项传递到 git clone
中,但是如果你使用 --mirror
,该选项将被忽略。(参见文档:https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---recurse-submodulesltpathspecgt)也许这种方法可以使用某种 git submodule foreach 'do clone --mirror' --recursive
脚本来进行递归镜像克隆?(参见文档:https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-foreach--recursiveltcommandgt) - Devin Rhode除了用户nobody的回答,其他答案都不太合适。
我在将一个仓库从一台服务器/系统移动到另一台时遇到了问题。当我克隆仓库时,它只为主分支创建了一个本地分支,因此当我推送到新的远程仓库时,只有主分支被推送。
所以我发现这两种方法非常有用。
方法1:
git clone --mirror OLD_REPO_URL
cd new-cloned-project
mkdir .git
mv * .git
git config --local --bool core.bare false
git reset --hard HEAD
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
git clone OLD_REPO_URL
cd new-cloned-project
git clone-branches
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo
以下是使用 awk 的答案。如果在新的存储库上使用,此方法应该足够。
git branch -r | awk -F/ '{ system("git checkout " $NF) }'
现有的分支将被简单检出,或声明为已经在其中,但可以添加过滤器以避免冲突。
也可以修改它,使其调用显式的git checkout -b <branch> -t <remote>/<branch>
命令。
或者我们可以指定远程分支。这是基于murphytalk的answer。
git branch -r | awk '{ system("git checkout -t " $NF) }'
当发生冲突时,它会抛出致命错误消息,但我认为它们无害。
这两个命令都可以设置别名。
参考nobody的answer,我们可以使用以下命令来创建别名:
git config --global alias.clone-branches '! git branch -r | awk -F/ "{ system(\"git checkout \" \$NF) }"'
git config --global alias.clone-branches '! git branch -r | awk "{ system(\"git checkout -t \" \$NF) }"'
个人建议使用track-all
或track-all-branches
。
$ git clone https://github.com/BrandonBlair/elegantframeworks.git
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/config_recipe
remotes/origin/functionaltests
remotes/origin/master
remotes/origin/parallel
remotes/origin/parametrize
remotes/origin/parametrize_data_excel
remotes/origin/unittesting
remotes/origin/unittesting1
然后,在遇到一些git checkout
的问题之后,最终对我有效的方法是:
$ for b in `git branch -a | cut -c18- | cut -d\ -f1`; do git checkout $b; git stash; done
接下来,这是我的分支:
$ git branch -a
config_recipe
functionaltests
master
parallel
parametrize
parametrize_data_excel
unittesting
* unittesting1
remotes/origin/HEAD -> origin/master
remotes/origin/config_recipe
remotes/origin/functionaltests
remotes/origin/master
remotes/origin/parallel
remotes/origin/parametrize
remotes/origin/parametrize_data_excel
remotes/origin/unittesting
remotes/origin/unittesting1
remotes/origin/
,然后过滤空格分隔符。可以说,我只需要用一个cut
命令就可以grep
出HEAD
,但这个问题留给评论区讨论。git checkout
任何你想要的即可。我正在使用Atlassian的代码库托管服务Bitbucket。因此,我尝试遵循他们的文档。这对我来说非常完美。使用以下简单和短的命令,您可以检出您的远程分支。
首先克隆您的代码库,然后切换到目标文件夹。最后,别忘了获取并检出:
git clone <repo> <destination_folder>
cd <destination_folder>
git fetch && git checkout <branch>
就是这样。下面给出一个更实际的例子:
git clone https://username@bitbucket.org/team/repository.git project_folder
cd project_folder
git fetch && git checkout develop
您可以在文档中找到有关以下命令的详细信息: Clone 命令,Fetch 命令,Checkout 命令
用于复制粘贴到命令行中:
git checkout master ; remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do git branch -D $brname ; git checkout -b $brname $remote/$brname ; done ; git checkout master
git checkout master ;
remote=origin ;
for brname in `
git branch -r | grep $remote | grep -v master | grep -v HEAD
| awk '{gsub(/^[^\/]+\//,"",$1); print $1}'
`; do
git branch -D $brname ;
git checkout -b $brname $remote/$brname ;
done ;
git checkout master
它基于 VonC 的 the answer。
git branch -a
)会展示远程仓库中的分支,但是如果你试图检出它们中的任何一个,你将处于'脱离HEAD'状态。第二个最受赞同的回答回答了另一个问题(即如何拉取所有分支,并且再次强调,这仅适用于你在本地跟踪的分支)。其中一些评论指出,你可以使用shell脚本解析git branch -a
的结果来本地跟踪所有远程分支。总结:没有Git原生方法可以实现你想要的功能,而且也许这并不是一个好主意。 - Day Davis Waterburyscp some_user@example.com:/home/some_user/project_folder ~
不确定这种解决方案是否适用于GitHub... - snapfractalpop