我克隆了一个 Git 存储库,其中包含大约五个分支。但是,当我执行 git branch
时,我只看到其中一个:
$ git branch
* master
我知道我可以执行 git branch -a
来查看 all 分支,但是我将如何在本地拉出所有分支,所以当我执行 git branch
时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
我克隆了一个 Git 存储库,其中包含大约五个分支。但是,当我执行 git branch
时,我只看到其中一个:
$ git branch
* master
我知道我可以执行 git branch -a
来查看 all 分支,但是我将如何在本地拉出所有分支,所以当我执行 git branch
时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
git branch -r | grep -v '->' | sed "s,x1B[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
(似乎拉拉从所有遥控器中获取所有分支,但我总是先获取。)
仅当服务器上没有由本地分支跟踪的服务器上的远程分支时,才能运行第一个命令。
您可以从这样的所有遥控器中获取所有分支:
git fetch --all
基本上是 power move 。
fetch
更新远程分支的本地副本,因此对于您的本地分支机构始终是安全的,但是:
fetch
不会更新本地分支(跟踪远程分支);如果要更新本地分支,您仍然需要拉每个分支。
fetch
不会*创建本地分支(跟踪远程分支),您必须手动执行此操作。如果要列出所有远程分支: git branch -a
到更新跟踪远程分支的本地分支:
git pull --all
但是,这仍然不足。它仅适用于您的本地分支,这些分支跟踪远程分支。要跟踪所有远程分支,在 git pull --all
之前执行此oneliner :
git branch -r | grep -v '->' | sed "s,x1B[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
P.S. afaik git fetch --all
, git remote update
是等效的。
Kamil Szot的 comment ,人们发现它很有用。
我必须使用:
for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done
因为您的代码创建了名为
origin/branchname
的本地分支,并且每当我提到它时,我都会获得“ refname'rount/branchname'。
You will need to create local branches tracking remote branches.
Assuming that you've got only one remote called origin
, this snippet will create local branches for all remote tracking ones:
for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
After that, git fetch --all
will update all local copies of remote branches.
Also, git pull --all
will update your local tracking branches, but depending on your local commits and how the 'merge' configure option is set it might create a merge commit, fast-forward or fail.
如果你这样做:
git fetch origin
然后他们将都在当地。如果您随后执行:
git branch -a
你会看到它们被列为远程/来源/分支名称。由于他们在当地,您可以随心所欲地对待他们。例如:
git diff origin/branch-name
或者
git merge origin/branch-name
或者
git checkout -b some-branch origin/branch-name
$ git remote update
$ git pull --all
This assumes all branches are tracked.
If they aren't you can fire this in Bash:
for remote in `git branch -r `; do git branch --track $remote; done
Then run the command.
Caution: pleas read the warning comments below.
The Bash for
loop wasn't working for me, but this did exactly what I wanted. All the branches from my origin mirrored as the same name locally.
git checkout --detach
git fetch origin '+refs/heads/*:refs/heads/*'
See Mike DuPont's comment below. I think I was trying to do this on a Jenkins Server which leaves it in detached head mode.
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, there is a much cleaner and quicker way, though it's a bit complicated. You need three steps to accomplish this:
First step
create a new empty folder on your machine and clone a mirror copy of the .git folder from the repository:
$ 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, there is just a hidden .git folder now that you can see with a "ls -alt" command from the terminal.
Second step
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 config --bool core.bare false
Third Step
Grab everything that inside the current folder and create all the branches on the local machine, therefore making this a normal repo.
$ git reset --hard
So now you can just type the command git branch
and you can see that all the branches are downloaded.
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 wanna do for every single project in this way.
You can fetch all the branches by:
git fetch --all
or:
git fetch origin --depth=10000 $(git ls-remote -h -t origin)
The --depth=10000
parameter may help if you've shallowed repository.
To pull all the branches, use:
git pull --all
If above won't work, then precede the above command with:
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
as the remote.origin.fetch
could support only a specific branch while fetching, especially when you cloned your repo with --single-branch
. Check this by: git config remote.origin.fetch
.
After that you should be able to checkout any branch.
See also:
To push all the branches to the remote, use:
git push --all
eventually --mirror
to mirror all refs.
If your goal is to duplicate a repository, see: Duplicating a repository article at GitHub.
I usually use nothing else but commands like this:
git fetch origin
git checkout --track origin/remote-branch
A little shorter version:
git fetch origin
git checkout -t origin/remote-branch
To list remote branches:
To checkout a remote branch as a local branch: