您好,登錄后才能下訂單哦!
本篇內容介紹了“Git如何更新”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
Tom 執行克隆操作并找到一個新文件 string.c。他想知道誰將此文件添加到存儲庫中以及出于什么目的,因此,他執行了git log命令。
[tom@CentOS ~]$ git clone gituser@git.server.com:project.git
上述命令將產生以下結果
Initialized empty Git repository in /home/tom/project/.git/ remote: Counting objects: 6, done. remote: Compressing objects: 100% (4/4), done. Receiving objects: 100% (6/6), 726 bytes, done. remote: Total 6 (delta 0), reused 0 (delta 0)
克隆操作將在當前工作目錄中創建一個新目錄。他將目錄更改為新創建的目錄并執行git log命令。
[tom@CentOS ~]$ cd project/ [tom@CentOS project]$ git log
上述命令將產生以下結果
commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <jerry@tutorialspoint.com>Date: Wed Sep 11 08:05:26 2013 +0530Changed return type of my_strlen to size_tcommit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 07:32:56 2013 +0530Initial commit
觀察日志后,他意識到文件string.c是Jerry添加的,用于實現基本的字符串操作。他對 Jerry 的代碼很好奇。于是他在文本編輯器中打開 string.c 并立即發現了一個錯誤。在 my_strlen 函數中,Jerry 沒有使用常量指針。因此,他決定修改 Jerry 的代碼。修改后,代碼如下
[tom@CentOS project]$ git diff
上述命令將產生以下結果
diff --git a/string.c b/string.c index 7da2992..32489eb 100644--- a/string.c +++ b/string.c @@ -1,8 +1,8 @@#include <stdio.h>-size_t my_strlen(char *s) +size_t my_strlen(const char *s) { - char *p = s; + const char *p = s; while (*p) ++p; }
經過測試,他提交了他的更改。
[tom@CentOS project]$ git status -s M string.c ?? string[tom@CentOS project]$ git add string.c [tom@CentOS project]$ git commit -m 'Changed char pointer to const char pointer'[master cea2c00] Changed char pointer to const char pointer1 files changed, 2 insertions(+), 2 deletions(-) [tom@CentOS project]$ git log
上述命令將產生以下結果
commit cea2c000f53ba99508c5959e3e12fff493b Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 08:32:07 2013 +0530Changed char pointer to const char pointercommit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <jerry@tutorialspoint.com>Date: Wed Sep 11 08:05:26 2013 +0530Changed return type of my_strlen to size_tcommit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 07:32:56 2013 +0530Initial commit
Tom 使用 git push 命令推送他的更改。
[tom@CentOS project]$ git push origin master
上述命令將產生以下結果
Counting objects: 5, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 336 bytes, done. Total 3 (delta 1), reused 0 (delta 0) To gituser@git.server.com:project.git d1e19d3..cea2c00 master ?> master
同時,Jerry 決定實現字符串比較功能。于是他修改了string.c。修改后,文件如下所示
[jerry@CentOS project]$ git diff
上述命令將產生以下結果
index 7da2992..bc864ed 100644--- a/string.c +++ b/string.c30Git Tutorials @@ -9,9 +9,20 @@ size_t my_strlen(char *s)return (p -s ); } +char *my_strcpy(char *t, char *s) + { + char *p = t; + + while (*t++ = *s++) + ; + + return p; + } +int main(void){ int i; + char p1[32]; char *s[] = { "Git tutorials", "Tutorials Point" @@ -20,5 +31,7 @@ int main(void) for (i = 0; i < 2; ++i) printf("string lenght of %s = %lu\n", s[i], my_strlen(s[i])); + printf("%s\n", my_strcpy(p1, "Hello, World !!!")); + return 0; } }
在測試之后,他準備好推動他的改變。
[jerry@CentOS project]$ git status -s M string.c ?? string[jerry@CentOS project]$ git add string.c [jerry@CentOS project]$ git commit -m "Added my_strcpy function"[master e944e5a] Added my_strcpy function1 files changed, 13 insertions(+), 0 deletions(-)
在推送操作之前,他通過查看日志消息來驗證提交。
[jerry@CentOS project]$ git log
上述命令將產生以下結果
commit e944e5aab74b26e7447d3281b225309e4e59efcd Author: Jerry Mouse <jerry@tutorialspoint.com>Date: Wed Sep 11 08:41:42 2013 +0530Added my_strcpy functioncommit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <jerry@tutorialspoint.com>Date: Wed Sep 11 08:05:26 2013 +0530Changed return type of my_strlen to size_tcommit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 07:32:56 2013 +0530Initial commit
Jerry 對這些變化很滿意,他想推動他的變化。
[jerry@CentOS project]$ git push origin master
上述命令將產生以下結果
To gituser@git.server.com:project.git ! [rejected] master ?> master (non-fast-forward)error: failed to push some refs to 'gituser@git.server.com:project.git'To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details.
但是 Git 不允許 Jerry 推送他的更改。因為 Git 發現遠程倉庫和 Jerry 的本地倉庫不同步。正因為如此,他可能會丟失項目的歷史記錄。為了避免這種混亂,Git 失敗了這個操作。現在,Jerry 必須先更新本地存儲庫,然后才能推送自己的更改。
Jerry 執行 git pull 命令以將他的本地存儲庫與遠程存儲庫同步。
[jerry@CentOS project]$ git pull
上述命令將產生以下結果
remote: Counting objects: 5, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From git.server.com:project d1e19d3..cea2c00 master ?> origin/master First, rewinding head to replay your work on top of it... Applying: Added my_strcpy function
拉操作后,Jerry 檢查日志消息并找到 Tom 提交 ID 為cea2c000f53ba99508c5959e3e12fff493ba6f69的提交的詳細信息
[jerry@CentOS project]$ git log
上述命令將產生以下結果
commit e86f0621c2a3f68190bba633a9fe6c57c94f8e4f Author: Jerry Mouse <jerry@tutorialspoint.com>Date: Wed Sep 11 08:41:42 2013 +0530Added my_strcpy functioncommit cea2c000f53ba99508c5959e3e12fff493ba6f69 Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 08:32:07 2013 +0530Changed char pointer to const char pointercommit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <jerry@tutorialspoint.com>Date: Wed Sep 11 08:05:26 2013 +0530Changed return type of my_strlen to size_tcommit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 07:32:56 2013 +0530Initial commit
現在,Jerry 的本地存儲庫與遠程存儲庫完全同步。所以他可以安全地推送他的更改。
[jerry@CentOS project]$ git push origin master
上述命令將產生以下結果
Counting objects: 5, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 455 bytes, done. Total 3 (delta 1), reused 0 (delta 0) To gituser@git.server.com:project.git cea2c00..e86f062 master ?> master
“Git如何更新”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。