git fetch - git merge - git pull 指令

Incorporates changes from a remote repository into the current branch. In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.
在默认模式下,git pull 命令是 git fetchgit merge FETCH_HEAD 命令的组合,git pull = git fetch + git merge FETCH_HEAD,将远程存储库中的更改合并到当前分支中。pull 指令其实就是去 a remote repository 抓东西下来 (fetch),并且更新 current branch 的进度 (merge)。

1 git fetch 指令

yongqiang@yongqiang:~$ git fetch --help
references,refs:引用
reference specification,refspec:具体的引用
origin:远程仓库链接标记名,远程仓库链接别名

Download objects and refs from another repository.
从另一个存储库下载对象和引用。

Fetch branches and/or tags (collectively, refs) from one or more other repositories, along with the objects necessary to complete their histories. Remote-tracking branches are updated.
从一个或多个其它存储库中获取分支和/或标签 (统称为 refs),以及使其历史完整所需的对象。远程跟踪分支已更新,将这些更新取回本地,就要用到 git fetch 指令。

git fetch can fetch from either a single named repository or URL, or from several repositories at once if <group> is given and there is a remotes.<group> entry in the configuration file.
git fetch 可以从单个命名存储库或 URL 获取,或者如果给定 <group> 并且配置文件中有 remotes.<group> 条目,则可以同时从多个存储库获取。

  1. Update the remote-tracking branches (更新远程跟踪分支)
$ git fetch origin

The above command copies all branches from the remote refs/heads/ namespace and stores them to the local refs/remotes/origin/ namespace, unless the branch.<name>.fetch option is used to specify a non-default refspec.
上述命令从远程 refs/heads/ namespace 复制所有分支,并将它们存储到本地的 refs/remotes/origin/ namespace 中,除非使用 branch.<name>.fetch 选项来指定非默认的 refspec

  1. Using refspecs explicitly (明确使用 refspec)
$ git fetch origin +pu:pu maint:tmp

This updates (or creates, as necessary) branches pu and tmp in the local repository by fetching from the branches (respectively) pu and maint from the remote repository.
通过从远程存储库的分支 (分别) 获取 pu and maint 来更新 (或根据需要创建) 本地存储库中的分支 pu and tmp

The pu branch will be updated even if it does not fast-forward, because it is prefixed with a plus sign; tmp will not be.
pu 分支即使不 fast-forward (快进) 合并也会被更新,因为它有一个加号前缀,tmp 不会。

  1. Peek at a remote’s branch, without configuring the remote in your local repository (查看远程分支,无需在本地存储库中配置远程)
$ git fetch git://git.kernel.org/pub/scm/git/git.git maint
$ git log FETCH_HEAD

The first command fetches the maint branch from the repository at git://git.kernel.org/pub/scm/git/git.git and the second command uses FETCH_HEAD to examine the branch with git log. The fetched objects will eventually be removed by git’s built-in housekeeping.
第一个命令从位于 git://git.kernel.org/pub/scm/git/git.git 的存储库中获取 maint 分支,第二个命令使用 FETCH_HEAD 通过 git log 检查分支,获取的对象最终将被 git 的内置管家删除。

  1. 命令格式 1
git fetch <远程仓库链接标记名>
$ git fetch
$ git fetch origin

将远程仓库的更新,全部取回本地。默认情况下,git fetch 取回所有分支的更新。

  1. 命令格式 2
git fetch <远程仓库链接标记名> <远程分支名>
$ git fetch origin master

git branch -r 查看远程分支,git branch -a 查看所有分支,本地的当前分支是 master,远程分支是 origin/master。

yongqiang@yongqiang:~/yongqiang_work/darknet$ git status
On branch master
Your branch is up to date with 'origin/master'.nothing to commit, working tree clean
yongqiang@yongqiang:~/yongqiang_work/darknet$
yongqiang@yongqiang:~/yongqiang_work/darknet$ git branch -rorigin/HEAD -> origin/masterorigin/masterorigin/tjluyao-masterorigin/yolov3
yongqiang@yongqiang:~/yongqiang_work/darknet$
yongqiang@yongqiang:~/yongqiang_work/darknet$ git branch -a
* masterremotes/origin/HEAD -> origin/masterremotes/origin/masterremotes/origin/tjluyao-masterremotes/origin/yolov3
yongqiang@yongqiang:~/yongqiang_work/darknet$

将远程 origin 的 master 分支代码拉取下来,在本地要用 “远程仓库链接标记名/分支名” 的形式读取。远程 origin 的 master 分支,在本地可以用 origin/master 读取。

取回远程更新以后,可以在它的基础上,使用 git checkout 命令创建一个新的分支。在 origin/master 的基础上,创建一个新分支 yongqiang。

$ git checkout -b yongqiang origin/master

使用 git merge 命令或者 git rebase 命令,在本地分支上合并远程分支 origin/master。

$ git merge origin/master
等同于
$ git rebase origin/master
$ git fetch origin master           # 从远程 origin 的 master 分支下载最新的版本到 origin/master 分支上
$ git log -p master..origin/master  # 比较本地的 master 分支和 origin/master 分支的差别
$ git merge origin/master           # 进行合并

上述过程可以用更清晰的方式来进行:

$ git fetch origin master:tmp
$ git diff tmp
$ git merge tmp

1.1 Example

yongqiang@yongqiang:~$ mkdir yongqiang_work
yongqiang@yongqiang:~$ cd yongqiang_work/
yongqiang@yongqiang:~/yongqiang_work$ git clone https://github.com/pjreddie/darknet.git
Cloning into 'darknet'...
remote: Enumerating objects: 5955, done.
remote: Total 5955 (delta 0), reused 0 (delta 0), pack-reused 5955
Receiving objects: 100% (5955/5955), 6.37 MiB | 1.34 MiB/s, done.
Resolving deltas: 100% (3932/3932), done.
yongqiang@yongqiang:~/yongqiang_work$ cd darknet/
yongqiang@yongqiang:~/yongqiang_work/darknet$ git status
On branch master
Your branch is up to date with 'origin/master'.nothing to commit, working tree clean
yongqiang@yongqiang:~/yongqiang_work/darknet$ git fetch
yongqiang@yongqiang:~/yongqiang_work/darknet$

此时执行 git fetch 指令没有任何信息,因为本地的进度跟线上的是一样的。

在 GitHub 网站上,直接线上编辑某个文件。在右上角会有个编辑的按钮,按下下方的 Commit changes 进行存档并新增一次 Commit,这样线上版本的 Commit 数就领先本机一次了。

此时再次执行 git fetch 指令:

$ git fetch
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:kaochenlong/practice-git85e848b..8c3a0a5  master     -> origin/master

git fetch 指令执行前:
本地 HEAD & master 同远程 origin/HEAD & origin/master 保持一致。

git fetch 指令执行后:
远程 origin/HEAD & origin/master 比本地 HEAD & master 多一个 commit。

2 git merge 指令

yongqiang@yongqiang:~$ git merge --help

Join two or more development histories together.
将两个或两个以上的开发历史合并一起。

Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch. This command is used by git pull to incorporate changes from another repository and can be used by hand to merge changes from one branch into another.
将 the named commits 的更改 (自从它们的历史与当前分支不同时起) 合并到当前分支中。git pull 使用此命令合并来自另一个存储库的更改,并且可以手动使用此命令将更改从一个分支合并到另一个分支。

octopus [ˈɒktəpəs]:n. 章鱼,章鱼肉,爪牙或分支机构众多的组织等
obsolete [ˈɒbsəliːt]:adj. 淘汰的,废弃的,过时的 n. 废词,被废弃的事物
sneak [sniːk]:v. 溜,偷偷地走,偷偷地做,偷带 adj. 突然的,出其不意的 n. 告状者
substantial [səb'stænʃ(ə)l]:adj. 大量的,价值巨大的,重大的,大而坚固的
refrain [rɪ'freɪn]:v. 避免,克制,节制 n. 副歌,经常重复的评价,迭歌,迭句
fix up ['fɪksʌp]:修理,解决,组织,安顿住处
abuse [əˈbjuːs]:v. 滥用,虐待,辱骂,伤害 n. 滥用,虐待,辱骂,妄用
bump [bʌmp]:v. 撞,碰上,颠簸行进 n. 凸块,隆起,碰撞,撞击 adv. 突然地,扑通一声
  • Merge branches fixes and enhancements on top of the current branch, making an octopus merge (在当前分支之上合并分支“fixes”和“enhancements”,进行章鱼合并)
$ git merge fixes enhancements
  • Merge branch obsolete into the current branch, using ours merge strategy (使用 ours 的合并策略将 obsolete 分支合并到当前分支中)
$ git merge -s ours obsolete
  • Merge branch maint into the current branch, but do not make a new commit automatically (将分支 maint 合并到当前分支,但不自动进行新提交)
$ git merge --no-commit maint

This can be used when you want to include further changes to the merge, or want to write your own merge commit message.
当您想要包含对合并的进一步更改,或者想要编写您自己的合并提交消息时,可以使用它。

You should refrain from abusing this option to sneak substantial changes into a merge commit. Small fixups like bumping release/version name would be acceptable.
你应该避免滥用此选项将大量更改出其不意的合并提交中。可以接受小的修复,例如修改发布/版本名称。

2.1 Example

为了使本地 HEAD & master 同远程 origin/HEAD & origin/master 保持一致,执行 git merge 指令:

$ git merge origin/master
Updating 85e848b..8c3a0a5
Fast-forwardREADME.md | 2 ++1 file changed, 2 insertions(+)

因为 origin/master 分支跟 master 分支本是同根生,所以在上面合并的过程可以看到是使用快转模式 (Fast Forward) 方式进行。

3 git pull 指令

命令格式:

git pull <远程仓库链接标记名> <远程分支名>:<本地分支名>
yongqiang@yongqiang:~$ git pull --help

如果你有一个分支设置为跟踪一个远程分支,可以使用 git pull 命令来自动的抓取然后合并远程分支到当前分支。默认情况下,git clone 命令会自动设置本地 master 分支跟踪克隆的远程仓库的 master 分支,即本地的 master 分支自动追踪 origin/master 分支。运行 git pull 通常会从最初克隆的服务器上抓取数据并自动尝试合并到当前所在的分支。

yongqiang@yongqiang:~/yongqiang_work/darknet$ git status
On branch master
Your branch is up to date with 'origin/master'.nothing to commit, working tree clean
yongqiang@yongqiang:~/yongqiang_work/darknet$
yongqiang@yongqiang:~/yongqiang_work/darknet$ git remote -v
origin  https://github.com/pjreddie/darknet.git (fetch)
origin  https://github.com/pjreddie/darknet.git (push)
yongqiang@yongqiang:~/yongqiang_work/darknet$
yongqiang@yongqiang:~/yongqiang_work/darknet$ git pull
Already up to date.
yongqiang@yongqiang:~/yongqiang_work/darknet$
yongqiang@yongqiang:~/yongqiang_work/darknet$ git pull origin
Already up to date.
yongqiang@yongqiang:~/yongqiang_work/darknet$
yongqiang@yongqiang:~/yongqiang_work/darknet$ git pull origin master
From https://github.com/pjreddie/darknet* branch            master     -> FETCH_HEAD
Already up to date.
yongqiang@yongqiang:~/yongqiang_work/darknet$

将远程 origin 的 master 分支代码拉取下来,与本地当前分支代码合并。如果当前分支与远程分支存在追踪关系,git pull 就可以省略远程分支名。

$ git pull
$ git pull origin
$ git pull origin master

将远程 origin 的 qiang 分支代码拉取下来,与本地当前分支代码合并:

$ git pull origin qiang
等同于
$ git fetch origin
$ git merge origin/qiang

将远程 origin 的 master 分支代码拉取下来,与本地 develop 分支代码合并:

$ git pull origin master:develop

本地的 master 分支追踪远程 origin/qiang 分支:

$ git branch --set-upstream master origin/next

git fetch 命令从服务器上抓取本地没有的数据时,它并不会修改工作目录中的内容。它只会获取数据然后让你自己合并。git pull 在大多数情况下的含义是一个 git fetch 紧接着一个 git merge 命令。如果有一个设置好的跟踪分支,不管它是显式地设置还是通过 git clonegit checkout 命令为你创建的,git pull 都会查找当前分支所跟踪的服务器与分支,从服务器上抓取数据然后尝试合并入那个远程分支。

strong@foreverstrong:~/project_work/airport_project$ git pull
Username for 'https://github.com': yqcheng@deepnorth.cn
Password for 'https://yqcheng@deepnorth.cn@github.com':
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 25 (delta 16), reused 25 (delta 16), pack-reused 0
Unpacking objects: 100% (25/25), done.
From https://github.com/DeepNorthAI/airport_projectdceebe9..d2751f3  master     -> origin/master
Updating dceebe9..d2751f3
Fast-forwardabove_the_wing/configs/vest_classify_cfgs.py       |  5 ++above_the_wing/examples/action_recognition_demo.py | 57 ++++++++++++++++------.../examples/vest_classification_demo.py           | 15 ++++--above_the_wing/src/application_util/processing.py  | 12 +++++4 files changed, 70 insertions(+), 19 deletions(-)
strong@foreverstrong:~/project_work/airport_project$

在实际使用中,git fetch 更安全一些,因为在 merge 前,我们可以查看更新情况,然后再决定是否合并。

3.1 git pull --rebase

More precisely, git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch. With --rebase, it runs git rebase instead of git merge.
在执行 git pull 指令的时候,可以再加上 --rebase 参数,它在 fetch 完成之后,便会使用 rebase 方式进行合并。

命令格式:

git pull --rebase <远程仓库链接标记名> <远程分支名>:<本地分支名>
$ git pull --rebase

在多人共同开发的时候,大家各自在自己的分支进行 commit,所以拉回来用一般的方式合并的时候常会产生为了合并而产生的额外 commit。为了合并而产生的这个 commit 本身并没有什么问题,但如果你不想要这个额外的 commit,可考虑使用 rebase 方式来进行合并。

References

https://yongqiang.blog.csdn.net/
(日) 大塚弘记 著, 支鹏浩, 刘斌 译. GitHub 入门与实践[M]. 北京:人民邮电出版社, 2015. 1-255
https://gitbook.tw/
https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes

git fetch - git merge - git pull 指令相关推荐

  1. Git Rebase vs Merge, GIt Reset vs Revert

    Git merge vs Rebase 构造场景如下: https://zhuanlan.zhihu.com/p/29682134?utm_source=wechat_session Git Rest ...

  2. 详解git pull和git fetch的区别:

    前言 在我们使用git的时候用的更新代码是git fetch,git pull这两条指令.但是有没有小伙伴去思考过这两者的区别呢?有经验的人总是说最好用git fetch+git merge,不建议用 ...

  3. Git fetch和git pull的区别

    原文:http://www.tech126.com/git-fetch-pull/ Git中从远程的分支获取最新的版本到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本到本地 ...

  4. #39;git pull#39;和#39;git fetch#39;有什么区别?

    主持人注意:鉴于此问题已经发布了67个答案 (其中一些已删除),请在发布另一个问题之前考虑您是否正在贡献新内容 . git pull和git fetch什么区别? #1楼 我花了一点时间来了解有什么区 ...

  5. git fetch和git pull的区别_Git实战(实验楼)学习笔记 实验2 基本用法(下)

    一.实验介绍 本节实验为 Git 入门第二个实验,继续练习最常用的 Git 命令. 知识点 对比差异 分布式的工作流程 Git 标签 实验环境 实验环境为 Ubuntu Linux 命令行环境,需要了 ...

  6. Git / git clone 、git pull 和 git fetch

    一.git clone Git 的 clone 命令的工作如下: 1.自动将远程主机命名为 origin,拉取它的所有数据. 2.创建一个指向它的 master 分支的指针,并且在本地将其命名为 or ...

  7. git 拉取和获取 pull 和 fetch 区别

    使用Git  直接提交的话   直接 push 获取最新版本  有两种  拉取 和 获取 pull 和 fetch git  pull     从远程拉取最新版本 到本地  自动合并 merge   ...

  8. 玩转GIT系列之【git pull和git fetch的区别】

    大家都知道,git中从远程的分支获取最新的版本到本地有2个命令,git pull和git fetch.但是,这两个命令究竟有什么区别?使用时候该怎么选择呢?很多人不是很清楚,我自己也不是很清楚.今天就 ...

  9. 真正理解 git fetch, git pull 以及 FETCH_HEAD

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 真正理解 git fetch, git pull 要讲清楚git fetch,git pull,必须 ...

最新文章

  1. “OMP: Error #15: Initializing libiomp5.dylib, but found libomp.dylib already initialized“ error
  2. 协鑫集成“熄灯工厂”装上ET工业大脑,验证订单命中率可提高3.99%
  3. Angular 5.0 学习2:Angular 5.0 开发环境的搭建和新建第一个ng5项目
  4. hdu3527spy(STL,map)
  5. c语言 cstring “+”: 运算符不起任何作用;应输入带副作用的运算符_国家计算机二级考试C语言选择题高频考点汇总,干货满满...
  6. 存储:Cookie/localStorage
  7. 一个事物两个方面的对比举例_《写作教练在你家》:推开写作之门第3课——对比的魔法...
  8. .net Core 在 CentOS7下,报The type initializer for 'Gdip' threw an exception.异常
  9. WebService SOAPUI接口测试
  10. Word 之 清除页眉下划线
  11. flash编译器错误查询表
  12. 如何手动控制Mac的风扇
  13. 大数据24小时:腾讯云在美新增两个数据中心,依图科技与华为合作发布人工智能一体机
  14. SQL 语法查询手册
  15. 四天搞懂生成对抗网络(二)——风格迁移的“精神始祖”Conditional GAN
  16. 有 1、2、3、4 四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
  17. 阿里云服务器可以解析到国外的域名吗
  18. 杨毅-Solo多机多节点部署
  19. 鹰偶尔飞的比鸡低,但鸡永远也飞不了鹰那么高!
  20. how2heap glibc 2.27

热门文章

  1. 《夏洛特烦恼》观后感
  2. 去除重复字母Python解法
  3. ubuntu20.04 磁盘管理
  4. 微信小程序-更换头像
  5. php子孙树,递归 迭代 得到家谱树 子孙树
  6. 从头开始训练一个 NER 标注器
  7. Excel教程:数值为0不显示的三种解决方法介绍
  8. untiy打包发布WebGL
  9. Python小白学习第二天
  10. echarts饼图中间默认内容显示与data数据显示切换