git fetch - git merge - git pull 指令

这篇具有很好参考价值的文章主要介绍了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
$ 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 - GitHub - Gerrit,git fetch,git merge,git pull,指令

git fetch 命令,Git - GitHub - Gerrit,git fetch,git merge,git pull,指令

此时再次执行 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 命令,Git - GitHub - Gerrit,git fetch,git merge,git pull,指令

git fetch 指令执行前:
本地 HEAD & master 同远程 origin/HEAD & origin/master 保持一致。
git fetch 命令,Git - GitHub - Gerrit,git fetch,git merge,git pull,指令

git fetch 指令执行后:
远程 origin/HEAD & origin/master 比本地 HEAD & master 多一个 commit。
git fetch 命令,Git - GitHub - Gerrit,git fetch,git merge,git pull,指令

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) 方式进行。

git fetch 命令,Git - GitHub - Gerrit,git fetch,git merge,git pull,指令

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

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文章来源地址https://www.toymoban.com/news/detail-752135.html

到了这里,关于git fetch - git merge - git pull 指令的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • git pull指令报错:error: You have not concluded your merge (MERGE_HEAD exists).

    使用git pull 指令时报错:error: You have not concluded your merge (MERGE_HEAD exists). 直接翻译上面的报错信息可知: 错误:您尚未结束合并(merge_HEAD存在)。 提示:请在合并之前提交您的更改。 致命:由于未完成合并而退出。 通过上面的信息多少知道了自己错误的根源。 首先我本地

    2024年02月03日
    浏览(35)
  • 【Git】Git 操作命令可视化(五):git clone、git fetch、git pull、git push、git pull --rebase、解决远程仓库与本地仓库的代码冲突

    1. git clone main是本地的main分支,o(origin)/main是表示本地拉去下来的远程的main分支 o/main分支记录了远程仓库拉取时的分支状态 远程分支有一个特别的属性,在你切换到远程分支时,git会自动进入分离 HEAD 状态(这样做是因为git不想让你在本地就能直接进行修改远程仓库代码的

    2024年02月08日
    浏览(51)
  • git 笔记/常见命令/as的fetch,pull ,update project的区别/标签管理

    头(HEAD)HEAD类似一个“指针”,指向当前活动 分支 的 最新版本。  就是在电脑里能看到的项目代码库目录,是我们搬砖的地方, 在这里我们可以新增文件、修改文件内容,或删除文件。 此时的项目代码库目录还是红色的(没有与git关联),未被git跟踪 此时的工作区的代码

    2024年02月06日
    浏览(37)
  • 【git 使用】git pull 和 git fetch 的区别

    恕我直言,我一直都用 git pull 从来没有用过 git fetch git fetch , git pull 都可以 用于获取远程仓库的内容,但它们有不同的作用和用法。 git fetch 用途 : git fetch 用于从远程仓库获取最新的提交,但不会自动合并或更新本地分支。 操作方式 :它会将远程仓库中的提交下载到本地

    2024年02月22日
    浏览(39)
  • git merge 和 git pull的区别是什么

    git merge 和 git pull 都是 Git 中用于合并代码的命令,但它们的使用场景和功能略有不同。 git merge 是用于合并一个或多个分支的命令。你可以将其他分支的代码合并到当前分支中。通常用于合并开发分支或修复分支到主分支上。 具体步骤: 首先,切换到你要合并到的目标分支

    2024年01月19日
    浏览(33)
  • git pull 总提示让输入merge 信息

    问题描述 :在生产环境拉代码的时候,总是出现 .git/MERGE_MSG,很烦。 虽然每次可以通过输入 :q 命令,取消,然后完成拉取。但是这样就很影响效率。 解决方法 : 方法一 : 暂时屏蔽错误法 我们可以通过以下命令进行拉取代码,屏蔽提示消息 git pull --no-edit origin master 这样

    2024年02月07日
    浏览(33)
  • git pull时自动产生 Merge branch

    如下图,在提交代码时,在自己的提交前面多出了一笔merge提交,这笔提交的内容实际上是在本地仓库进行git pull时,本地仓库更新的远程仓库中代码的内容 本地分支与远程分支存在分叉 分叉就是你在本地仓库做项目的时候,将自己的修改的代码Commit到本地了,而同时你的其

    2024年02月16日
    浏览(36)
  • git进阶(撤销pull、撤销merge、撤销add)

    git reflog reset到某个版本 git reset --hard 91ae6ad 如果merge了其他分支代码造成了大量的冲突 想撤销本次merge如果操作 1、使用上面的reset命令撤销到上一个提交版本 2、执行以下命令直接撤销了本地合并 git merge --abort 查看每次提交的文件修改列表,和分支变化的图形信息 git log --n

    2023年04月09日
    浏览(30)
  • SSH连接下Git fetch/pull/push 速度太慢的一些解决办法

    有时候某些git的代码库在fetch远端的时候会非常慢,速度只有几十k或者几k。这个速度拉取一整个代码库可能要花费一晚上或者一天时间。甚至在每天更新已有代码库时候也会花上十分钟。可以尝试一下这些方法来提升拉取远端代码的速度: 1. Git 版本过低的话可能会导致这个

    2024年02月10日
    浏览(41)
  • git pull无效,显示 * branch master -> FETCH_HEADAlready up to date. pull无效解决方法

    本地文件夹中删除文件后 ,git pull无效。显示如下: 命令如下: 比如错删a.txt: 一个命令恢复全部文件: 参考文章如下: 【Git 教程系列第 22 篇】删除本地文件后,使用 git pull 命令从远程仓库无法拉取到被删除文件的解决方案_git 删除本地文件再pull没有_Allen Su的博客-CSDN博客

    2024年02月15日
    浏览(49)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包