方式一
查看远程仓库
$ 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
比较本地的仓库和远程参考的区别$ 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
git fetch origin master:temp这句命令的意思是:从远程的origin仓库的master分支下载到本地并新建一个分支temp
$ git diff temp
命令的意思是:比较master分支(个人理解为“本地master分支”)和temp分支的不同由于我的没有区别就没有显示其他信息
4. 合并temp分支到master分支(个人理解为“本地master分支”)
$ git merge temp
$ git branch -D temp
如果该分支没有合并到主分支会报错,可以用以下命令强制删除git branch -D <分支名>
总结:方式二更好理解,更安全,对于pull也可以更新代码到本地,相当于fetch+merge,多人写作的话不够安全
本地有修改如何强制更新
傻傻地办法就是用心的目录重新clone一个,正确的做法是什么?
正确的做法应该是:
git fetch --all git reset --hard origin/master
git fetch 只是下载远程的库的内容,不做任何的合并git reset 把HEAD指向刚刚下载的最新的版本