修改现有函数
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),谢谢!