[英]How do I clone all remote branches?
First, clone a remote Git repository and cd
into it:首先,克隆一个远程Git存储库并cd
进入它:
$ git clone git://example.com/myproject
$ cd myproject
Next, look at the local branches in your repository:接下来,查看存储库中的本地分支:
$ git branch
* master
But there are other branches hiding in your repository!但是您的存储库中还隐藏着其他分支! See these using the -a
flag:使用-a
标志查看这些:
$ git branch -a
* master
remotes/origin/HEAD
remotes/origin/master
remotes/origin/v1.0-stable
remotes/origin/experimental
To take a quick peek at an upstream branch, check it out directly:要快速查看上游分支,请直接查看:
$ git checkout origin/experimental
To work on that branch, create a local tracking branch, which is done automatically by:要在该分支上工作,请创建一个本地跟踪分支,该分支通过以下方式自动完成:
$ git checkout experimental
Branch experimental set up to track remote branch experimental from origin.
Switched to a new branch 'experimental'
Here, "new branch" simply means that the branch is taken from the index and created locally for you.在这里,“新分支”只是意味着从索引中获取分支并在本地为您创建。 As the previous line tells you, the branch is being set up to track the remote branch, which usually means the origin/branch_name branch.正如上一行告诉您的那样,正在设置分支以跟踪远程分支,这通常意味着 origin/branch_name 分支。
Your local branches should now show:您当地的分支机构现在应该显示:
$ git branch
* experimental
master
You can track more than one remote repository using git remote
:您可以使用git remote
跟踪多个远程存储库:
$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* master
remotes/origin/HEAD
remotes/origin/master
remotes/origin/v1.0-stable
remotes/origin/experimental
remotes/win32/master
remotes/win32/new-widgets
At this point, things are getting pretty crazy, so run gitk
to see what's going on:在这一点上,事情变得非常疯狂,所以运行gitk
看看发生了什么:
$ gitk --all &
If you have many remote branches that you want to fetch at once, do:如果您有许多要一次获取的远程分支,请执行以下操作:
git pull --all
Now you can checkout any branch as you need to, without hitting the remote repository.现在您可以根据需要签出任何分支,而无需访问远程存储库。
Note: This will not create working copies of any non-checked out branches, which is what the question was asking.注意:这不会创建任何未签出分支的工作副本,这就是问题所在。 For that, see为此,请参阅
This Bash script helped me out:这个Bash脚本帮助了我:
#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
done
It will create tracking branches for all remote branches, except master (which you probably got from the original clone command).它将为所有远程分支创建跟踪分支,除了 master(您可能从原始克隆命令中获得)。 I think you might still need to do a我想你可能还需要做一个
git fetch --all
git pull --all
to be sure.为了确定。
One liner :
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
一个班轮:git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
As usual: test in your setup before copying rm -rf universe as we know it像往常一样:在复制我们所知道的 rm -rf Universe 之前在您的设置中进行测试Credits for one-liner go to user cfi单线 go 给用户 cfi 的积分
Using the --mirror
option seems to copy the remote
tracking branches properly.使用--mirror
选项似乎可以正确复制remote
跟踪分支。 However, it sets up the repository as a bare repository, so you have to turn it back into a normal repository afterwards.但是,它将存储库设置为裸存储库,因此您必须在之后将其转回普通存储库。
git clone --mirror path/to/original path/to/dest/.git
cd path/to/dest
git config --bool core.bare false
git checkout anybranch
Reference: Git FAQ: How do I clone a repository with all remotely tracked branches?参考: Git 常见问题解答:如何克隆包含所有远程跟踪分支的存储库?
You can easily switch to a branch without using the fancy "git checkout -b somebranch origin/somebranch" syntax.您可以轻松切换到分支,而无需使用花哨的“git checkout -b somebranch origin/somebranch”语法。 You can do:你可以做:
git checkout somebranch
Git will automatically do the right thing: Git 会自动做正确的事:
$ git checkout somebranch
Branch somebranch set up to track remote branch somebranch from origin.
Switched to a new branch 'somebranch'
Git will check whether a branch with the same name exists in exactly one remote, and if it does, it tracks it the same way as if you had explicitly specified that it's a remote branch. Git 将检查同名的分支是否存在于一个远程,如果存在,它会以与您明确指定它是远程分支相同的方式跟踪它。 From the git-checkout man page of Git 1.8.2.1:从 Git 1.8.2.1 的 git-checkout 手册页:
If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to如果 <branch> 未找到,但在一个远程(称为 <remote>)中确实存在一个具有匹配名称的跟踪分支,则视为等效于
$ git checkout -b <branch> --track <remote>/<branch>
Regarding,关于,
git checkout -b experimental origin/experimental git 结帐-b 实验起源/实验
using使用
git checkout -t origin/experimental
or the more verbose, but easier to remember或者更冗长,但更容易记住
git checkout --track origin/experimental
might be better, in terms of tracking a remote repository.在跟踪远程存储库方面可能会更好。
The fetch that you are doing should get all the remote branches, but it won't create local branches for them.您正在执行的 fetch 应该获取所有远程分支,但不会为它们创建本地分支。 If you use gitk , you should see the remote branches described as "remotes/origin/dev" or something similar.如果您使用gitk ,您应该看到描述为“remotes/origin/dev”或类似内容的远程分支。
To create a local branch based on a remote branch, do something like:要基于远程分支创建本地分支,请执行以下操作:
git checkout -b dev refs/remotes/origin/dev
Which should return something like:应该返回如下内容:
Branch dev set up to track remote branch refs/remotes/origin/dev. Switched to a new branch "dev"
Now, when you are on the dev branch, "git pull" will update your local dev to the same point as the remote dev branch.现在,当您在dev分支上时,“git pull”会将您的本地dev更新到与远程dev分支相同的点。 Note that it will fetch all branches, but only pull the one you are on to the top of the tree.请注意,它将获取所有分支,但只会将您所在的分支拉到树的顶部。
Use aliases.使用别名。 Though there aren't any native Git one-liners, you can define your own as虽然没有任何原生 Git 单行,但您可以将自己定义为
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
and then use it as然后将其用作
git clone-branches
Here is the best way to do this:这是执行此操作的最佳方法:
mkdir repo
cd repo
git clone --bare path/to/repo.git .git
git config --unset core.bare
git reset --hard
At this point you have a complete copy of the remote repository with all of its branches (verify with git branch
).此时,您拥有远程存储库及其所有分支的完整副本(使用git branch
进行验证)。 You can use --mirror
instead of --bare
if your remote repository has remotes of its own.如果您的远程存储库有自己的远程,您可以使用--mirror
代替--bare
。
When you do "git clone git://location", all branches and tags are fetched.当您执行“git clone git://location”时,将获取所有分支和标签。
In order to work on top of a specific remote branch, assuming it's the origin remote:为了在特定的远程分支上工作,假设它是源远程:
git checkout -b branch origin/branchname
git clone
downloads all remote branches but still considers them "remote", even though the files are located in your new repository. git clone
下载所有远程分支,但仍将它们视为“远程”,即使文件位于新存储库中。 There's one exception to this, which is that the cloning process creates a local branch called "master" from the remote branch called "master".有一个例外,即克隆过程从名为“master”的远程分支创建一个名为“master”的本地分支。 By default, git branch
only shows local branches, which is why you only see "master".默认情况下, git branch
只显示本地分支,这就是你只看到“master”的原因。
git branch -a
shows all branches, including remote branches . git branch -a
显示所有分支,包括远程分支。
If you actually want to work on a branch, you'll probably want a "local" version of it.如果你真的想在一个分支上工作,你可能需要它的“本地”版本。 To simply create local branches from remote branches (without checking them out and thereby changing the contents of your working directory) , you can do that like this:要简单地从远程分支创建本地分支(不检查它们并因此更改工作目录的内容) ,您可以这样做:
git branch branchone origin/branchone
git branch branchtwo origin/branchtwo
git branch branchthree origin/branchthree
In this example, branchone
is the name of a local branch you're creating based on origin/branchone
;在此示例中, branchone
是您基于origin/branchone
创建的本地分支的名称; if you instead want to create local branches with different names, you can do this:如果你想创建不同名称的本地分支,你可以这样做:
git branch localbranchname origin/branchone
Once you've created a local branch, you can see it with git branch
(remember, you don't need -a
to see local branches).创建本地分支后,您可以使用git branch
查看它(请记住,您不需要-a
来查看本地分支)。
This isn't too complicated.这并不太复杂。 Very simple and straightforward steps are as follows;非常简单直接的步骤如下;
git fetch origin
: This will bring all the remote branches to your local. git fetch origin
:这会将所有远程分支带到您的本地。
git branch -a
: This will show you all the remote branches. git branch -a
:这将显示所有远程分支。
git checkout --track origin/<branch you want to checkout>
Verify whether you are in the desired branch by the following command;通过以下命令验证您是否在所需的分支中;
git branch
The output will like this; output会喜欢这个;
*your current branch
some branch2
some branch3
Notice the * sign that denotes the current branch.注意表示当前分支的 * 符号。
Just do this:只需这样做:
$ git clone git://example.com/myproject
$ cd myproject
$ git checkout branchxyz
Branch branchxyz set up to track remote branch branchxyz from origin.
Switched to a new branch 'branchxyz'
$ git pull
Already up-to-date.
$ git branch
* branchxyz
master
$ git branch -a
* branchxyz
master
remotes/origin/HEAD -> origin/master
remotes/origin/branchxyz
remotes/origin/branch123
You see, 'git clone git://example.com/myprojectt' fetches everything, even the branches, you just have to checkout them, then your local branch will be created.您会看到,'git clone git://example.com/myprojectt' 获取所有内容,甚至是分支,您只需签出它们,然后将创建您的本地分支。
You only need to use "git clone" to get all branches.您只需要使用“git clone”来获取所有分支。
git clone <your_http_url>
Even though you only see the master branch, you can use "git branch -a" to see all branches.即使您只看到主分支,您也可以使用“git branch -a”查看所有分支。
git branch -a
And you can switch to any branch which you already have.你可以切换到你已经拥有的任何分支。
git checkout <your_branch_name>
Don't worry that after you "git clone", you don't need to connect with the remote repository.不用担心“git clone”之后就不需要连接远程仓库了。 "git branch -a" and "git checkout <your_branch_name>" can be run successfully when you don't have an Internet connection. “git branch -a”和“git checkout <your_branch_name>”可以在没有网络连接的情况下成功运行。 So it is proved that when you do "git clone", it already has copied all branches from the remote repository.所以证明当你做“git clone”时,它已经从远程仓库复制了所有的分支。 After that, you don't need the remote repository.之后,您不需要远程存储库。 Your local already has all branches' code.您的本地已经拥有所有分支机构的代码。
A git clone
is supposed to copy the entire repository. git clone
应该复制整个存储库。 Try cloning it, and then run git branch -a
.尝试克隆它,然后运行git branch -a
。 It should list all the branches.它应该列出所有分支。 If then you want to switch to branch "foo" instead of "master", use git checkout foo
.如果您想切换到分支“foo”而不是“master”,请使用git checkout foo
。
All the answers I saw here were valid, but there is a much cleaner way to clone a repository and to pull all the branches at once.我在这里看到的所有答案都是有效的,但是有一种更简洁的方法来克隆存储库并一次提取所有分支。
When you clone a repository, all the information of the branches is actually downloaded, but the branches are hidden.当你克隆一个存储库时,实际上是下载了所有分支的信息,但分支是隐藏的。 With the command用命令
git branch -a
you can show all the branches of the repository, and with the command您可以显示存储库的所有分支,并使用命令
git checkout -b branchname origin/branchname
you can then "download" them manually one at a time.然后,您可以一次手动“下载”它们。
However, when you want to clone a repository with a lot of branches, all the ways illustrated in previous answers are lengthy and tedious in respect to a much cleaner and quicker way that I am going to show, though it's a bit complicated.但是,当您想要克隆具有大量分支的存储库时,相对于我将展示的一种更清洁、更快捷的方式而言,前面的答案中说明的所有方式都是冗长而乏味的,尽管它有点复杂。 You need three steps to accomplish this:您需要三个步骤来完成此操作:
Create a new empty folder on your machine and clone a mirror copy of the .git folder from the repository:在您的机器上创建一个新的空文件夹并从存储库中克隆.git文件夹的镜像副本:
cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder
git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git
The local repository inside the folder my_repo_folder is still empty, and there is just a hidden .git folder now that you can see with a "ls -alt" command from the terminal.文件夹 my_repo_folder 中的本地存储库仍然是空的,只有一个隐藏的.git文件夹,现在您可以从终端使用“ls -alt”命令查看。
Switch this repository from an empty (bare) repository to a regular repository by switching the boolean value "bare" of the Git configurations to false:通过将 Git 配置的 boolean 值“bare”切换为 false,将此存储库从空(裸)存储库切换到常规存储库:
git config --bool core.bare false
Grab everything that inside the current folder and create all the branches on the local machine, therefore making this a normal repository.抓取当前文件夹中的所有内容并在本地计算机上创建所有分支,从而使其成为一个正常的存储库。
git reset --hard
So now you can just type the command "git branch" and you can see that all the branches are downloaded.所以现在你可以输入命令“git branch”,你可以看到所有的分支都被下载了。
This is the quick way in which you can clone a Git repository with all the branches at once, but it's not something you want to do for every single project in this way.这是您可以一次克隆包含所有分支的 Git 存储库的快速方法,但您不想以这种方式对每个项目都执行此操作。
If you're looking for a self-contained clone or backup that includes all remote branches and commit logs, use:如果您正在寻找包含所有远程分支和提交日志的独立克隆或备份,请使用:
git clone http://user@repo.url
git pull --all
The accepted answer of git branch -a
only shows the remote branches. git branch -a
接受的答案仅显示远程分支。 If you attempt to checkout
the branches you'll be unable to unless you still have network access to the origin server.如果您尝试checkout
出分支机构,除非您仍然可以通过网络访问原始服务器,否则您将无法签出。
Credit: Gabe Kopley's for suggesting using git pull --all
.信用: Gabe Kopley建议使用git pull --all
。
Note:笔记:
Of course, if you no longer have network access to the remote/origin
server, remote/origin branches
will not have any updates reflected in your repository clone.当然,如果您不再有remote/origin
服务器的网络访问权限, remote/origin branches
将不会有任何更新反映在您的存储库克隆中。 Their revisions will reflect commits from the date and time you performed the two repository cloning commands above.他们的修订将反映您执行上述两个存储库克隆命令的日期和时间的提交。
To checkout ALL your clone branches to local branches with one command, use one of the bash commands below:要使用一个命令将所有克隆分支检出到本地分支,请使用以下 bash 命令之一:
$ for i in $(git branch -a |grep 'remotes' | awk -F/ '{print $3}' \ | grep -v 'HEAD ->');do git checkout -b $i --track origin/$i; done
OR或者
If your repo has nested branches then this command will take that into account also:如果你的仓库有嵌套分支,那么这个命令也会考虑到这一点:
for i in $(git branch -a |grep 'remotes' |grep -v 'HEAD ->');do \ basename ${i##\./} | xargs -I {} git checkout -b {} --track origin/{}; done
The above commands will checkout
a local branch into your local git repository, named the same as the remote/origin/<branchname>
and set it to --track
changes from the remote branch on the remote/origin
server should you regain network access to your origin repo server once more and perform a git pull
command in the usual way.上述命令会将本地分支checkout
到本地 git 存储库中,名称与remote/origin/<branchname>
相同,并将其设置为--track
如果您重新获得网络访问权限,则从remote/origin
服务器上的远程分支更改您的原始存储库服务器再次以通常的方式执行git pull
命令。
Use my tool git_remote_branch ( grb ).使用我的工具git_remote_branch ( grb )。 You need Ruby installed on your machine).您需要在您的机器上安装Ruby )。 It's built specifically to make remote branch manipulations dead easy.它是专门为使远程分支操作变得非常容易而构建的。
Each time it does an operation on your behalf, it prints it in red at the console.每次它代表您执行操作时,都会在控制台上以红色打印。 Over time, they finally stick into your brain:-)随着时间的推移,它们最终会进入你的大脑:-)
If you don't want grb to run commands on your behalf, just use the 'explain' feature.如果您不希望 grb 代表您运行命令,只需使用“解释”功能。 The commands will be printed to your console instead of executed for you.这些命令将打印到您的控制台而不是为您执行。
Finally, all commands have aliases, to make memorization easier.最后,所有命令都有别名,以便于记忆。
Note that this is alpha software ;-)请注意,这是alpha 软件;-)
Here's the help when you run grb help:这是运行 grb help 时的帮助:
git_remote_branch version 0.2.6 Usage: grb create branch_name [origin_server] grb publish branch_name [origin_server] grb rename branch_name [origin_server] grb delete branch_name [origin_server] grb track branch_name [origin_server] Notes: - If origin_server is not specified, the name 'origin' is assumed (git's default) - The rename functionality renames the current branch The explain meta-command: you can also prepend any command with the keyword 'explain'. Instead of executing the command, git_remote_branch will simply output the list of commands you need to run to accomplish that goal. Example: grb explain create grb explain create my_branch github All commands also have aliases: create: create, new delete: delete, destroy, kill, remove, rm publish: publish, remotize rename: rename, rn, mv, move track: track, follow, grab, fetch
Cloning from a local repo will not work with git clone & git fetch: a lot of branches/tags will remain unfetched. git clone & git fetch 无法从本地 repo 克隆:许多分支/标签将保持未获取状态。
To get a clone with all branches and tags.获取包含所有分支和标签的克隆。
git clone --mirror git://example.com/myproject myproject-local-bare-repo.git
To get a clone with all branches and tags but also with a working copy:要获得包含所有分支和标签的克隆,但也包含工作副本:
git clone --mirror git://example.com/myproject myproject/.git
cd myproject
git config --unset core.bare
git config receive.denyCurrentBranch updateInstead
git checkout master
#!/bin/bash
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
git branch --track ${branch#remotes/origin/} $branch
done
These code will pull all remote branches code to the local repository.这些代码会将所有远程分支代码拉到本地存储库。
Looking at one of the answers to the question I noticed that it's possible to shorten it:查看问题的答案之一,我注意到可以缩短它:
for branch in `git branch -r | grep -v 'HEAD\|master'`; do
git branch --track ${branch##*/} $branch;
done
But beware, if one of remote branches is named, eg, admin_master it won't get downloaded!但请注意,如果远程分支之一被命名,例如admin_master ,它将不会被下载!
OK, when you clone your repo, you have all branches there...好的,当你克隆你的仓库时,你有所有的分支......
If you just do git branch
, they are kind of hidden...如果你只是做git branch
,它们有点隐藏......
So if you'd like to see all branches name, just simply add --all
flag like this:因此,如果您想查看所有分支名称,只需添加--all
标志,如下所示:
git branch --all
or git branch -a
git branch --all
或git branch -a
If you just checkout to the branch, you get all you need.如果您只是结帐到分行,您将获得所需的一切。
But how about if the branch created by someone else after you clone?但是如果你克隆后别人创建的分支呢?
In this case, just do:在这种情况下,只需执行以下操作:
git fetch
and check all branches again...并再次检查所有分支......
If you like to fetch and checkout at the same time, you can do:如果您想同时提取和结帐,您可以执行以下操作:
git fetch && git checkout your_branch_name
Also created the image below for you to simplify what I said:还为您创建了下面的图像以简化我所说的:
None of these answers cut it, except user nobody is on the right track .这些答案都没有削减它,除了用户没有人在正确的轨道上。
I was having trouble with moving a repository from one server/system to another.我在将存储库从一台服务器/系统移动到另一台服务器/系统时遇到了麻烦。 When I cloned the repository, it only created a local branch for master, so when I pushed to the new remote, only the master branch was pushed.当我克隆存储库时,它只为master创建了一个本地分支,所以当我推送到新的远程时,只推送了master分支。
So I found these two methods very useful.所以我发现这两种方法非常有用。
Method 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
Method 2:方法二:
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
git clone --mirror
on the original repo works well for this. git clone --mirror
on the original repo 适用于此。
git clone --mirror /path/to/original.git
git remote set-url origin /path/to/new-repo.git
git push -u origin
For copy-pasting into the command line:对于复制粘贴到命令行:
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
For higher readability:为了提高可读性:
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
This will:这将:
It is based on the answer of VonC .它基于VonC的答案。
I wrote these small PowerShell functions to be able to checkout all my Git branches, that are on origin remote.我编写了这些小的 PowerShell函数,以便能够检查我的所有 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>' #>
}
More Git functions can be found in my Git settings repository .更多 Git 功能可以在我的 Git 设置库中找到。
Here's an answer that uses awk.这是一个使用 awk 的答案。 This method should suffice if used on a new repo.如果在新的 repo 上使用此方法就足够了。
git branch -r | awk -F/ '{ system("git checkout " $NF) }'
Existing branches will simply be checked out, or declared as already in it, but filters can be added to avoid the conflicts.现有的分支将被简单地检出,或声明为已经存在,但可以添加过滤器以避免冲突。
It can also be modified so it calls an explicit git checkout -b <branch> -t <remote>/<branch>
command.也可以对其进行修改,以便调用显式git checkout -b <branch> -t <remote>/<branch>
命令。
This answer follows Nikos C.这个答案遵循Nikos C。 's idea .的想法。
Alternatively we can specify the remote branch instead.或者,我们可以指定远程分支。 This is based on murphytalk 's answer .这是基于murphytalk的回答。
git branch -r | awk '{ system("git checkout -t " $NF) }'
It throws fatal error messages on conflicts but I see them harmless.它会在冲突中引发致命的错误消息,但我认为它们是无害的。
Both commands can be aliased.这两个命令都可以有别名。
Using nobody 's answer as reference, we can have the following commands to create the aliases:使用nobody的答案作为参考,我们可以使用以下命令来创建别名:
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) }"'
Personally I'd use track-all
or track-all-branches
.我个人会使用track-all
或track-all-branches
。
I'm using Bitbucket, a repository hosting service of Atlassian.我正在使用 Bitbucket,这是 Atlassian 的存储库托管服务。 So I try to follow their documentation.所以我尝试遵循他们的文档。 And that works perfectly for me.这对我来说非常有效。 With the following easy and short commands you can checkout your remote branch.使用以下简单而简短的命令,您可以签出您的远程分支。
At first clone your repository, and then change into the destination folder.首先克隆您的存储库,然后更改为目标文件夹。 And last, but not least, fetch and checkout:最后但并非最不重要的一点是获取和结帐:
git clone <repo> <destination_folder>
cd <destination_folder>
git fetch && git checkout <branch>
That's it.而已。 Here a little more real-world example:这里有一个更真实的例子:
git clone https://username@bitbucket.org/team/repository.git project_folder
cd project_folder
git fetch && git checkout develop
You will find detail information about the commands in the documentation: Clone Command , Fetch Command , Checkout Command您将在文档中找到有关命令的详细信息: Clone Command 、 Fetch Command 、 Checkout Command
I'm cloning a repository from the Udemy course Elegant Automation Frameworks with Python and Pytest , so that I can later go over it OFFLINE .我正在使用 Python 和 Pytest 从 Udemy 课程优雅自动化框架中克隆一个存储库,以便我以后可以离线使用 go 。 I tried downloading the zip, but this only comes for the current branch, so here are my 2 cents.我尝试下载 zip,但这仅适用于当前分支,所以这是我的 2 美分。
I'm working on Windows and, obviously, I resorted to the Ubuntu shell from the Windows Subsystem for Linux . I'm working on Windows and, obviously, I resorted to the Ubuntu shell from the Windows Subsystem for Linux . Immediately after cloning, here's my 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
Then — and after hitting a few git checkout
brick walls —, what finally worked for me was:然后——在打了几git checkout
砖墙之后——最终对我有用的是:
$ for b in `git branch -a | cut -c18- | cut -d\ -f1`; do git checkout $b; git stash; done
After this, here are my branches:在此之后,这是我的分支:
$ 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
Mine goes physical, cutting out the initial remotes/origin/
and then filtering for space delimiters.我的是物理的,删除初始的remotes/origin/
,然后过滤空格分隔符。 Arguably, I could just have grep
ed out HEAD
and be done with one cut
, but I'll leave that for the comments.可以说,我可以让grep
淘汰HEAD
并完成一次cut
,但我会留下评论。
Please notice that your current branch is now the last on the list.请注意,您当前的分支现在是列表中的最后一个。 If you don't know why that is, you're in a tight spot there.如果你不知道为什么会这样,那么你就处于困境中。 Just git checkout
whatever you want now.现在只需git checkout
您想要的任何东西。
Git usually (when not specified) fetches all branches and/or tags (refs, see: git ls-refs
) from one or more other repositories along with the objects necessary to complete their histories. Git 通常(未指定时)从一个或多个其他存储库以及完成其历史所需的对象获取所有分支和/或标签(参考,参见: git ls-refs
)。 In other words, it fetches the objects which are reachable by the objects that are already downloaded.换句话说,它获取已经下载的对象可以访问的对象。 See: What does git fetch
really do?请参阅: git fetch
到底有什么作用?
Sometimes you may have branches/tags which aren't directly connected to the current one, so git pull --all
/ git fetch --all
won't help in that case, but you can list them by:有时您可能有未直接连接到当前分支/标签的分支/标签,因此git pull --all
/ git fetch --all
在这种情况下无济于事,但您可以通过以下方式列出它们:
git ls-remote -h -t origin
And fetch them manually by knowing the ref names.并通过知道参考名称手动获取它们。
So to fetch them all , try:因此,要全部获取它们,请尝试:
git fetch origin --depth=10000 $(git ls-remote -h -t origin)
The --depth=10000
parameter may help if you've shallowed repository.如果您已经浅化了存储库, --depth=10000
参数可能会有所帮助。
Then check all your branches again:然后再次检查所有分支:
git branch -avv
If the above won't help, you need to add missing branches manually to the tracked list (as they got lost somehow):如果上述方法没有帮助,您需要手动将丢失的分支添加到跟踪列表中(因为它们以某种方式丢失了):
$ git remote -v show origin
...
Remote branches:
master tracked
by git remote set-branches
like:通过git remote set-branches
,如:
git remote set-branches --add origin missing_branch
so it may appear under remotes/origin
after fetch:所以它可能会在获取后出现在remotes/origin
下:
$ git remote -v show origin
...
Remote branches:
missing_branch new (next fetch will store in remotes/origin)
$ git fetch
From github.com:Foo/Bar
* [new branch] missing_branch -> origin/missing_branch
If you still cannot get anything other than the master branch, check the following:如果您仍然无法获得除 master 分支以外的任何内容,请检查以下内容:
git remote -v
), eg仔细检查您的遥控器( git remote -v
),例如
I needed to do exactly the same.我需要做同样的事情。 Here is my Ruby script.这是我的Ruby脚本。
#!/usr/bin/env ruby
local = []
remote = {}
# Prepare
%x[git reset --hard HEAD]
%x[git checkout master] # Makes sure that * is on master.
%x[git branch -a].each_line do |line|
line.strip!
if /origin\//.match(line)
remote[line.gsub(/origin\//, '')] = line
else
local << line
end
end
# Update
remote.each_pair do |loc, rem|
next if local.include?(loc)
%x[git checkout --track -b #{loc} #{rem}]
end
%x[git fetch]
Here is another short one-liner command which creates local branches for all remote branches:这是另一个为所有远程分支创建本地分支的简短单行命令:
(git branch -r | sed -n '/->/!s#^ origin/##p' && echo master) | xargs -L1 git checkout
It works also properly if tracking local branches are already created.如果已经创建了跟踪本地分支,它也可以正常工作。 You can call it after the first git clone
or any time later.您可以在第一个git clone
之后或之后的任何时间调用它。
If you do not need to have master
branch checked out after cloning, use如果克隆后不需要签出master
分支,请使用
git branch -r | sed -n '/->/!s#^ origin/##p'| xargs -L1 git checkout
As of early 2017, the answer in this comment works:截至 2017 年初, 此评论中的答案有效:
git fetch <origin-name> <branch-name>
brings the branch down for you. git fetch <origin-name> <branch-name>
为您带来分支。 While this doesn't pull all branches at once, you can singularly execute this per-branch.虽然这不会一次拉出所有分支,但您可以单独执行每个分支。
This variation will clone a remote repo with all branches available locally without having to checkout each branch one by one.此变体将克隆一个远程存储库,其中包含本地可用的所有分支,而无需逐个签出每个分支。 No fancy scripts needed.不需要花哨的脚本。
Make a folder with the same name of the repo you wish to clone and cd into for example:创建一个与您希望克隆的 repo 同名的文件夹,然后 cd 进入,例如:
mkdir somerepo
cd somerepo
Now do these commands but with actual repo usersname/reponame现在执行这些命令,但使用实际的 repo 用户名/reponame
git clone --bare git@github.com:someuser/somerepo.git .git
git config --bool core.bare false
git reset --hard
git branch
Voiala!瞧! you have all the branches there!你那里有所有的分支机构!
To create a "full" backup of all branches+refs+tags+etc stored in your git host (github/bitbucket/etc), run:要创建存储在 git 主机(github/bitbucket/etc)中的所有分支+refs+tags+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
This is compiled from everything I've learned from other answers.这是根据我从其他答案中学到的所有内容编译而成的。
You can then use this local repo mirror to transition to a different SCM system/git host, or you can keep this as a backup.然后,您可以使用此本地 repo 镜像转换到不同的 SCM 系统/git 主机,或者您可以将其保留为备份。 It's also useful as a search tool, since most git hosts only search code on the "main" branch of each repo, if you git log -S"specialVar"
, you'll see all code on all branches.它作为搜索工具也很有用,因为大多数 git 主机只在每个 repo 的“主”分支上搜索代码,如果你git log -S"specialVar"
,你会看到所有分支上的所有代码。
Note: if you want to use this repo in your day-to-day work, run:注意:如果您想在日常工作中使用此 repo,请运行:
git config --unset remote.origin.mirror
WARNING: you may run into strange issues if you attempt to use this in your day-to-day work.警告:如果您尝试在日常工作中使用它,您可能会遇到奇怪的问题。 If your ide/editor is doing some auto-fetching, your local master
may update because, you did git clone --mirror
.如果您的 ide/editor 正在执行一些自动获取,您的本地master
可能会更新,因为您执行了git clone --mirror
。 Then those files appear in your git staging area.然后这些文件出现在您的 git 暂存区域中。 I actually had a situation where I'm on a local feature branch.. that branch has no commits, and all files in the repo appear in the staging area.实际上,我遇到了一种情况,即我在本地功能分支上。该分支没有提交,并且存储库中的所有文件都出现在暂存区域中。 Just nuts.简直是疯了。
I think this does the trick:我认为这可以解决问题:
mkdir YourRepo
cd YourRepo
git init --bare .git # create a bare repo
git remote add origin REMOTE_URL # add a remote
git fetch origin refs/heads/*:refs/heads/* # fetch heads
git fetch origin refs/tags/*:refs/tags/* # fetch tags
git init # reinit work tree
git checkout master # checkout a branch
So far, this works for me.到目前为止,这对我有用。
I was trying to find out how to pull down a remote branch I had deleted locally.我试图找出如何拉下我在本地删除的远程分支。 Origin was not mine, and I didn't want to go through the hassle of re-cloning everything.起源不是我的,我不想通过重新克隆所有东西的麻烦来 go。
This worked for me:这对我有用:
assuming you need to recreate the branch locally:假设您需要在本地重新创建分支:
git checkout -b recreated-branch-name
git branch -a (to list remote branches)
git rebase remotes/remote-origin/recreated-branch-name
So if I forked from gituser/master to sjp and then branched it to sjp/mynewbranch , it would look like this:因此,如果我从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
This solution worked for me to "copy" a repository to another one:该解决方案对我有用,可以将存储库“复制”到另一个存储库:
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
On target repository I can see the same branches and tags than the origin repo.在目标存储库上,我可以看到与原始存储库相同的分支和标签。
gitCloneAllBranches() { # clone all git branches at once easily and cd in
# clone as "bare repo"
git clone --mirror $1
# rename without .git extension
with_extension=$(basename $1)
without_extension=$(echo $with_extension | sed 's/.git//')
mv $with_extension $without_extension
cd $without_extension
# change from "bare repository" to not
git config --bool core.bare false
# check if still bare repository if so
if [[ $(git rev-parse --is-bare-repository) == false ]]; then
echo "ready to go"
else
echo "WARNING: STILL BARE GIT REPOSITORY"
fi
# EXAMPLES:
# gitCloneAllBranches https://github.com/something/something.git
}
This is what I do whenever I need to bring down all branches.每当我需要关闭所有分支时,我都会这样做。 Credits to Ray Villalobos from Linkedin Learning.来自 Linkedin Learning 的Ray Villalobos学分。 Try cloning all branches including commits:尝试克隆所有分支,包括提交:
mkdir -p -- newproject_folder
cd newproject_folder
git clone --mirror https://github.com/USER_NAME/RepositoryName.git .git
git config --bool core.bare false
git reset --hard
A better alternative solution for developers using Visual Studio Code is to use Git Shadow Extension .对于使用Visual Studio Code的开发人员来说,一个更好的替代解决方案是使用Git Shadow Extension 。
This Visual Studio Code extension allows cloning repository content and directories, that can be filtered by branch name or commit hash.此 Visual Studio Code 扩展允许克隆存储库内容和目录,可以按分支名称或提交 hash 过滤。 That way, branches or commits can be used as boilerplates/templates for new projects.这样,分支或提交可以用作新项目的样板/模板。
Here's a cross-platform PowerShell 7 function adapted from the previous answers.这是改编自先前答案的跨平台PowerShell 7 function 。
function Invoke-GitCloneAll($url) {
$repo = $url.Split('/')[-1].Replace('.git', '')
$repo_d = Join-Path $pwd $repo
if (Test-Path $repo_d) {
Write-Error "fatal: destination path '$repo_d' already exists and is not an empty directory." -ErrorAction Continue
} else {
Write-Host "`nCloning all branches of $repo..."
git -c fetch.prune=false clone $url -q --progress &&
git -c fetch.prune=false --git-dir="$(Join-Path $repo_d '.git')" --work-tree="$repo_d" pull --all
Write-Host "" #newline
}
}
Note: -c fetch.prune=false
makes it include stale branches that would normally be excluded.注意: -c fetch.prune=false
使其包含通常会被排除的陈旧分支。 Remove that if you're not interested in it.如果您对它不感兴趣,请删除它。
You can make this work with PowerShell 5.1 (the default in Windows 10) by removing &&
from the function, but that makes it try to git pull
even when the previous command failed. You can make this work with PowerShell 5.1 (the default in Windows 10) by removing &&
from the function, but that makes it try to git pull
even when the previous command failed. So, I strongly recommend just using the cross-platform PowerShell it's always bugging you about trying.因此,我强烈建议您只使用跨平台 PowerShell,它总是让您烦恼尝试。
Do a bare clone of the remote repository, save the contents to a.git directory对远程存储库进行裸克隆,将内容保存到 a.git 目录
git clone --bare remote-repo-url.git localdirname/.git
(A bare git repository, created using git clone --bare or git init --bare, is a storage repository, it does not have a working directory, you cannot create or modify files there.) (一个裸 git 存储库,使用 git clone --bare 或 git init --bare 创建,是一个存储库,它没有工作目录,不能创建或修改文件。)
Change directory to your local directory将目录更改为本地目录
cd localdirname
Make your git repository modifiable使您的 git 存储库可修改
git config --bool core.bare false
Restore your working directory恢复你的工作目录
git reset --hard
List all your branches列出所有分支
git branch -al
How to create a local branch for each branch on remote origin
matching pattern
.如何为远程origin
匹配pattern
上的每个分支创建本地分支。
#!/bin/sh
git fetch --all
git for-each-ref --format='%(refname:short)' refs/remotes/origin/pattern |\
sed 's@\(origin/\)\(.*\)@\2\t\1\2@' |\
xargs -n 2 git branch --track
All remote references (branches/tags) are fetched and then local references are created.获取所有远程引用(分支/标签),然后创建本地引用。 Should work on most systems, fast, without checking out the index or relying on bashisms.应该可以在大多数系统上快速运行,无需检查索引或依赖 bashism。
I could not edit Bigfish answer's .我无法编辑Bigfish 的回答。 He proposes a bash script which I offer to update and give a better git integration.他提出了一个 bash 脚本,我提供它来更新并提供更好的 git 集成。 egrep is outdated and should be replaced by grep -E. egrep 已过时,应替换为 grep -E。
#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | grep -E --invert-match '(:?HEAD|master)$'); do
git branch --track "${branch##*/}" "$branch"
done
You can extend git by adding this bash file as a git custom subcommand :您可以通过将此 bash 文件添加为git 自定义子命令来扩展 git:
$ mkdir ~/.gitbin; touch ~/.gitbin/git-fetchThemAll
$ chmod u+x ~/.gitbin/git-fetchThemAll
Put the content of the bash script in git-fetchThemAll
.将 bash 脚本的内容放入git-fetchThemAll
中。
$ echo 'export PATH="$HOME/.gitbin:$PATH"' >> ~/.bashrc
$ source ~/.bashrc # update PATH in your current shell
$ git fetchThemAll
If you prefer, you could use a shell alias for this oneliner by user cfi如果你愿意,你可以使用 shell 用户 cfi 为这个 oneliner 别名
alias fetchThemAll=git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
Script to download all braches from a Git project从 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
Ready!准备好! Now just call the command (allBranches) and tell the Git project directory that you want to download all branches现在只需调用命令 (allBranches) 并告诉 Git 项目目录您要下载所有分支
Use利用
Example 1:示例 1:
~$ allBranches /var/www/myproject1/
Example 2:示例 2:
~$ allBranches /var/www/myproject2/
Example 3 (if already inside the project directory):示例 3(如果已经在项目目录中):
~$ allBranches ./
or或者
~$ allBranches .
View result:查看结果:
git branch
Repository allBranches GitHub: https://github.com/marceloviana/allBranches存储库 allBranches GitHub: https://github.com/marceloviana/allBranches
If you use Bitbucket, you can use import Repository .如果你使用 Bitbucket,你可以使用import Repository 。 This will import all Git history (all the branches and commits).这将导入所有 Git 历史记录(所有分支和提交)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.