标签云

微信群

扫码加入我们

WeChat QR Code

My colleague and I are working on the same repository we've branched it into two branches each technically for different projects, but they have similarities so we'll sometimes want to commit back to the *master from the branch.

However, I have the branch. My question is, how can my colleague pull that branch specifically? A git clone of the repo does not seem to create the branches locally for him, though I can see them live on unfuddle after a push on my end.

Also, when I originally made the branch I did -b checkout. Not sure if that makes much difference?

iMac:test_solar dave$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/discover
origin/master

git fetch origin discover
git checkout discover

These are the commands I ran. But it definitely is not working. I want to be able to check out that branch and then push and commit back just the branches changes from various collaborators or workstations.


possible duplicate of git checkout remote branch

2018年08月16日26分27秒

I used: git fetch --all, Then to view all the branches: git branch, Then I checkout the branch: git checkout nameofnewbranch

2018年08月16日26分27秒

check your question ans https://stackoverflow.com/questions/1072261/push-and-pull-branches-in-git/47780374#47780374

2018年08月16日26分27秒

"git fetch" to make sure your repo is updated with remote references and "git checkout --track origin/discover" should be enough. Then you can commit to that branch and a "git push" to sync the remote with your changes.

2018年08月16日26分27秒

I tried this and got "fatal: git checkout: updating paths is incompatible with switching branches. Did you intend to checkout 'upstream/develop' which can not be resolved as commit?". Am I doing something wrong?

2018年08月16日26分27秒

Looks like git 1.5.6.5 needs this instead: git checkout --track -b origin/daves_branch

2018年08月15日26分27秒

This made a mess for me, it created a local branch named origin/<branch> which is now ambiguous to the remote branch origin/<branch> and I don't know how to get rid of the crazy local branch!

2018年08月16日26分27秒

You need to add the local branch name explicitly, otherwise git creates a new local branch with the full branch path, as AlanMoore and derekmx271 stated above: git checkout -b --track daves_branch origin/daves_branch

2018年08月15日26分27秒

You assume Git autocompletion is setup. code-worrier.com/blog/autocomplete-git

2018年08月15日26分27秒

This worked for me to get the remote code into a local branch. It did not however get my local branch to track the remote branch.

2018年08月16日26分27秒

This worked for me, the accepted answer didn't.

2018年08月16日26分27秒

For some reason, git fetch remote branch didn't add a branch head at all for me, though all the refs got fetched, so when I tried to follow the steps in the accepted answer, I got the error that pathspec did not match any file(s) known to git., but the rbranch:lbranch approach worked. Interestingly, it also fetched all the tags that started with the same prefix, like it is a wildcard (rbranch*).

2018年08月15日26分27秒

Nit: git doesn't do the autocompleting, it is the bash shell that is doing it.

2018年08月15日26分27秒

"pull" from origin is confusing language, better to change your comment above to "fetch" from origin. Pull is a command that does a fetch and a merge and that certainly confuses newcomers (me included)

2018年08月16日26分27秒

Worked for me! Adding it into my notes now :) Thank u!

2018年08月15日26分27秒

Worked for me, I hadn't fetched the correct remote branch so the accepted answer kept failing for me with a confusing message. +1

2018年08月15日26分27秒

Simple and sweet, I'm sure most users just want to do this operation at first place.

2018年08月16日26分27秒

This is what I have always been looking for ! Thank you paneer_tikka

2018年08月16日26分27秒

In other words you don't have to write -t?

2018年08月16日26分27秒

I think there's an error in this answer. I originally did the command without -t and got You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. because there wasn't a local branch of the same name. I had to re-run with -t to fix.

2018年08月15日26分27秒

This worked well for me - my colleague had added a new remote branch that I wanted to add to my local repo. I kept fetching but didn't see the new branch appearing locally. Didn't realize that I could just run checkout for it to create it. Thanks!

2018年08月16日26分27秒

depends on git versions. Latest version, as said elsewhere here, only needs git checkout <name of branch on origin>. BE CAREFUL WITH SAME-NAME LOCAL vs REMOTE BRANCHES - they will mess stuff up

2018年08月16日26分27秒

git checkout remotes/repo/branch makes git checkout look for a pathspec, not a remote repo.

2018年08月15日26分27秒

Yes, is it even possible to checkout a branch on the remote repo? Obviously (or maybe it wasn't so obvious), remotes are first fetched so that you have them locally. The git book has a good section on them: git-scm.com/book/en/Git-Branching-Remote-Branches

2018年08月15日26分27秒

Thanks Guillaume! I just used this command and wrote a post about it to depict my exact case: leniel.net/2014/05/…

2018年08月16日26分27秒

<!-- git checkout <remote-branch-name> --> works as expected, Thanks Guillaume!

2018年08月15日26分27秒

Works! Thanks you!

2018年08月15日26分27秒

great simple answer that works. should be toward the top IMO

2018年08月16日26分27秒

from git checkout documentation: If <branch_name> is not found but there does exist a tracking branch in exactly one remote with a matching name, treat as equivalent to: git checkout -b <branch_name> --track <remote>/<branch_name>

2018年08月15日26分27秒

Does not bring other branches

2018年08月16日26分27秒

BenjaminHarel the question says "fetch a remote branch" not all branches. for that follow this may be helpfull for you stackoverflow.com/questions/10312521/…

2018年08月15日26分27秒

This worked for me.

2018年08月16日26分27秒