当前位置:   article > 正文

git fetch - git merge - git pull 指令_git fetch merge

git fetch merge

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
  • 1
references,refs:引用
reference specification,refspec:具体的引用
origin:远程仓库链接标记名,远程仓库链接别名
  • 1
  • 2
  • 3

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
  • 1

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
  • 1

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
  • 1
  • 2

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 <远程仓库链接标记名>
  • 1
$ git fetch
$ git fetch origin
  • 1
  • 2

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

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

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$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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

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

$ git checkout -b yongqiang origin/master
  • 1

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

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

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

$ git fetch origin master:tmp
$ git diff tmp 
$ git merge tmp
  • 1
  • 2
  • 3

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$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

此时执行 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

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
  • 1

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. 突然地,扑通一声
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • Merge branches fixes and enhancements on top of the current branch, making an octopus merge (在当前分支之上合并分支“fixes”和“enhancements”,进行章鱼合并)
$ git merge fixes enhancements
  • 1
  • Merge branch obsolete into the current branch, using ours merge strategy (使用 ours 的合并策略将 obsolete 分支合并到当前分支中)
$ git merge -s ours obsolete
  • 1
  • Merge branch maint into the current branch, but do not make a new commit automatically (将分支 maint 合并到当前分支,但不自动进行新提交)
$ git merge --no-commit maint
  • 1

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(+)
  • 1
  • 2
  • 3
  • 4
  • 5

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

在这里插入图片描述

3. git pull 指令

命令格式:

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

如果你有一个分支设置为跟踪一个远程分支,可以使用 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$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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

$ git pull
$ git pull origin
$ git pull origin master
  • 1
  • 2
  • 3

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

$ git pull origin qiang
等同于
$ git fetch origin
$ git merge origin/qiang
  • 1
  • 2
  • 3
  • 4

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

$ git pull origin master:develop
  • 1

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

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

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$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在实际使用中,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 <远程仓库链接标记名> <远程分支名>:<本地分支名>
  • 1
$ git pull --rebase
  • 1

在多人共同开发的时候,大家各自在自己的分支进行 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/458611
推荐阅读
相关标签
  

闽ICP备14008679号