Git提交更改


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

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

在上一步中,我們已經修改了 main.py 檔案中的程式碼,在程式碼中定義了兩個變數並提交程式碼,但是要再次新增和修改main.py 檔案中的程式碼,實現新功能:求兩個變數相加值。修改提交的操作更改包含提交訊息的最後一個提交; 它建立一個新的提交ID。

在修改操作之前,檢查提交紀錄檔,如下命令所示 -

$ git log
commit d757c8e92ad6053db294100c77075865f829b7ac
Author: your_name <[email protected]>
Date:   Fri Jul 7 23:04:16 2017 +0800

    define two var 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

下面我們開啟檔案:main.py 加入以下兩行:

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

更正操作提交新的更改,並檢視提交紀錄檔。首先檢視狀態,如下命令 -

$ git status
On branch master
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

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 status
On branch master
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   main.py

提交更改的檔案,如下所示 -

$ git commit --amend -m "add the sum of a & b "
[master 51de0f0] add the sum of a & b
 1 file changed, 5 insertions(+), 1 deletion(-)

現在,使用 git log命令顯示將顯示新的提交訊息與新的提交ID(51de0f02eb48ed6b84a732512f230028d866b1ea),最近一次提交的放前面:

$ 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