git系列文章目录

第五章 git远程库修改的拉取


前言

当团队协作的时候,远程程序员A将修改的代码推送到远程库中,这时A公司项目经理需要将代码拉取到自己的本地仓库时,就需要远程库修改的拉取操作。

在这里插入图片描述


一、pull操作是什么?

远程库修改的拉取 是基于远程库 的一种 拉取pull + 合并merge 操作,该操作是为了解决同步任务的。

在这里插入图片描述

二、操作步骤

1.先确认远程库的内容,确保远程库已经被修改了

项目经理登录远程库查看,确认远程库已经被修改了:

在这里插入图片描述

2.拉取操作

首先进行fetch操作,注意这里仅仅是将远程库的代码下载到本地,并没有和本地仓库代码合并。

代码如下(示例):


Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git remote -v
origin  https://gitee.com/gitee18261897435/git-resp.git (fetch)
origin  https://gitee.com/gitee18261897435/git-resp.git (push)

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ ll
total 1
-rw-r--r-- 1 Apple 197121 7 Jan 23 00:06 Demo.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git fetch origin master
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), 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), 329 bytes | 65.00 KiB/s, done.
From https://gitee.com/gitee18261897435/git-resp
 * branch            master     -> FETCH_HEAD
   9fd9953..bf506bf  master     -> origin/master

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ ll
total 1
-rw-r--r-- 1 Apple 197121 7 Jan 23 00:06 Demo.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$


在这里插入图片描述

然后,确认下载的远程库文件目录

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git checkout origin/master
Note: switching to 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at bf506bf 在另一台电脑clone后新建Demo2.txt修改后push到远程仓库

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp ((bf506bf...))
$ ll
total 2
-rw-r--r-- 1 Apple 197121 7 Jan 23 00:06 Demo.txt
-rw-r--r-- 1 Apple 197121 3 Jan 23 16:51 Demo2.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp ((bf506bf...))
$

最后,确认远程库推送的内容,通常项目经理要将整个项目进行审核,也就是查看每个文件和远程库是否正确(避免手残下载错了,然后又合并)。。。

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp ((bf506bf...))
$ cat Demo2.txt
bbb

3.合并操作(确认下载的内容都正确以后才能合并)

先从刚才查看下载查看的远程库切换到本地master分支


Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp ((bf506bf...))
$ git checkout master
Previous HEAD position was bf506bf 在另一台电脑clone后新建Demo2.txt修改后push到远程仓库
Switched to branch 'master'

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$


然后合并分支

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git merge origin/master
Updating 9fd9953..bf506bf
Fast-forward
 Demo2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 Demo2.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$

4.拉取合并快速操作(确认远程库的内容都正确以后才能合并)

git pull操作可以将拉取合并一步成功,但是需要仔细check。
在这里插入图片描述

小结:

git fetch + git merge这种情况一般是比较常用的,这种方式比较保险,慎重。
git pull这种操作一般用于不需要检测的情况,或者代码比较简单


总结

本文仅仅简单介绍了gie远程库修改的拉取+合并操作,提供了两种方式可供选择,无论选择哪种拉取合并,都是需要检查以后再操作,避免出错。
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐