标签云

微信群

扫码加入我们

WeChat QR Code

Somebody pushed a branch called test with git push origin test to a shared repository. I can see the branch with git branch -r.

Now I'm trying to check out the remote test branch.

I've tried:

  • git checkout test which does nothing

  • git checkout origin/test gives * (no branch). Which is confusing. How can I be on "no branch"?

How do I check out a remote Git branch?


inger But it does not include the possibility to rename the new local branch (if you want to --set-upstream later on and keep naming consistency)

2018年08月16日26分15秒

I think this thread is unhelpful. Nothing seems to work, the original question seems to have been lost in many of the answers. I have read every word, tried everything below, and have no idea how to do what the OP wants to do.

2018年08月15日26分15秒

Git commands are not intuitive to begin with, add the changes introduced with recent versions to the mix and you have this page...

2018年08月15日26分15秒

I feel like I'm taking crazy pills. I'm trying to checkout a branch from an upstream, not just origin, and every recommended answer doesn't do anything remotely helpful (pun-intended). EDIT - excuse me, the multitude of suggestions contained in the top 2 answers were useless; 3rd one (git branch test origin/test) is what works. Glad the top 2 have 20x the number of votes...

2018年08月16日26分15秒

Maybe you have a file named 'test' in your work tree, see stackoverflow.com/a/45006389/792416 for detail.

2018年08月16日26分15秒

To expand on this: git doesn't allow you to work on someone else's branches. You can only work on your own. So if you want to add to someone else's branch, you need to create your own "copy" of that branch, which is what the above command does (well, it creates your branch and checks it out, too).

2018年08月16日26分15秒

If it's a new remote branch you may need to git fetch before doing this so that git is aware of origin/test

2018年08月15日26分15秒

...and you would do this with git fetch origin test

2018年08月16日26分15秒

Error: "git checkout: updating paths is incompatible with switching branches. Did you intend to checkout origin/test which can not be resolved as commit?"

2018年08月16日26分15秒

git checkout test will NOT work in modern git if you have multiple remotes which have the same branch name. It can't know which one to use.

2018年08月15日26分15秒

Unsurprising, but this version has been released in the last few years - knowing this syntax can save a lot of time since there's still a lot of old documentation and comment threads floating around that suggest the older method for doing this.

2018年08月16日26分15秒

"modern git"--for the record, (approx) what version are you referring to? Sometimes we have to work on systems running older distros.

2018年08月16日26分15秒

"modern git" in this context is git 1.6.6

2018年08月16日26分15秒

aidan If you get a response like error: pathspec 'branch_name' did not match any file(s) known to git. then you should do a git fetch first.

2018年08月16日26分15秒

Using git version 1.8.3.msysgit.0 and this doesn't work for me - did not match any file(s) known to git - I've done many git fetches

2018年08月16日26分15秒

This will create a local branch without switching to it.

2018年08月16日26分15秒

Though I got fatal: Ambiguous object name: 'origin/dev' - where a branch 'dev' on origin most definitely exists - but I'd accidentally created a branch called "origin/dev" on my machine (in my previous stupid attempts to get this right, no doubt) ... ouch

2018年08月16日26分15秒

This has been giving me the error error: failed to push some refs to hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart. Check out this branch and merge the remote changes hint: (e.g. 'git pull') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

2018年08月16日26分15秒

git branch test works for me

2018年08月15日26分15秒

I'll add a note if you have a separate branch locally: Make sure you have associated this with the remote repo using 'git remote add origin [the_path_to_your_repo/repo_name.git]' . Then use 'git fetch origin' where 'origin' means the origin repository you have made the association with.

1970年01月01日00分03秒

git checkout -b newbranch also works great for 1-step create and checkout a new branch based on the current branch.

2018年08月15日26分15秒

I think this is the most up-to-date (it keeps $#!ing changing!). Git 2.5.5 I found the only way to actually see the remote branches was git ls-remote and the only way to actually use one is git checkout -b [branch] --track [remote/branch]...and that's after git pull [remote] [branch] worked. I.e., it actually pulled the whole branch, but still wouldn't list it.

2018年08月15日26分15秒

and when this answer doesn't work either, see this one.

2018年08月15日26分15秒

This worked for me when neither git fetch origin or git remote update created local branches. I'm not sure why.

2018年08月16日26分15秒

This was the most direct way to accomplish what I needed which was to use a remote branch (not master) to create a new branch.

2018年08月16日26分15秒

Worked seamlessly, especially when having cloned a single branch from a remote with multiple branches.

2018年08月15日26分15秒

this worked for me too, where accepted answers and other high voted didn't. My git version is 2.5.0

2018年08月16日26分15秒

Does anyone have any idea why this works when everything else doesn't? (I'm on git 2.13.0)

2018年08月16日26分15秒

Do you realize that this is an extract of this answer

2018年08月16日26分15秒

Looking at it now, they do overlap. Only mine is succinct and tells you what to do rather than tell a story. I assume it can be more useful therefore, especially for nowadays git versions. You can downvote it if you think it is a bad answer.

2018年08月15日26分15秒

git clone <either ssh url /http url> - works perfectly for me

2018年08月15日26分15秒

Yes you are correct. Thanks for your information, I will update it very soon warvariuc

2018年08月15日26分15秒

If the remote has no master, this is not going to work.

2018年08月16日26分15秒

Great picture, thanks

2018年08月16日26分15秒

Does not work if there is no local branch test

2018年08月16日26分15秒

DmitriZaitsev yes, it will work, if the remote branch is there, and you do fetch, you will get the branch locally... git fetch && git checkout test..So this works, unless there is no remote branch, but the question saying there is already a remote branch there...

2018年08月16日26分15秒

The way I see the question, test looks like a new branch, so it is not likely to be present locally. Otherwise you could pull it easier with single git pull command.

2018年08月15日26分15秒

DmitriZaitsev, yes, that's why I said git fetch, that check for remote branches which just created, pull can bring other unwanted stuffs in, but fetch makes all branches available if you already have the repo locally...

2018年08月16日26分15秒

FYI, --track is no longer needed in newer versions of git, because it's set by default, as explained in this earlier answer.

2018年08月15日26分15秒

I dont understand -b. If you can do "git checkout master" why cant you do "git checkout origin/test"?

2018年08月16日26分15秒

-b for new branch which is from origin/master

2018年08月16日26分15秒

This answer would be a lot more useful if you explain why you are using it this way. i.e. why someone should use '--track' and so on...

2018年08月16日26分15秒

FYI, git remote update will also fetch all remote branches.

2018年08月15日26分15秒

hallski answered not working 2.15.1 versions but i have reduce .git file weigth clone only 1 branch and filter history with --depth flag. for example $ git clone -b release --single-branch --depth 5 https://github.com/user/repo.git Wron't information $ git remote show originthis does not listed all remote branch with single branch cloned repositories.

2018年08月15日26分15秒

thanks. I was wondering if I had to use the full path (remotes/origin/feature/branch) that I saw in git when calling git branch -a command, but I wasn't for sure, so I just used git checkout -b apps/FEATURE/branch origin/apps/FEATURE/branch and it appeared to work. Message: Branch 'apps/FEATURE/branch' set up to track remote branch 'apps/FEATURE/epicBranch' from 'origin'. Switched to a new branch 'apps/FEATURE/branch'

2018年08月16日26分15秒