git fetch 的簡單用法:更新遠程代碼到本地倉庫及衝突處理

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

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

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

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

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

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

個人本地參考代碼和遠程代碼相同,因此是Already up-to-dategithub

以上的方式有點很差理解,你們可使用下面的方式,而且很安全
方式二
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. 比較本地的倉庫和遠程參考的區別
1
2
$ git diff temp su@SUCHANGLI /e/eoe_client/android-app (master) 

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

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

因爲沒有區別,因此顯示Already up-to-date.
若是系統中有一些配置文件在服務器上作了配置修改,而後後續開發又新添加一些配置項的時候, 在發佈這個配置文件的時候,會發生代碼衝突: fetch

error: Your local changes to the following files would be overwritten by merge: spa

    protected/config/main.php

    Please, commit your changes or stash them before you can merge.

若是但願保留生產服務器上所作的改動,僅僅併入新配置項, 處理方法以下:

  git stash

  git pull

  git stash pop

  而後可使用git diff -w +文件名 來確認代碼自動合併的狀況.

反過來,若是但願用代碼庫中的文件徹底覆蓋本地工做版本. 方法以下:

  git reset --hard

  git pull

  其中git reset是針對版本,

  若是想針對文件回退本地修改,使用 git checkout HEAD file/to/restore

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,多人寫做的話不夠安全。若有錯誤請指正

相關文章
相關標籤/搜索