Git更新遠端倉庫程式碼到本地 git fetch
阿新 • • 發佈:2018-12-29
當我們在多臺電腦上開發一個專案的時候,需要經常修改提交內容並在另一臺電腦上更新遠端最新的程式碼,今天看了一下如何從遠端程式碼倉庫獲取更新到本地,總結了一下網上的文章,採用如下的方式比較簡單。
檢視遠端分支
使用如下命令可以檢視遠端倉庫(我這裡有一個origin倉庫)
1 2 3 |
$
git remote - v
origin
[email protected]:username /Animations .git
(fetch)
origin
[email protected]:username /Animations .git
(push)
|
從遠端獲取最新版本到本地
使用如下命令可以在本地新建一個temp分支,並將遠端origin倉庫的master分支程式碼下載到本地temp分支
1 2 3 4 5 6 7 8 |
$
git fetch origin master:temp
remote:
Counting objects: 18, done .
remote:
Compressing objects: 100% (6 /6 ), done .
remote:
Total 11 (delta 3), reused 0 (delta 0)
Unpacking
objects: 100% (11 /11 ), done .
From
github.com:username /Animations
*
[new branch] master -> temp
c07bdc7..40f902d
master -> origin /master
|
比較本地倉庫與下載的temp分支
使用如下命令來比較原生代碼與剛剛從遠端下載下來的程式碼的區別:
1 2 3 4 5 6 7 8 9 10 11 |
$
git diff temp
diff
--git a/README.md b/README.md
deleted
file mode 100644
index
76699ed.. 0000000
---
a/README.md
+++
/dev/ null
@@
- 1 , 6 + 0 , 0 @@
-Animations
-==========
-
。。。
|
合併temp分支到本地的master分支
對比區別之後,如果覺得沒有問題,可以使用如下命令進行程式碼合併:
1 2 3 4 5 6 7 |
$
git merge temp
Updating
c07bdc7..40f902d
Fast-forward
README.md
| 6 ++++++
src /cn/exercise/animations/MainActivity .java
| 4 ++--
2
files changed, 8 insertions(+), 2 deletions(-)
create
mode 100644 README.md
|
刪除temp分支
如果temp分支不想要保留,可以使用如下命令刪除該分支:
1 2 |
$
git branch -d temp
Deleted
branch temp (was 40f902d).
|
如果該分支的程式碼之前沒有merge到本地,那麼刪除該分支會報錯,可以使用Git branch -D temp強制刪除該分支。
這樣,我們把遠端倉庫程式碼更新到本地了。不過,還有一些其他方式更新,後面找時間再研究一下。另外,git diff如果不借助一些GUI工具,看起來比較痛苦,下一次配置一個圖形化比較工具來體驗一下。