标签云

微信群

扫码加入我们

WeChat QR Code


The accepted answer here (git branch -a) shows you the branches in the remote, but if you attempt to check any of those out you will be in a 'detached HEAD' state. The next answer down (second most upvotes) answers a different question (to wit: how to pull all branches, and, again, this only works for those you're tracking locally). Several of the comments point out that you could parse the git branch -a results with a shell script that would locally track all the remote branches. Summary: There's no git native way to do what you want and it might not be all that great an idea anyway.

2018年11月17日26分11秒

Maybe just copy the entire folder the old fashioned way? scp some_userexample.com:/home/some_user/project_folder ~ Not sure if that solution works for github though..

2018年11月17日26分11秒

Rather than saying "I've cloned, pulled, and fetched," much better to show us the exact commands that you executed.

2018年11月17日26分11秒

It always boggles me why "clone" isn't in the sense of an exact copy. If it's an exact clone, shouldn't all the branches be part of the local repository? I mean isn't that one of the point of being distributed? So when something repository is gone you still have a complete copy of everything. Or is it the so called "remote" really are part of the local repository already?

2018年11月18日26分11秒

Seeing all the upvotes, answers, comments on answers and the mind-boggling number of views, I think it is time git added a command for doing this. And right you are huggie, my thoughts exactly.

2018年11月17日26分11秒

How can someone create automatically all the remote branches, e.g. experimental for origin/experimental?

2018年11月17日26分11秒

Cristian: I used to always create a branch 'foo' for every branch 'origin/foo', but this led to two problems: (1) I wound up with lots of really stale tracking branches that were many commits behind the corresponding remote branch, and (2) in older versions of git, running 'git push' would attempt to push all my local branches to a remote, even when those branches were stale. So now I only keep local branches for things that I'm actively developing, and access the origin/* branches directly if I need information about them. (That said, you could use a shell script to parse 'git branch -a'.)

2018年11月18日26分11秒

"git fetch <origin-name> <branch-name>" brings the branch down locally for you.

2018年11月18日26分11秒

Good answer, but kinda misses the question. I was looking for a one-liner to checkout all the remote branches.

2018年11月17日26分11秒

The question was about cloning all remote branches, not checking them out. And, as I noted above, you really don't want to make any more local tracking branches than necessary, because when they get really stale, they can cause headaches.

2018年11月17日26分11秒

If I do git clone, I have the master branch locally and 10 branches "remote". So THIS answer by Gabe was very helpful and answers the question.

2018年11月17日26分11秒

this only fetch remote branches that have been locally added not any remote branch

2018年11月18日26分11秒

The first command is redundant. Simply git pull --all will do the same – it just won't fetch twice. And infosec812 is right that this does not answer the question anyway. I wonder how this got so many upvotes.

2018年11月17日26分11秒

After I did git remote update, then tried git branch, I only see local branches. But if I do git branch -a I can now see the remote branches and I can do a git pull <branchname> to get the branch I want. -- I landed on this question from a Google search, and this answer solves my problem.

2018年11月17日26分11秒

This is not helpful at all, doesn't pull any remote branches other than that is existing.

2018年11月18日26分11秒

This is really close to being a perfect solution.. The only thing that would make it better is if this functionality were built-in as an option in git.

2018年11月18日26分11秒

"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 As usual: test in your setup before copying rm -rf universe as we know it

2018年11月18日26分11秒

This command creates the feature branches from remote as normal branches (not feature branches) - how to fix this?

2018年11月18日26分11秒

if you run into issues with "/" in branch names there is a solution below using a git alias. see answer by "nobody" on "answered May 15 '13 at 11:02"

2018年11月18日26分11秒

I'm trimming just remotes/origin/ to preserve namespaces: for BRANCH in $(git branch -a | grep remotes | grep -v HEAD | grep -v master); do git branch --track "${BRANCH#remotes/origin/}" "${BRANCH}"; done

1970年01月01日00分03秒

You know this actually seems to be a pretty good answer even though it has no votes. Are there any pitfalls to doing it that way? I had to explicitly checkout a branch after running those commands.

2018年11月17日26分11秒

This combined with git push --mirror are exactly what I needed to create an exact duplicate of a remote git repo when moving from github.com to a github enterprise install. Thanks!

2018年11月18日26分11秒

Dave: Add a final git checkout as last command to finally checkout the head of the current branch on the cloned repo. This is a great answer, by far the best. Be brave, eventually we'll get you to the top :-)

2018年11月17日26分11秒

Dave: Hm. I'm having second thoughts: --mirror does more than just setting up all branches as being tracked. It copies all refs from the origin and subsequent git remote update will do that again. Behaviour of pulls change. I'm back to believing the full copy requires a one-line script.

2018年11月17日26分11秒

git clone --mirror is very good for backing up your git repositories ^_^

2018年11月17日26分11秒

So, if the name of the branch you checkout is the identical to the name of the remote branch, everything after the "/", then git will create a branch of the same name, everything after the "/", "tracking" that remote? And by tracking, we mean: git push, git pull, etc. will be done on that remote? If this is correct, then expand on your answer with more information, because I aggree with Daniel, this answer deserves more rep.

2018年11月17日26分11秒

BullfrogBlues, the answer to all your questions appears to be yes (I'm using git v1.7.7.4). I agree this behavior should be better known. (It's not in the manual for this version of git.) I actually don't like this behavior, I'd rather get an error and have to say git checkout --track origin/somebranch explicitly.

2018年11月17日26分11秒

dubiousjim: Actually, this is in the manual. git-checkout(1) says: "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 'git checkout -b <branch> --track <remote>/<branch>' " (Git V.1.8.1.1).

2018年11月17日26分11秒

What we need is $ git pull * <remote>/* - where "*" is a wildcard, so it pulls all branches, including those not yet on the local system. How to do this? Are we really supposed to checkout/pull every branch just to get the code pulled to our local system?

2018年11月18日26分11秒

You don't need refs/remotes here. git checkout -b dev origin/dev will work fine.

2018年11月17日26分11秒

This will always work: git checkout -b newlocaldev --track origin/dev. If you want the local branch has the same name as the remote one, and the remote one doesn't have a tricky name, you can omit the -b newlocaldev. With the default branch.autosetupmerge config setting, and assuming you don't have a local branch named dev, these two commands may do the same thing: git checkout -b dev origin/dev and just plain git checkout dev. Finally, git checkout origin/dev doesn't create a new branch, but just puts you in detached HEAD state.

2018年11月18日26分11秒

What happens when the remote no longer exists but Git is too stupid to acknowledge its been deleted? This assumes you updated and git branch -a continues to lists it as a remote branch.

2018年11月18日26分11秒

And we do this for dozens of branches?

2018年11月17日26分11秒

I appreciate your note "all branches and tags are fetched". I was going to comment on your answer being wrong, but then I checked it and found, you are perfectly right. So in a way, you have provided the shortest answer - if you cloned, you already have it. Nice. One could try to add: try $ git branch -a to learn, what remote branches are already available.

2018年11月18日26分11秒

Thanks suraj. The reason because its not been voted much. And the ans is not accepted by the questioner.

2018年11月18日26分11秒

The "git fetch origin" did not bring any of the remote branches to my local - or are they hidden somewhere? Reading all the answers above gave me a headache. We are looking for "git fetch all branches to local". There must be a way aside from bash-scripts to do this.

2018年11月17日26分11秒

Just after you execute "git fetch origin", it will show the output like this in your terminal - "* [new branch] branch_name -> origin/branch_name", but when you run "git branch" it will show you only your local branches instead, so to see all the branches you can do "git branch -a" and then to switch to the remote branch you need to run " git checkout --track origin/<branch you want to checkout>". Hope this helps. :-)

2018年11月18日26分11秒

Suraj, because the question was, how to "clone all remote branches" - not how to manually update one at a time. It appears there is no answer to the actual question - just ways to do a whole lot of typing if you have a lot of branches.

2018年11月18日26分11秒

Thanks for the explanation of why only master is the only visible branch

2018年11月18日26分11秒

Something went wrong during the edits here. Now this answer doesn't make sense. The "--bare" mentioned in the last sentence doesn't exist in the given command list.

2018年11月17日26分11秒

taking from Dave's answer below. Using 'git config --bool core.bare false' instead of 'git config unset core.bare' seems to do the job.

2018年11月17日26分11秒

I have the error: key does not contain a section: unset. The Dave's answer works better.

2018年11月18日26分11秒

It's git config --unset core.bare actually... To me, this seems the cleanest solution of all presented in the answers here. A pity it has so few upvotes...

2018年11月17日26分11秒

Thanks ChrisSim I agree for git config --bool core.bare false. This is why I recommend instead the Dave's answer. What do you think about the Dave's answer? Cheers

2018年11月18日26分11秒

Very clear response here. Lots of folks confused about this topic.

2018年11月18日26分11秒

You can run git commands with or without the hyphen. Both "git-branch" and "git branch" will work.

2018年11月17日26分11秒

Maybe this answer was given a long time ago when git worked differently, but I think it's misleading today. git clone does download all the remote branches, but it only makes a local branch of master. Since git branch only shows local branches, you need git branch -a to see remote branches, too.

2018年11月17日26分11秒

Thank you. This is kind of strange default behavior IMO. I'll just chalk it up to more cryptic gitness. If it downloaded the branches, why would it hide them when calling git branch?

2018年11月18日26分11秒

Cerran, thanks; I've updated my answer accordingly.

2018年11月18日26分11秒

"does download all the remote branches, but it only makes a local branch of master". I need help understanding this. It seems git clone does NOT clone any branches except master, as when you do "git branch -a" it shows that the "develop" branch is only at "remotes/origin/develop". This must be saying that you dont have this branch anywhere locally, it only exists currently on the origin right?

2018年11月18日26分11秒

Word to the wise: It looks like this project was abandoned around the time this answer was posted. I can't find any updates after 2008. Caveat emptor and all that. If I'm wrong, I hope someone will edit and provide a current pointer, because I'd love to have a tool like this handy.

2018年11月17日26分11秒

You can improve the regex, or maybe use Awk instead of grep, to improve the filter to avoid false positives.

2018年11月17日26分11秒

all the branches are 'origin\my_branch_name', which is not definitely what I want.

2018年11月17日26分11秒

I've not seen the construct ${branch##*/} before - looks really useful - any idea where I can find out more on that? can't seem to find under bash anywhere. Thx.

1970年01月01日00分03秒

There is a difference between "you have it" and "you see it". git branch -all will NOT list the remote branches any more when you remove the remote repository.

2018年11月18日26分11秒

ref. coderwall.com/p/0ypmka/git-clone-all-remote-branches-locally

2018年11月17日26分11秒