美文网首页git操作指南
git 更新远程代码到本地仓库

git 更新远程代码到本地仓库

作者: yichen_china | 来源:发表于2019-02-16 14:09 被阅读18次

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

方式一 (远程代码直接下载合并到本地)

1. 查看远程仓库

$ 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 ,从远程获取最新版本到本地

$ 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. 比较本地的仓库和远程参考的区别

$ git log -p master.. origin/master
su@SUCHANGLI /e/eoe_client/android-app (master) 

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

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

$ git merge origin/master
Already up-to-date.
su@SUCHANGLI /e/eoe_client/android-app (master) 

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

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

方式二 (远程代码下载到本地新建分支;对比区别后在合并)

1.查看远程分支,和上面的第一步相同
2. 从远程获取最新版本到本地

$ 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. 比较本地的仓库和远程参考的区别
$ git diff temp
su@SUCHANGLI /e/eoe_client/android-app (master) 

命令的意思是:比较master分支和temp分支的不同
由于我的没有区别就没有显示其他信息

4. 合并temp分支到master分支

$ git merge temp
Already up-to-date.
su@SUCHANGLI /e/eoe_client/android-app (master) 

由于没有区别,所以显示Already up-to-date.
合并的时候可能会出现冲突,有时间了再把如何处理冲突写一篇博客补充上。

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

$ git branch -d temp
Deleted branch temp (was d6d48cc).
su@SUCHANGLI /e/eoe_client/android-app (master) 

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

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

其实生活很美好,指示你想的太多了。没有,不会,有差距很正常,因为我不会

相关文章

  • git 命令行操作笔记

    git中的选项解释 创建本地git仓库 提交代码到git仓库 本地git仓库添加到远程仓库中 克隆远程仓库到本地 ...

  • git 更新远程代码到本地仓库

    git fetch 的简单用法:更新远程代码到本地仓库 方式一 (远程代码直接下载合并到本地) 1. 查看远程仓库...

  • git常用指令

    下载远程仓库代码 git clone 代码仓库地址 从远程仓库拉取代码 git pull 提交代码到本地分支,并推...

  • git本地仓库关联远程仓库的两种方式

    git本地仓库关联远程仓库的两种方式: 1.将远程的代码clone到本地仓库 2.将本地的代码关联到远程仓库 第1...

  • knowledge point

    elementUI默认样式修改不成功的问题 jQuery分页 Git更新远程仓库代码到本地 git fetch v...

  • 常用命令行总结

    Git : // 初始化git git init // 从远程仓库下载代码到本地 git clone https:...

  • 常用命令行总结

    Git : // 初始化git git init // 从远程仓库下载代码到本地 git clone https:...

  • git仓库迁移和更新远程仓库地址

    git仓库迁移和更新远程仓库地址 一、git仓库迁移 1,从原仓库clone或pull到本地仓库 git clon...

  • Git 简单的使用(存档,方便自己查阅)

    一: 从远程仓库下载项目到本地,这里的url就是仓库克隆地址 二:提交更新 三:Git远程仓库地址变更本地修改 四...

  • cocoapod添加仓库

    1、提交代码到github 1)在github添加远程仓库2)将本地代码提交到远程仓库git remote add...

网友评论

    本文标题:git 更新远程代码到本地仓库

    本文链接:https://www.haomeiwen.com/subject/ccwbeqtx.html