Git 更新操作

來源:互聯網
上載者:User

標籤:iat   diff   initial   writing   poi   key   ++i   mouse   sep   

修改現有函數

Tom 執行複製操作後,看到新的檔案string.c,他想知道這個檔案到存放庫?目的是什嗎?於是,他執行 git 日誌命令。

[[email protected] ~]$ git clone [email protected]: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日誌命令。

[[email protected] ~]$ cd project/ [[email protected] project]$ git log

上面的命令會產生以下結果。

commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <[email protected]> Date: Wed Sep 11 07:32:56 2013 +0530 Initial commit

查看記錄後,他意識到,string.c 檔案加入Jerry 實現基底字元串操作。他是好奇Jerry 的代碼。於是他開啟文本編輯string.c 檔案,並立即找到了一個漏洞。 my_strlen函數中 Jerry 沒有使用常量指標。於是,他決定修改Jerry 的代碼。修改後的代碼會這個樣子。

[[email protected] 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;

經過測試,他提交他的變化。

[[email protected] project]$ git status -s M string.c ?? string [[email protected] project]$ git add string.c [[email protected] project]$ git commit -m ‘Changed char pointer to const char pointer’ [master cea2c00] Changed char pointer to const char pointer 1 files changed, 2 insertions(+), 2 deletions(-) [[email protected] project]$ git log

上面的命令會產生以下結果。

commit cea2c000f53ba99508c5959e3e12fff493b Author: Tom Cat <[email protected]> Date: Wed Sep 11 08:32:07 2013 +0530 Changed char pointer to const char pointer commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <[email protected]> Date: Wed Sep 11 07:32:56 2013 +0530 Initial commit

Tom 使用git push命令,推他的變化。

[[email protected] 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 [email protected]:project.git d1e19d3..cea2c00 master −> master

添加新函數

與此同時,Jerry 決定實現字串比較函數。於是,他修改string.c檔案。修改後的檔案會這個樣子。

[[email protected] 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 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; }

經過測試,他準備推他的變化。

[[email protected] project]$ git status -s M string.c ?? string [[email protected] project]$ git add string.c [[email protected] project]$ git commit -m “Added my_strcpy function” [master e944e5a] Added my_strcpy function 1 files changed, 13 insertions(+), 0 deletions(-)

push操作之前,他驗證通過查看日誌資訊提交。

[[email protected] project]$ git log

上面的命令會產生以下結果。

commit e944e5aab74b26e7447d3281b225309e4e59efcd Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:41:42 2013 +0530 Added my_strcpy function commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <[email protected]> Date: Wed Sep 11 07:32:56 2013 +0530 Initial commit

Jerry 為變化感到高興,他想推他的變化

[[email protected] project]$ git push origin master

上面的命令會產生以下結果。

To [email protected]:project.git ! [rejected] master −> master (non-fast-forward) error: failed to push some refs to ‘[email protected]: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命令遠程命令來同步自己的本地倉庫。

[[email protected] 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

[[email protected] project]$ git log

上面的命令會產生以下結果。

commit e86f0621c2a3f68190bba633a9fe6c57c94f8e4f Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:41:42 2013 +0530 Added my_strcpy function commit cea2c000f53ba99508c5959e3e12fff493ba6f69 Author: Tom Cat <[email protected]> Date: Wed Sep 11 08:32:07 2013 +0530 Changed char pointer to const char pointer commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <[email protected]> Date: Wed Sep 11 07:32:56 2013 +0530 Initial commit

現在,Jerry 的本機存放區庫是完全同步的遠程倉庫。所以,他可以放心地將他的變化。

[[email protected] 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 [email protected]:project.git cea2c00..e86f062 master −> master

 

PS:如果您想和業內技術大牛交流的話,請加qq群(521249302)或者關注公眾 號(AskHarries),謝謝!

Git 更新操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.