Git pull/fetch with refspec differences
Asked Answered
O

4

16

Using refspec is a convenient way to grab a remote branch and create a similar one but with given name (or the other way round: create a remote one with a given name different from the local one). I'm puzzled about one tiny thing - as pull will also do the merge with current branch I would expect different behavior from:

git fetch origin master:mymaster

and from

git pull origin master:mymaster

Both of the above commands seem to produce exactly same result - that is a local branch called mymaster, same as origin/master. Am I right or is there a vague difference between the two?

Finally, using a refspec will create a local branch not a tracking branch, right? Since tracking branches are pushed automagically when one invokes git push without any arguments AFAIK

Ottinger answered 24/8, 2011 at 0:28 Comment(1)
Would it really be exactly the same result though? I would expect pull to merge origin/master to local master.Exploit
U
34

A refspec is just a source/destination pair. Using a refspec x:y with fetch tells git to make a branch in this repo named "y" that is a copy of the branch named "x" in the remote repo. Nothing else.

With pull, git throws a merge on top. First, a fetch is done using the given refspec, and then the destination branch is merged into the current branch. If that's confusing, here's a step-by-step:

git pull origin master:mymaster
  1. Go to origin and get branch "master"
  2. Make a copy of it locally named "mymaster"
  3. Merge "mymaster" into the current branch

Fully qualified, that would be refs/heads/mymaster and refs/heads/master. For comparison, the default refspec set up by git on a clone is +refs/heads/*:refs/remotes/origin/*. refs/remotes makes a convenient namespace for keeping remote branches separate from local ones. What you're doing is telling git to put a remote-tracking branch in the same namespace as your local branches.

As for "tracking branches", that's just an entry in your config file telling git where to pull and push a local branch to/from by default.

Ultramicrochemistry answered 24/8, 2011 at 1:12 Comment(0)
F
4

git fetch origin master:mymaster updates branch mymaster in the local repository by fetching from the master branch of the remote repository.

git pull origin master:mymaster does above and merges it into the current branch.

Faeroese answered 24/8, 2011 at 1:6 Comment(0)
B
0

git fetch origin master:mymaster

However, the command must meet the following two conditions strictly:

  1. The local current branch cannot be mymaster.

  2. The local mymaster branch is the ancestor of origin/master.

then will make fast-forward merge. Otherwise it will report a fatal.

When both of the above conditions are true, execute the command:

git pull origin master:mymaster 

In addition to executing the command:

git fetch origin master:mymaster

Will also execute:

git merge FETCH_HEAD

Notice:not git merge mymaster

FETCH_HEAD is different from mymaster, because mymaster maybe already fast-forward merge.

Banneret answered 28/7, 2019 at 4:38 Comment(0)
H
-1

I had used smartgit to create branch, so might be at that branch doesn't properly merged into master. Created support branch for release ie support/4.2, tag automatically get created but now wehn I try to do git pull, it shows me same error. As support/4.2 brannch is created in github but not properly merged in local. So I have used this :- git pull origin master:mymaster

In my case - git pull origin support/4.2:support/4.2

It works :)

Hetzel answered 9/7, 2015 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.