0

git clone will create a pair of local and remote tracking branches.

git fetch will only create (or update?) the remote tracking branch, and not create a local tracking one, from http://git-scm.com/book/ch3-5.html:

It’s important to note that when you do a fetch that brings down new remote-tracking branches, you don’t automatically have local, editable copies of them. In other words, in this case, you don’t have a new serverfix branch – you only have an origin/serverfix pointer that you can’t modify.

How about git pull? Does git pull create a pair of local and remote tracking branches, or just a remote tracking branch?

Tim
  • 1
  • 141
  • 372
  • 590
  • Possible duplicate of [What are the differences between 'git pull' and 'git fetch'?](http://stackoverflow.com/questions/292357/what-are-the-differences-between-git-pull-and-git-fetch) – Makoto Dec 28 '15 at 02:36
  • @Makoto: according to your link, is it correct that git pull create a pair of local and remote tracking branches? – Tim Dec 28 '15 at 02:43
  • No, that's probably not the case. I thought an answer in that question went over it in a bit more detail, but I guess it didn't. I'll supply my own answer in a bit. – Makoto Dec 28 '15 at 02:46
  • 1
    BTW, for clarity, the git terminology is "local branch" (one adjective in front of "branch") and "remote-tracking branch" (another, often hyphenated, adjective in front of "branch"). The combination adjective-pair "local tracking" should probably be avoided here, although people (and documents) will say things like "local branch foo is now tracking remote-tracking branch upstream/foo", which is confusing, redundant, and redundantly-confusing :-) and hence classic git-speak... – torek Dec 28 '15 at 04:22
  • @torek: why "The combination adjective-pair "local tracking" should probably be avoided here"? I saw the terminology from Version Control with Git by Loeliger 2ed. – Tim Dec 29 '15 at 00:29
  • I had to go look that up. You're correct: on page 199 they define the term "local-tracking branch" to mean "a local branch that has its upstream set to a remote-tracking branch" (via `git branch --set-upstream-to` or similar). I think this is not a good use of terminology though: setting an upstream on a local branch does not change its behavior, it just enables shortcuts (`git rebase` and `git merge` will use the upstream information). Besides this, it's also possible to set the upstream of a local branch to another *local* branch, which messes with that notion. – torek Dec 29 '15 at 01:37
  • @torek: they also define a local, nontracking branch on the same page, and called it a topic/development branch. – Tim Dec 29 '15 at 01:42

1 Answers1

2

When you perform git pull, you're generally performing git fetch and git merge, but the main caveat here is that you're not creating any local branches in this manner. The merge only happens with the branch you're currently pulling into.

For instance, if you were on master, and you performed git pull origin other_branch, you would pull in the latest version of other_branch from origin, but then you would also be merging your changes into your master branch. This operation would not create any local branches on your system.

The general way to create a local tracking branch from remote is through the use of git fetch followed by git checkout. If the branch exists already on your remote repository, then simply executing git checkout new_branch will create a local branch that will track the remote branch for you.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Thanks. is it correct that `git pull` will update the local working directory by merging, while `git fetch` doesn't and leave the local working directory intact until I run `git checkout` following `git fetch`? – Tim Dec 28 '15 at 18:40
  • Another question. After `git fetch`, is it correct that I have two options: either run `git checkout ` to create a local tracking branch for the remote tracking branch and check out the local tracking branch, so that I can work on the fetched commit, or run `git merge` to merge the remote tracking branch into the current local branch? – Tim Dec 28 '15 at 23:48
  • To your first question: `git pull` is `git fetch && git merge`, and generally that is done on a branch that is known to be tracked remotely (i.e. master). In that effect, this operation will update the working directory. Using `git checkout` would update the working directory, if the branch is known to be on the remote repository. To your second question: yes, those are your only two options in that scenario. @Tim – Makoto Dec 29 '15 at 00:02
  • Thanks. In the merge step of `git pull`, Is the remote tracking branch merged into the current branch? Does the merge step modify the remote tracking branch? See http://stackoverflow.com/questions/34502240/branches-in-git-merge-relation-with-the-current-branch-and-whether-they-are-mo – Tim Dec 29 '15 at 00:24