Update: Using Git Switch
All of the information written below was accurate, but a new command, git switch has been added that simplifies the effort.
If daves_branch exists on the remote repository, but not on your local branch, you can simply type:
git fetch --all
git switch daves_branch
Since you do not have the branch locally, this will automatically make switch look on the remote repo. It will then also automatically set up remote branch tracking.
Original Post
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 recent 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 (it 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 are here: 3.5 Git Branching - Remote Branches, Tracking Branches
Answer from ralphtheninja on Stack OverflowUpdate: Using Git Switch
All of the information written below was accurate, but a new command, git switch has been added that simplifies the effort.
If daves_branch exists on the remote repository, but not on your local branch, you can simply type:
git fetch --all
git switch daves_branch
Since you do not have the branch locally, this will automatically make switch look on the remote repo. It will then also automatically set up remote branch tracking.
Original Post
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 recent 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 (it 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 are here: 3.5 Git Branching - Remote Branches, Tracking Branches
I have used fetch followed by checkout...
git fetch <remote> <rbranch>:<lbranch>
git checkout <lbranch>
...where <rbranch> is the remote branch or source ref and <lbranch> 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 <refspec>.
Bash is so smart it auto completes the first command if I tab after the first few letters of the remote branch. That is, I don't even have to name the local branch; Bash automatically copies the name of the remote branch for me. Thanks, Bash!
Also as the answer in this similar Stack Overflow 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. That is, git fetch <remote> <branch> followed by git checkout -b <branch> <remote>/<branch> does exactly the same as my initial answer. And evidently, if your repository has only one remote, then you can just do git checkout <branch> after fetch and it will create a local branch for you. For example, you just cloned a repository 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 <refspec> 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: git fetch <remote> <refspec> is short for git fetch <remote> <refspec>: which would therefore do nothing, but git fetch <remote> <tag> is the same as git fetch <remote> <tag>:<tag> which should copy the remote <tag> 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, 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 one-liner. Well... sort of a one-liner, because you would still have to run git fetch <remote> first.
FYI: The order of the <refspecs> (source:destination) explains the bizarre pre Git 1.7 method for deleting remote branches. That is, push nothing into the destination refspec.
How do you update your local branch?
git clone gives `WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!`
Is It Acceptable to Rename a Git Branch I Created?
Discard all changes made to a file in a branch
How do I pull a specific remote branch?
How do I pull a remote branch that I don't have locally?
How do I pull with rebase instead of merge?
Hey,
Anyone know is it possible to do a git pull from another remote branch on top of your local git and see all the changes the new pull will make? instead of forcing all the changes and only showing merge conflicts. Git fetch doesn't add changes on top of the files in code.
git pull <remote> <branch>
?
If you wanted to see the remote changes, but you aren't sure if you want to pull / merge yet, you could do git fetch, then checkout the remote branch.
Assuming remote name is origin, and branch name is master, these commands should do:
git fetch
git checkout origin/master
The remote changes should now be present in your local directory. To go back to your local branch, a simple
git checkout master
should do.