git fetch 的簡單用法:更新遠程代碼到本地倉庫

Git中從遠程的分支獲取最新的版本到本地方式以下,
如何更新下載到代碼到本地,請參閱ice的博客基於Github參與eoe的開源項目指南
方式一
1. 查看遠程倉庫html

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 ,從遠程獲取最新版本到本地android

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. 比較本地的倉庫和遠程參考的區別git

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

由於個人本地倉庫和遠程倉庫代碼相同因此沒有其餘任何信息
4. 把遠程下載下來的代碼合併到本地倉庫,遠程的和本地的合併github

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. 從遠程獲取最新版本到本地app

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分支下載到本地並新建一個分支tempfetch

  1. 比較本地的倉庫和遠程參考的區別
1
2
$ git diff temp su@SUCHANGLI /e/eoe_client/android-app (master) 

命令的意思是:比較master分支和temp分支的不一樣
因爲個人沒有區別就沒有顯示其餘信息
4. 合併temp分支到master分支spa

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

因爲沒有區別,因此顯示Already up-to-date.
合併的時候可能會出現衝突,有時間了再把如何處理衝突寫一篇博客補充上。
5.若是不想要temp分支了,能夠刪除此分支code

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

若是該分支沒有合併到主分支會報錯,能夠用如下命令強制刪除git branch -D <分支名>htm

總結:方式二更好理解,更安全,對於pull也能夠更新代碼到本地,至關於fetch+merge,多人寫做的話不夠安全。
若有錯誤請指正

 

轉自:

原文做者: com360

原文地址: http://my.eoe.cn/com360/archive/3533.html

相關文章
相關標籤/搜索