Chinaunix首页 | 论坛 | 博客
  • 博客访问: 448259
  • 博文数量: 362
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-07-26 17:08
文章分类

全部博文(362)

文章存档

2015年(362)

我的朋友

分类: LINUX

2015-12-11 00:22:21

当git仓库中存在远程分支,并同时用了一个本地分支去跟踪它的时候,我们通常会使用fetch或者pull从remote中抓取数据。

首先我们先看看git fetch 和 git pull的区别:

以下两篇为转载内容,人家已经讲的很好了,我没必要再废话:

git fetch && pull

  1. git fetch 与 git pull 都是从 remote 端取信息
  2. 在不接参数时,git fetch 与 git pull 有如下不同
  3. 1. git fetch 可以在一个 bare类型的repository内执行,而git pull 不可以
  4. 2. git fetch 只从远程端取repository信息,如新的branch,tag,及新的代码变化,也就是更新.git或bare型repository中的内容
  5. From git://git.kernel.org/pub/scm/boot/u-boot/galak/u-boot
  6. * [new branch] base -> origin/base
  7. * [new branch] bootm -> origin/bootm
  8. * [new branch] fsl_ddr -> origin/fsl_ddr
  9. * [new branch] master -> origin/master
  10. * [new branch] mpc85xx -> origin/mpc85xx
  11. * [new branch] origin -> origin/origin
  12. * [new tag] DENX-2005-10-29-2350 -> DENX-2005-10-29-2350
  13. * [new tag] LABEL_2002_11_05_0120 -> LABEL_2002_11_05_0120
  14. * [new tag] LABEL_2002_11_05_1735 -> LABEL_2002_11_05_1735
  15. 但 git fetch 不会 checkout出任何代码
  16. git pull 不仅会执行git fetch的操作,git pull 还做了merge的操作。

  1. Git中从远程的分支获取最新的版本到本地有这样2个命令:
  2. 1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge
  3. git fetch origin master
  4. git log -p master..origin/master
  5. git merge origin/master
  6. 以上命令的含义:
  7. 首先从远程的origin的master主分支下载最新的版本到origin/master分支上
  8. 然后比较本地的master分支和origin/master分支的差别
  9. 最后进行合并
  10. 上述过程其实可以用以下更清晰的方式来进行:
  11. git fetch origin master:tmp
  12. git diff tmp
  13. git merge tmp
  14. 从远程获取最新的版本到本地的test分支上
  15. 之后再进行比较合并
  16. 2. git pull:相当于是从远程获取最新版本并merge到本地
  17. git pull origin master
  18. 上述命令其实相当于git fetch 和 git merge
  19. 在实际使用中,git fetch更安全一些
  20. 因为在merge前,我们可以查看更新情况,然后再决定是否合并
~~~~~~~~~~~~~~~~~~转载结束~~~~~~~~~~~~~~~~~~~~
    
    在含有工作副本的git仓库中可以使用git pull,在获取远程数据后,本地跟踪分支就可以快进,并和远程分支指向的commit点保持一致在bare仓库中只能使用fetch从remote中抓取数据,但是本地已跟踪的分支可能并没有快进到和remote同步的commit点,出现例如:
  1. tekkaman_b2 pushes to tekkaman_b2 (local out of date)
如果需要本地分支快进,与remote同步,可使用以下命令:
  1. git fetch origin tekkaman_b2:tekkaman_b2
  2. git fetch :
 
以上的命令可以解决本地跟踪分支与远程不同步的问题。

这个方法在非bare仓库中同样适用,但是在实际的使用中存在特殊情况:
在非bare仓库中fetch的特殊情况
  1. tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git branch
  2. master
  3. no_remote
  4. * tekkaman_b2
  5. tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git fetch origin
  6. remote: Counting objects: 4, done.
  7. remote: Compressing objects: 100% (2/2), done.
  8. remote: Total 3 (delta 1), reused 0 (delta 0)
  9. Unpacking objects: 100% (3/3), done.
  10. From /home/tekkaman/development/research/git/test1
  11. e3030a6..ba86b38 tekkaman_b2 -> origin/tekkaman_b2
  12. tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git remote show origin
  13. * remote origin
  14. Fetch URL: /home/tekkaman/development/research/git/test1
  15. Push URL: /home/tekkaman/development/research/git/test1
  16. HEAD branch: tekkaman_b2
  17. Remote branches:
  18. master tracked
  19. no_remote tracked
  20. tekkaman_b1 tracked
  21. tekkaman_b2 tracked
  22. Local branch configured for 'git pull':
  23. tekkaman_b2 merges with remote tekkaman_b2
  24. Local refs configured for 'git push':
  25. master pushes to master (up to date)
  26. no_remote pushes to no_remote (up to date)
  27. tekkaman_b2 pushes to tekkaman_b2 (local out of date)
  28. tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git fetch origin tekkaman_b2:tekkaman_b2
  29. fatal: Refusing to fetch into current branch refs/heads/tekkaman_b2 of non-bare repository
  30. fatal: The remote end hung up unexpectedly
  31. tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git checkout master
  32. Switched to branch 'master'
  33. tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git fetch origin tekkaman_b2:tekkaman_b2
  34. From /home/tekkaman/development/research/git/test1
  35. e3030a6..ba86b38 tekkaman_b2 -> tekkaman_b2
  36. tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git remote show origin
  37. * remote origin
  38. Fetch URL: /home/tekkaman/development/research/git/test1
  39. Push URL: /home/tekkaman/development/research/git/test1
  40. HEAD branch: tekkaman_b2
  41. Remote branches:
  42. master tracked
  43. no_remote tracked
  44. tekkaman_b1 tracked
  45. tekkaman_b2 tracked
  46. Local branch configured for 'git pull':
  47. tekkaman_b2 merges with remote tekkaman_b2
  48. Local refs configured for 'git push':
  49. master pushes to master (up to date)
  50. no_remote pushes to no_remote (up to date)
  51. tekkaman_b2 pushes to tekkaman_b2 (up to date)
    从上面的实验中可以看出:在非bare的git仓库中,如果你要同步的本地跟踪分支是当前分支,就会出现拒绝fetch的情况。也就是说不可以在非bare的git仓库中通过fetch快进你的当前分支与远程同步。个人和我同事的理解是:如果本地数据被修改过了,这种同步的fetch会更改本地的文件(相当于fetch后再checkout到某个commit点),可能影响当前未提交的的工作。

    但是暂时没有更多证据来证实我们的猜想。在网上找的的唯一参考:git refusing to fetch into current branch 其中的一句话是这样的:
“What you're trying to do is to fetch the branch you're workin on. That is, you are on the master branch and you try to update it. That's not possible. ”

阅读(2004) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~