git

git fetch 详解

2017-12-14  本文已影响0人  忆飞

git fetch从远程分支拉取代码。

fetch常结合merge一起用,git fetch + git merge == git pull
一般要用git fetch+git merge,因为git pull会将代码直接合并,造成冲突等无法知道,fetch代码下来要git diff orgin/xx来看一下差异然后再合并。

1. FETCH_HEAD概念

指定某个branch在服务器上最新状态。

$cat .git/FETCH_HEAD
01e8809a7861a55f7a403981f2f1bcd68603e33a branch 'dev' of https://github.com/Moonergfp/learngit
ff47932aba92e0eaec6c75ce8112d2f24d890dab not-for-merge
branch 'develop' of https://github.com/Moonergfp/learngit
ff47932aba92e0eaec6c75ce8112d2f24d890dab not-for-merge
branch 'master' of https://github.com/Moonergfp/learngit

第一列是版本号,第二列是当前FETCH_HEAD是否将要合并的,第三列是git版本库路径。
如上3行中,dev就是要默认指定merge的分支。直接git merge就可以merge把origin/dev到dev分支上。

ff47932aba92e0eaec6c75ce8112d2f24d890dab branch 'master' of https://github.com/Moonergfp/learngit
fb515ac7195a9cf210839a4b4941e1e31c55067d not-for-merge
branch 'dev' of https://github.com/Moonergfp/learngit
01e8809a7861a55f7a403981f2f1bcd68603e33a not-for-merge
branch 'dev2' of https://github.com/Moonergfp/learngit
ff47932aba92e0eaec6c75ce8112d2f24d890dab not-for-merge
branch 'develop' of https://github.com/Moonergfp/learngit

可以看到当前要合并的是master分支。

2. 用法

缺点:会拉取当前项目的所有分支的commit。这样没必要,如当前项目有很多人在参与,那么就会有很多分支,那么其他分支的提交也会拉取下来,你得等半天下下来,网速不好就蛋疼了。

$ git fetch
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From https://github.com/Moonergfp/learngit
7c5a386..b008b08 abc -> origin/abc
ff47932..6f28960 master -> origin/master

上一篇 下一篇

猜你喜欢

热点阅读