Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pull/fetch with refspec differences

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

like image 898
pielgrzym Avatar asked Aug 24 '11 00:08

pielgrzym


People also ask

What is difference between pull and fetch in git?

git fetch is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn't do any file transferring. It's more like just checking to see if there are any changes available). git pull on the other hand does that AND brings (copy) those changes from the remote repository.

Should I use git pull or fetch?

When comparing Git pull vs fetch, Git fetch is a safer alternative because it pulls in all the commits from your remote but doesn't make any changes to your local files. On the other hand, Git pull is faster as you're performing multiple actions in one – a better bang for your buck.

Will git pull overwrite local changes?

The reason for error messages like these is rather simple: you have local changes that would be overwritten by the incoming new changes that a "git pull" would bring in. For obvious safety reasons, Git will never simply overwrite your changes.

Does git pull fetch all tags?

git fetch fetches all branch heads (or all specified by the remote. fetch config option), all commits necessary for them, and all tags which are reachable from these branches. In most cases, all tags are reachable in this way.


1 Answers

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.

like image 173
Ryan Stewart Avatar answered Oct 18 '22 08:10

Ryan Stewart