fetch整个仓库 github_git fetch 的简单用法:更新远程代码到本地仓库

本文介绍了两种使用git fetch更新远程代码到本地仓库的方法。第一种直接将远程master分支的代码下载到本地master分支,然后进行合并。第二种是创建一个临时分支temp,将远程master分支的代码拉取到temp,比较并合并到master,最后删除temp分支。这种方式更加安全,适合多人协作的场景。
摘要由CSDN通过智能技术生成

方式一

1. 查看远程仓库

1

2

3

4

5

6

$ git remote -v

eoecn https://github.com/eoecn/android-app.git (fetch)

eoecn https://github.com/eoecn/android-app.git (push)

origin https://github.com/com360/android-app.git (fetch)

origin https://github.com/com360/android-app.git (push)

su@SUCHANGLI /e/eoe_client/android-app (master)

从上面的结果可以看出,远程仓库有两个,一个是eoecn,一个是origin

2 ,从远程获取最新版本到本地

1

2

3

4

$ git fetch origin master

From https://github.com/com360/android-app

* branch master -> FETCH_HEAD

su@SUCHANGLI /e/eoe_client/android-app (master)

$ git fetch origin master 这句的意思是:从远程的origin仓库的master分支下载代码到本地的origin master

3. 比较本地的仓库和远程参考的区别

1

2

$ git log -p master.. origin/master

su@SUCHANGLI /e/eoe_client/android-app (master)

因为我的本地仓库和远程仓库代码相同所以没有其他任何信息

4. 把远程下载下来的代码合并到本地仓库,远程的和本地的合并

1

2

3

$ git merge origin/master

Already up-to-date.

su@SUCHANGLI /e/eoe_client/android-app (master)

我的本地参考代码和远程代码相同,所以是Already up-to-date

以上的方式有点不好理解,大家可以使用下面的方式,并且很安全

方式二

1.查看远程分支,和上面的第一步相同

2. 从远程获取最新版本到本地

1

2

3

4

$ git fetch origin master:temp

From https://github.com/com360/android-app

* [new branch] master -> temp

su@SUCHANGLI /e/eoe_client/android-app (master)

git fetch origin master:temp 这句命令的意思是:从远程的origin仓库的master分支下载到本地并新建一个分支temp

比较本地的仓库和远程参考的区别

1

2

$ git diff temp

su@SUCHANGLI /e/eoe_client/android-app (master)

命令的意思是:比较master分支和temp分支的不同

由于我的没有区别就没有显示其他信息

4. 合并temp分支到master分支

1

2

3

$ git merge temp

Already up-to-date.

su@SUCHANGLI /e/eoe_client/android-app (master)

由于没有区别,所以显示Already up-to-date.

合并的时候可能会出现冲突,有时间了再把如何处理冲突写一篇博客补充上。

5.如果不想要temp分支了,可以删除此分支

1

2

3

$ git branch -d temp

Deleted branch temp (was d6d48cc).

su@SUCHANGLI /e/eoe_client/android-app (master)

如果该分支没有合并到主分支会报错,可以用以下命令强制删除git branch -D

总结:方式二更好理解,更安全,对于pull也可以更新代码到本地,相当于fetch+merge,多人写作的话不够安全。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值