git fetch - git merge - git pull 指令

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
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 指令

为了使本地 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': [email protected]
Password for 'https://[email protected]@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

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

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/129904051