Git 更新操作
修改現有函數
Tom 執行克隆操作後,看到新的文件string.c,他想知道這個文件到存儲庫?目的是什麼?於是,他執行 git 日誌命令。
[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日誌命令。
[tom@CentOS ~]$ cd project/ [tom@CentOS project]$ git log
上麵的命令會產生以下結果。
commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <jerry@gitbook.net> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <tom@gitbook.net> Date: Wed Sep 11 07:32:56 2013 +0530 Initial 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-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 yiibaier to const char yiibaier' [master cea2c00] Changed char yiibaier to const char yiibaier 1 files changed, 2 insertions(+), 2 deletions(-) [tom@CentOS project]$ git log
上麵的命令會產生以下結果。
commit cea2c000f53ba99508c5959e3e12fff493b Author: Tom Cat <tom@gitbook.net> Date: Wed Sep 11 08:32:07 2013 +0530 Changed char yiibaier to const char yiibaier commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <jerry@gitbook.net> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <tom@gitbook.net> Date: Wed Sep 11 07:32:56 2013 +0530 Initial 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.c 30Git 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 Yiibai" @@ -20,5 +31,7 @@ int main(void) for (i = 0; i < 2; ++i) printf("string lenght of %s = %lu ", s[i], my_strlen(s[i])); + printf("%s ", 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 function 1 files changed, 13 insertions(+), 0 deletions(-)
push操作之前,他驗證通過查看日誌信息提交。
[jerry@CentOS project]$ git log
上麵的命令會產生以下結果。
commit e944e5aab74b26e7447d3281b225309e4e59efcd Author: Jerry Mouse <jerry@gitbook.net> Date: Wed Sep 11 08:41:42 2013 +0530 Added my_strcpy function commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <jerry@gitbook.net> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <tom@gitbook.net> Date: Wed Sep 11 07:32:56 2013 +0530 Initial 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@gitbook.net> Date: Wed Sep 11 08:41:42 2013 +0530 Added my_strcpy function commit cea2c000f53ba99508c5959e3e12fff493ba6f69 Author: Tom Cat <tom@gitbook.net> Date: Wed Sep 11 08:32:07 2013 +0530 Changed char yiibaier to const char yiibaier commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <jerry@gitbook.net> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <tom@gitbook.net> Date: Wed Sep 11 07:32:56 2013 +0530 Initial 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