简体   繁体   中英

confusion with git fetch and git pull

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>

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.

不同之处在于 git pull 执行 git fetch ,然后执行 git merge 以更新您的本地分支。

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM