当git仓库中存在远程分支,并同时用了一个本地分支去跟踪它的时候,我们通常会使用fetch或者pull从remote中抓取数据。
首先我们先看看git fetch 和 git pull的区别:
以下两篇为转载内容,人家已经讲的很好了,我没必要再废话:
git fetch && pull
- git fetch 与 git pull 都是从 remote 端取信息
-
在不接参数时,git fetch 与 git pull 有如下不同
-
-
1. git fetch 可以在一个 bare类型的repository内执行,而git pull 不可以
-
2. git fetch 只从远程端取repository信息,如新的branch,tag,及新的代码变化,也就是更新.git或bare型repository中的内容
-
如
-
From git://git.kernel.org/pub/scm/boot/u-boot/galak/u-boot
-
* [new branch] base -> origin/base
-
* [new branch] bootm -> origin/bootm
-
* [new branch] fsl_ddr -> origin/fsl_ddr
-
* [new branch] master -> origin/master
-
* [new branch] mpc85xx -> origin/mpc85xx
-
* [new branch] origin -> origin/origin
-
* [new tag] DENX-2005-10-29-2350 -> DENX-2005-10-29-2350
-
* [new tag] LABEL_2002_11_05_0120 -> LABEL_2002_11_05_0120
-
* [new tag] LABEL_2002_11_05_1735 -> LABEL_2002_11_05_1735
-
但 git fetch 不会 checkout出任何代码
-
git pull 不仅会执行git fetch的操作,git pull 还做了merge的操作。
- Git中从远程的分支获取最新的版本到本地有这样2个命令:
-
-
1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge
-
-
git fetch origin master
-
git log -p master..origin/master
-
git merge origin/master
-
-
-
以上命令的含义:
-
首先从远程的origin的master主分支下载最新的版本到origin/master分支上
-
然后比较本地的master分支和origin/master分支的差别
-
最后进行合并
-
-
上述过程其实可以用以下更清晰的方式来进行:
-
-
git fetch origin master:tmp
-
git diff tmp
-
git merge tmp
-
-
-
从远程获取最新的版本到本地的test分支上
-
之后再进行比较合并
-
-
2. git pull:相当于是从远程获取最新版本并merge到本地
-
-
git pull origin master
-
-
-
上述命令其实相当于git fetch 和 git merge
-
在实际使用中,git fetch更安全一些
-
因为在merge前,我们可以查看更新情况,然后再决定是否合并
~~~~~~~~~~~~~~~~~~转载结束~~~~~~~~~~~~~~~~~~~~
在含有工作副本的git仓库中可以使用git pull,在获取远程数据后,本地跟踪分支就可以快进,并和远程分支指向的commit点保持一致在bare仓库中只能使用fetch从remote中抓取数据,但是本地已跟踪的分支可能并没有快进到和remote同步的commit点,出现例如:
- tekkaman_b2 pushes to tekkaman_b2 (local out of date)
如果需要本地分支快进,与remote同步,可使用以下命令:
- git fetch origin tekkaman_b2:tekkaman_b2
-
-
git fetch :
以上的命令可以解决本地跟踪分支与远程不同步的问题。
这个方法在非bare仓库中同样适用,但是在实际的使用中存在特殊情况:
在非bare仓库中fetch的特殊情况
- tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git branch
-
master
-
no_remote
-
* tekkaman_b2
-
tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git fetch origin
-
remote: Counting objects: 4, done.
-
remote: Compressing objects: 100% (2/2), done.
-
remote: Total 3 (delta 1), reused 0 (delta 0)
-
Unpacking objects: 100% (3/3), done.
-
From /home/tekkaman/development/research/git/test1
-
e3030a6..ba86b38 tekkaman_b2 -> origin/tekkaman_b2
-
tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git remote show origin
-
* remote origin
-
Fetch URL: /home/tekkaman/development/research/git/test1
-
Push URL: /home/tekkaman/development/research/git/test1
-
HEAD branch: tekkaman_b2
-
Remote branches:
-
master tracked
-
no_remote tracked
-
tekkaman_b1 tracked
-
tekkaman_b2 tracked
-
Local branch configured for 'git pull':
-
tekkaman_b2 merges with remote tekkaman_b2
-
Local refs configured for 'git push':
-
master pushes to master (up to date)
-
no_remote pushes to no_remote (up to date)
-
tekkaman_b2 pushes to tekkaman_b2 (local out of date)
-
tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git fetch origin tekkaman_b2:tekkaman_b2
-
fatal: Refusing to fetch into current branch refs/heads/tekkaman_b2 of non-bare repository
-
fatal: The remote end hung up unexpectedly
-
tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git checkout master
-
Switched to branch 'master'
-
tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git fetch origin tekkaman_b2:tekkaman_b2
-
From /home/tekkaman/development/research/git/test1
-
e3030a6..ba86b38 tekkaman_b2 -> tekkaman_b2
-
tekkaman@tekkaman-desktop:~/development/research/git/test1_g.git$ git remote show origin
-
* remote origin
-
Fetch URL: /home/tekkaman/development/research/git/test1
-
Push URL: /home/tekkaman/development/research/git/test1
-
HEAD branch: tekkaman_b2
-
Remote branches:
-
master tracked
-
no_remote tracked
-
tekkaman_b1 tracked
-
tekkaman_b2 tracked
-
Local branch configured for 'git pull':
-
tekkaman_b2 merges with remote tekkaman_b2
-
Local refs configured for 'git push':
-
master pushes to master (up to date)
-
no_remote pushes to no_remote (up to date)
-
tekkaman_b2 pushes to tekkaman_b2 (up to date)
从上面的实验中可以看出:在非bare的git仓库中,如果你要同步的本地跟踪分支是当前分支,就会出现拒绝fetch的情况。也就是说不可以在非bare的git仓库中通过fetch快进你的当前分支与远程同步。个人和我同事的理解是:如果本地数据被修改过了,这种同步的fetch会更改本地的文件(相当于fetch后再checkout到某个commit点),可能影响当前未提交的的工作。
“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. ”
阅读(335) | 评论(0) | 转发(0) |