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 -r
  origin/HEAD -> origin/master
  origin/master
  origin/tjluyao-master
  origin/yolov3
yongqiang@yongqiang:~/yongqiang_work/darknet$
yongqiang@yongqiang:~/yongqiang_work/darknet$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/tjluyao-master
  remotes/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-git
   85e848b..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-forward
 README.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_project
   dceebe9..d2751f3  master     -> origin/master
Updating dceebe9..d2751f3
Fast-forward
 above_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

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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值