I have a git branch checked out named foo
.
> git status
# On branch foo
nothing to commit (working directory clean)
It was originally checked out using this command:
> git checkout origin/foo -b foo --track
I want to get updates to this branch from the remote repository. I know that either of these commands will suffice:
> git fetch origin foo # ignore the lack of merging
> git pull origin foo
If I omit the arguments to fetch
or pull
, will git default to fetching (or pulling) the branch that I currently have checked out? That is, are the following pairs of commands equivalent?
> git checkout foo
> git pull
and
> git checkout foo
> git pull origin foo
Even in the conditions Mark describes, when they appear to be the same, there is still a subtle difference --
git pull origin foo
will not update the remote tracking branch, whereasgit pull
will. This is documented in the manpage for git-pull:A parameter <ref> without a colon is equivalent to <ref>: when pulling/fetching, so it merges <ref> into the current branch without storing the remote branch anywhere locally
So in your case, for the true equivalent to
git pull
, you'd need to dogit pull origin foo:refs/remotes/origin/foo
Yes, they are. The tracking information is saved in
.git/config
.Unfortunately, whether they are equivalent or not in general depends on which branch you are on, your configuration, phase of the moon, etc.
You can figure this out from the
git pull
man page, as I've described below, but I would generally try to avoid having to work that out by doing:git fetch origin
and thengit merge origin/foo
. (I wrote a somewhat rambling blog post about this.)However, your question is really about the default behaviour of
git pull
when you don't specify a remote or a refspec. We can figure this out from thegit pull
man page, and in particular theDEFAULT BEHAVIOUR
section. This is somewhat tough to figure out, so I've put in bold the only parts that really apply to your question given you given that (a) you are on branchfoo
, (b) you created that branch as you described in the question, and (c) you haven't been changing your configuration.When you created the branch
foo
with:... it will have set the following config options, which associate your branch
foo
withrefs/heads/foo
in theorigin
repository:So, if you put that together with the emboldened sentences above, the answer is "Yes, in this situation you describe, when you're on branch
foo
, the commandsgit pull
andgit pull origin foo
are equivalent."