Git fetch remote branch

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

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, 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 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 

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

回答1:

You need to create a local branch that tracks a remote branch. The following command will create a local branch named daves_branch, tracking the remote branch origin/daves_branch. When you push your changes the remote branch will be updated.

For most versions of git:

git checkout --track origin/daves_branch 

--track is shorthand for git checkout -b [branch] [remotename]/[branch] where [remotename] is origin in this case and [branch] is twice the same, daves_branch in this case.

For git 1.5.6.5 you needed this:

git checkout --track -b daves_branch origin/daves_branch 

For git 1.7.2.3 and higher this is enough (might have started earlier but this is the earliest confirmation I could find quickly):

git checkout daves_branch 

Note that with recent git versions, this command will not create a local branch and will put you in a 'detached HEAD' state. If you want a local branch, use the --track option. Full details here: http://git-scm.com/book/en/v2/Git-Branching-Remote-Branches#Tracking-Branches



回答2:

I have used fetch followed by checkout ...

git fetch :  git checkout 

... where is the remote branch or source ref and is the as yet non-existent local branch or destination ref you want to track and which you probably want to name the same as the remote branch or source ref. This is explained under options in the explanation of .

Git is so smart it auto completes the first command if I tab after the first few letters of the remote branch. IE: I don't even have to name the local branch, Git automatically copies the name of the remote branch for me. Thanks Git!

Also as the answer in this similar SO post shows, if you don't name the local branch in fetch, you can still create it when you check it out by using the -b flag. IE: git fetch followed by git checkout -b / does exactly the same as my initial answer. And evidently if your repo has only one remote, then you can just do git checkout after fetch and it will create a local branch for you. EG: You just cloned a repo and want to check out additional branches from the remote.

I believe that some of the documentation for fetch may have been copied verbatim from pull. In particular the section on in options is the same. However, I do not believe that fetch will ever merge, so that if you leave the destination side of the colon empty fetch should do nothing.

NOTE: That git fetch is short for git fetch : which would therefore do nothing, but git fetch is the same as git fetch : which should copy the remote locally.

I guess this is only helpful if you want to copy a remote branch locally, but not necessarily check it out right away. Otherwise I now would use the accepted answer above, which is explained in detail in the first section of the checkout description and later in the options section under the explanation of --track, since it's a 1-liner. Well... sort of a 1-liner, because you would still have to run git fetch first.

FYI: The order of the (source:destination) explains the bizarre pre Git-1.7 method for deleting remote branches. IE: Push nothing into the destination refspec.



回答3:

If you are trying to "checkout" a new remote branch (that exists only on the remote, but not locally), here's what you'll need:

git fetch origin git checkout --track origin/

This assumes you want to fetch from origin. If not, replace origin by your remote name.



回答4:

To checkout myBranch that exists remotely and not a locally - This worked for me:

git fetch --all git checkout myBranch 

I got this message:

Branch myBranch set up to track remote branch myBranch from origin Switched to a new branch 'myBranch' 


回答5:

Use git branch -a (both local and remote branches) or git branch -r (only remote branches) to see all the remotes and their branches. You can then do a git checkout -t remotes/repo/branch to the remote and create a local branch.

There is also a git ls-remote command to see all the refs and tags for that remote.



回答6:

The title and the question are confused:

  • Git fetch remote branch
  • how can my colleague pull that branch specifically.

If the question is how can I get a remote branch to work with or how to git checkout a remote branch, a simpler solution is:

With git (>= 1.6.6) you are able to use:

git checkout 

If local 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  --track /

see documentation for git checkout

For your friend:

$ git checkout discover Branch discover set up to track remote branch discover Switched to a new branch 'discover' 


回答7:

git checkout -b serverfix origin/serverfix 

This is a common enough operation that git provides the --track shorthand:

git checkout --track origin/serverfix 

In fact, this is so common that there’s even a shortcut for that shortcut. If the branch name you’re trying to checkout (a) doesn’t exist and (b) exactly matches a name on only one remote, Git will create a tracking branch for you:

git checkout serverfix 

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

git checkout -b sf origin/serverfix 

Now, your local branch sf will automatically pull from origin/serverfix.

Source: Pro Git 2nd Edition, written by Scott Chacon and Ben Straub (cut for readability)



回答8:

git fetch  git branch -r  git checkout 


回答9:

You can fetch and checkout the remote branch in one shot too:-

git fetch && git checkout the-branch-name 


回答10:

I typed

git checkout 

and got

Branch  set up to track remote branch  from origin. Switched to a new branch '' 


回答11:

At times you are asked not to fiddle with the master branch and work only the remote branch (as I was asked to). So all you need is the remote branch.

So to clone the remote branch alone (without the master), do this

git clone url --branch remote_branch_name 

where, remote_branch_name is the name of the remote branch

For example,

git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git --branch v3.15 

This will make sure that you clone the remote branch to your local branch with the name of the remote branch.

Now if you commit your code and push, the code will be submitted to that branch alone.



回答12:

git fetch --all & git checkout



回答13:

The steps are as follows;

  1. git fetch origin or git fetch --all , this will fetch all the remote branches to your local and then this the second option you can proced with.

  2. git checkout --track origin/

Then work on this branch and you can verify whether you are on that branch or not by typing

git branch 

It displayes the branch you currently in.



回答14:

If you already know your remote branch like so...

git remote => One => Two 

and you know the branch name you wish to checkout ex. br1.2.3.4 then do

git fetch One => returns all meta data of remote i.e. the branch name in question. 

all that is left is to checkout the branch

git checkout br.1.2.3.4 

Then make any new branches off of it.



回答15:

With this simple command:

git checkout -b 'your_branch' origin/'remote branch' 


回答16:

The easiest way to do it, at least for me:

git fetch origin 


回答17:

You use 'git pull' to keep your branches separate. I will use actual repo and branch names to help since 'lbranch' and 'rbranch' is tough to decipher.

Let's use:

  • myteam.unfuddle.com = the remote git server
  • tlc = unfuddle project account where the repo exists
  • daves_branch = remote branch name

    You, or any colleague, can run this to pull only your branch, no matter how many branches there are:

     git init git pull git@myteam.unfuddle.com:myteam/tlc daves_branch:refs/remotes/origin/daves_branch 


  • 回答18:

    Check your .git/config, particulary what tracking is present on fetch for that remote.

    [remote "randomRemote"]     url = git@github.com:someUser/someRepo.git     fetch = +refs/heads/*:refs/remotes/randomRemote/* 

    If it has heads/* pointing to randomRemote/*, when you run git fetch randomRemote, it will fetch all branches. Then you can just checkout that branch.

    Otherwise,

    1. You need to add remote branches to the tracking using this. Check your .git/config after running this. You will understand. git remote set-branches --add randomRemote randomBranch

    2. Run git fetch randomRemote. This will fetch the remote branch.

    3. Now you can run git checkout randomBranch



    回答19:

    simply try

    $git pull origin your_branch_name



    回答20:

    A simple command -"git checkout remote_branch_name" will help you to create a local branch that has all the changes in remote branch.



    回答21:

    If you have a repository that was cloned with --depth 1 then many of the commands that were listed will not work. For example see here

    % git clone --depth 1 https://github.com/repo/code Cloning into 'code'... cd code remote: Counting objects: 1778, done. remote: Compressing objects: 100% (1105/1105), done. remote: Total 1778 (delta 87), reused 1390 (delta 58), pack-reused 0 Receiving objects: 100% (1778/1778), 5.54 MiB | 4.33 MiB/s, done. Resolving deltas: 100% (87/87), done. Checking connectivity... done. Checking out files: 100% (1215/1215), done. % cd code % git checkout other_branch error: pathspec 'other_branch' did not match any file(s) known to git. % git fetch origin other_branch remote: Counting objects: 47289, done. remote: Compressing objects: 100% (15906/15906), done. remote: Total 47289 (delta 30151), reused 46699 (delta 29570), pack-reused 0 Receiving objects: 100% (47289/47289), 31.03 MiB | 5.70 MiB/s, done. Resolving deltas: 100% (30151/30151), completed with 362 local objects. From https://github.com/repo/code  * branch            other_branch-> FETCH_HEAD % git checkout other_branch error: pathspec 'other_branch' did not match any file(s) known to git. % 

    In this case I would reclone the repo, but perhaps there are other techniques e.g. git shallow clone (clone --depth) misses remote branches



    回答22:

    Let's say that your remote is git@xyz.git and you want its random_branch branch. The process should be as follows:

    1. First check the list of your remotes by

      git remote -v

    2. If you don't have the git@xyz.git remote in the above command's output, you would add it by

      git remote add xyz git@xyz.git

    3. Now you can fetch the contents of that remote by

      git fetch xyz

    4. Now checkout the branch of that remote by

      git checkout -b my_copy_random_branch xyz/random_branch

    5. Check the branch list by

      git branch -a

    The local branch my_copy_random_branch would be tracking the random_branch branch of your remote.



    回答23:

    git branch --track origin/



    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!