Git更新操作


在本文章教學中,我們將演示如何檢視 Git 儲存庫的檔案和提交檔案記錄,並對儲存庫中的檔案作修改和提交。

注意:在開始學習本教學之前,先克隆一個儲存庫,有關如何克隆儲存庫,請參考: /6/67/2091.html

執行克隆操作,並得到了一個新的檔案:main.py。想知道誰將這個檔案修改變提交到儲存庫中,那麼可以執行git log命令,為了更好的演示,開發人員minsu另外一台機(Ubuntu Linux)上,使用以下命令克隆 -

yiibai@ubuntu:~$ cd /home/yiibai/git
yiibai@ubuntu:~/git$ git clone http://git.oschina.net/yiibai/sample.git
Cloning into 'sample'...
remote: Counting objects: 15, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 15 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (15/15), done.
Checking connectivity... done.
yiibai@ubuntu:~/git$

克隆操作將在當前工作目錄中建立一個新的目錄(sample)。 將目錄更改(cd /home/yiibai/git/sample)為新建立的目錄,並執行git log命令。

yiibai@ubuntu:~/git$ cd /home/yiibai/git/sample
yiibai@ubuntu:~/git/sample$ git log
commit 51de0f02eb48ed6b84a732512f230028d866b1ea
Author: your_name <[email protected]>
Date:   Fri Jul 7 23:04:16 2017 +0800

    add the sum of a & b

commit be24e214620fa072efa877e1967571731c465884
Author: your_name <[email protected]>
Date:   Fri Jul 7 18:58:16 2017 +0800

    ??????mark

commit 5eccf92e28eae94ec5fce7c687f6f92bf32a6a8d
Author: your_name <[email protected]>
Date:   Fri Jul 7 18:52:06 2017 +0800

    this is main.py file commit mark use -m option

commit 6e5f31067466795c522b01692871f202c26ff948
Author: your_name <[email protected]>
Date:   Fri Jul 7 18:42:43 2017 +0800

    this is main.py file commit mark without use "-m" option

commit 290342c270bc90f861ccc3d83afa920169e3b07e
Author: Maxsu <[email protected]>
Date:   Fri Jul 7 16:55:12 2017 +0800

    Initial commit
yiibai@ubuntu:~/git/sample$

觀察紀錄檔後,可以看到maxsu新增了檔案程式碼來實現兩個變數ab之和,現在假設minsu在文字編輯器中開啟main.py,並修改優化程式碼。在示兩個變數ab之和,定義一個函式來實現兩個變數之和。修改後,程式碼如下所示:

#!/usr/bin/python3
#coding=utf-8

a = 10
b = 20

def sum(a, b):
    return (a+b)

c = sum(a, b)
print("The value of c is  ", c)

使用 git diff 命令檢視更改,如下所示 -

yiibai@ubuntu:~/git/sample$ git diff
diff --git a/main.py b/main.py
index 25eb22b..e84460d 100644
--- a/main.py
+++ b/main.py
@@ -7,5 +7,9 @@ print ("Life is short, you need Python !")
 a = 10

 b = 20
-c = a + b
-print("The value of c is  ", c)
\ No newline at end of file
+
+def sum(a, b):
+    return (a+b)
+
+c = sum(a, b)
+print("The value of c is  ", c)
yiibai@ubuntu:~/git/sample$

經過測試,程式碼沒有問題,提交了上面的更改。

yiibai@ubuntu:~/git/sample$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   main.py

no changes added to commit (use "git add" and/or "git commit -a")

yiibai@ubuntu:~/git/sample$ git add main.py
yiibai@ubuntu:~/git/sample$ git commit -m "add a new function sum(a,b)"
[master 01c5462] add a new function sum(a,b)
 1 file changed, 6 insertions(+), 2 deletions(-)
yiibai@ubuntu:~/git/sample$

再次檢視提交記錄資訊 -

yiibai@ubuntu:~/git/sample$ git log
commit 01c54624879782e4657dd6c166ce8818f19e8251
Author: minsu <[email protected]>
Date:   Sun Jul 9 19:01:00 2017 -0700

    add a new function sum(a,b)

commit 51de0f02eb48ed6b84a732512f230028d866b1ea
Author: your_name <[email protected]>
Date:   Fri Jul 7 23:04:16 2017 +0800

    add the sum of a & b
....

經過上面新增和提交修改後,其它開發人員並不能看到程式碼中定義的 sum(a, b) 函式,還需要將這裡提交的原生代碼推播到遠端儲存庫。使用以下命令 -

yiibai@ubuntu:~/git/sample$ git push origin master
Username for 'http://git.oschina.net': [email protected]
Password for 'http://[email protected]@git.oschina.net':<你的帳號的密碼>
Counting objects: 3, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 419 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://git.oschina.net/yiibai/sample.git
   51de0f0..01c5462  master -> master
yiibai@ubuntu:~/git/sample$

現在,程式碼已經成功提交到遠端儲存庫中了,只要其它開發人員使用 git clonegit pull 就可以得到這些新提交的程式碼了。

下面我們將學習其它一些更為常用的 git 命令,經過下面的學習,你就可以使用這些基本的git操作命令來檔案和程式碼管理和協同開發了。

新增新函式

在開發人員B(minsu)修改檔案main.py中的程式碼的同時,開發人員A(maxsu)在檔案main.py中實現兩個變數的乘積函式。 修改後,main.py檔案如下所示:

#!/usr/bin/python3
#coding=utf-8

print ("Life is short, you need Python !")


a = 10

b = 20
c = a + b
print("The value of c is  ", c)

def mul(a, b):
    return (a * b)

現在修改完程式碼,執行以下命令 -

$ git diff

得到如下結果 -

現在新增並提交上面的程式碼 -

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   main.py

no changes added to commit (use "git add" and/or "git commit -a")

$ git add main.py

Administrator@MY-PC /D/worksp/sample (master)
$ git commit -m "add a mul(a, b) function"
[master e5f8dfa] add a mul(a, b) function
 1 file changed, 4 insertions(+), 1 deletion(-)

現在檢視上提交的紀錄檔資訊,如下結果 -

$ git log
commit e5f8dfa9e7e89fea8813ab107e14b9b7412df2ae
Author: your_name <[email protected]>
Date:   Sun Jul 9 23:06:32 2017 +0800

    add a mul(a, b) function

commit 51de0f02eb48ed6b84a732512f230028d866b1ea
Author: your_name <[email protected]>
Date:   Fri Jul 7 23:04:16 2017 +0800

    add the sum of a & b
... ...

好了,現在要將上面的程式碼推播到遠端儲存庫,使用以下命令 -

$ git push origin master

執行上面命令,結果如下 -

$ git push origin master
Username for 'http://git.oschina.net': [email protected]
Password for 'http://[email protected]@git.oschina.net':
To http://git.oschina.net/yiibai/sample.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'http://git.oschina.net/yiibai/sample.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

但Git推播失敗。因為Git確定遠端儲存庫和本地儲存庫不同步。為什麼呢?因為在向檔案main.py新增以下程式碼片段時 -


def mul(a, b):
    return (a * b)

另外一個開發人員B已經向遠端儲存庫推播修改的程式碼,所以這裡在向遠端儲存庫推播程式碼時,發現上面的新的推播程式碼,現在這個要推播的程式碼與遠端儲存庫中的程式碼不一致,如果強行推播上去,Git不知道應該以誰的為準了。

所以,必須先更新本地儲存庫,只有在經過此步驟之後,才能推播自己的改變。

獲取最新更改

執行git pull命令以將其本地儲存庫與遠端儲存庫同步。

$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From http://git.oschina.net/yiibai/sample
   51de0f0..01c5462  master     -> origin/master
Auto-merging main.py
CONFLICT (content): Merge conflict in main.py
Automatic merge failed; fix conflicts and then commit the result.

現在開啟 main.py 內容如下 -

#!/usr/bin/python3
#coding=utf-8

print ("Life is short, you need Python !")


a = 10

b = 20
<<<<<<< HEAD
c = a + b
print("The value of c is  ", c)

def mul(a, b):
    return (a * b)
=======

def sum(a, b):
    return (a+b)

c = sum(a, b)
print("The value of c is  ", c)
>>>>>>> 01c54624879782e4657dd6c166ce8818f19e8251

拉取操作後,檢查紀錄檔訊息,並行現其它開發人員的提交的詳細資訊,提交ID為:01c54624879782e4657dd6c166ce8818f19e8251

開啟 main.py 檔案,修改其中的程式碼,修改完成後保檔案,檔案的程式碼如下所示 -

#!/usr/bin/python3
#coding=utf-8

print ("Life is short, you need Python !")

a = 10
b = 20

c = a + b
print("The value of c is  ", c)

def mul(a, b):
    return (a * b)

def sum(a, b):
    return (a+b)

c = sum(a, b)
print("The value of c is  ", c)

再次新增提交,最後推播,如下命令 -

$ git add main.py

Administrator@MY-PC /D/worksp/sample (master|MERGING)
$ git commit -m "synchronized with the remote repository "
[master ef07ab5] synchronized with the remote repository

Administrator@MY-PC /D/worksp/sample (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean

Administrator@MY-PC /D/worksp/sample (master)
$ git push origin master
Username for 'http://git.oschina.net': [email protected]
Password for 'http://[email protected]@git.oschina.net':
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 657 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
To http://git.oschina.net/yiibai/sample.git
   01c5462..ef07ab5  master -> master

現在,一個新的程式碼又提交並推播到遠端儲存庫中了。