2

I was going through git scm book and reading the Pulling section on this page. It says:

While the git fetch command will fetch down all the changes on the server that you don’t have yet, it will not modify your working directory at all. It will simply get the data for you and let you merge it yourself.

I have used 'git fetch upstream' and it does merge all the changes existing in the remote branch to my local branch, which according to me updates the working directory as well. But then it contradicts the above statement.

I am confused and not able to get my head around it. Can someone please explain?

I have already gone through the links which are marked as duplicate. I may as well be dumb but I didn't get any clarity of the above statement from those answers. Please help.

Update

Here's the command that I ran and its output:

bash-3.2$ git fetch upstream
remote: Counting objects: 108, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 108 (delta 77), reused 77 (delta 77), pack-reused 23
Receiving objects: 100% (108/108), 25.92 KiB | 0 bytes/s, done.
Resolving deltas: 100% (79/79), completed with 31 local objects.
From <git-repo-url>
   a82339d..9844eeb  master -> upstream/master
   802bae5..6c84bfb  <some-branch> -> upstream/<some-branch>
Bharat Gupta
  • 2,628
  • 1
  • 19
  • 27
  • 1
    Possible duplicate of [Difference between 'git pull' and 'git fetch'](http://stackoverflow.com/questions/292357/difference-between-git-pull-and-git-fetch) – Mayank Shukla Feb 09 '17 at 08:58
  • there are many answers available on SO with best explanation for the same ques, refer any one. – Mayank Shukla Feb 09 '17 at 08:59
  • That's true...you can find plenty of similar answers..anyway I try to collect some command differences in my answer...hope this help!! Have a nice day! – Marco smdm Feb 09 '17 at 09:00
  • check this one also: http://stackoverflow.com/questions/14894768/in-git-how-is-fetch-different-than-pull-and-how-is-merge-different-than-rebase – Mayank Shukla Feb 09 '17 at 09:02
  • Can you include the exact commands that you used in your question, and the output which leads you to believe that `git fetch` has updated your working directory, please? – mkrieger1 Feb 09 '17 at 12:20
  • @mkrieger1 I have updated the question with the command and its output. – Bharat Gupta Feb 09 '17 at 12:35
  • 1
    Well. As expected, `git fetch` downloaded all missing objects from the "upstream" repository, and updated the remote branches. But it did not change your local branches, and didn't change the working directory. – mkrieger1 Feb 09 '17 at 12:40

3 Answers3

2

I have used 'git fetch upstream' and it does merge all the changes existing in the remote branch to my local branch, which according to me updates the working directory as well. But then it contradicts the above statement.

git fetch doesn't update your local branches nor your working directory.

You might think it updates a local branch, given a line like this,

   a82339d..9844eeb  master -> upstream/master

but according to the Output section in the git-fetch manual, this means that

  • master is the name of the "ref" (a branch is one kind of ref) in the remote repository being fetched from, and
  • upstream/master is the name of the ref in the local repository being updated.

Note that the branch master in the local repository is not updated.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
1

The difference is that a git pull does a git fetch followed by a git merge to update your local branch.

Harald Greve
  • 147
  • 3
  • 14
1

Not sure why this is generating confusion....anyway: the comment you mentioned is exactly what git fetch is doing. Please have a look at old What is the difference between 'git pull' and 'git fetch'?

For example: git fetch origin --> will fetch the changes but not merge into yours git merge origin/master --> has to be used to merge afterwards.

Other way to do this in a fashion and fast way is to use pull: git pull origin(whatever) --> will do both in one shot.

Hope this clarify a bit your doubts :-) Have a nice day!

Community
  • 1
  • 1
Marco smdm
  • 1,020
  • 1
  • 15
  • 25