## 修改现有函数
Sampson 执行克隆操作后,看到新的文件string.c,他想知道这个文件到存储库?目的是什么?于是,他执行 git 日志命令。
[sampson@CentOS ~]$ git clone gituser@git.server.com:project.git
上面的命令会产生以下结果。
Initialized empty Git repository in /home/sampson/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日志命令。
[sampson@CentOS ~]$ cd project/
[sampson@CentOS project]$ git log
上面的命令会产生以下结果。
commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Byron <xiaobosun@gridinfo.com.cn>
Date: Wed Sep 11 08:05:26 2015 +0530
Changed return type of my_strlen to size_t
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Sampson <sampson@12xue.com>
Date: Wed Sep 11 07:32:56 2015 +0530
Initial commit
查看记录后,他意识到,string.c 文件加入Byron 实现基本字符串操作。他是好奇Byron 的代码。于是他打开文本编辑string.c 文件,并立即找到了一个漏洞。 my_strlen函数中 Byron 没有使用常量指针。于是,他决定修改Byron 的代码。修改后的代码会这个样子。
[sampson@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;
经过测试,他提交他的变化。
[sampson@CentOS project]$ git status -s
M string.c
?? string
[sampson@CentOS project]$ git add string.c
[sampson@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(-)
[sampson@CentOS project]$ git log
上面的命令会产生以下结果。
commit cea2c000f53ba99508c5959e3e12fff493b
Author: Sampson <sampson@12xue.com>
Date: Wed Sep 11 08:32:07 2015 +0530
Changed char yiibaier to const char yiibaier
commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Byron <xiaobosun@gridinfo.com.cn>
Date: Wed Sep 11 08:05:26 2015 +0530
Changed return type of my_strlen to size_t
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Sampson <sampson@12xue.com>
Date: Wed Sep 11 07:32:56 2015 +0530
Initial commit
Sampson 使用git push命令,推他的变化。
[sampson@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
## 添加新函数
与此同时,Byron 决定实现字符串比较函数。于是,他修改string.c文件。修改后的文件会这个样子。
[byron@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;
}
经过测试,他准备推他的变化。
[byron@CentOS project]$ git status -s
M string.c
?? string
[byron@CentOS project]$ git add string.c
[byron@CentOS project]$ git commit -m "Added my_strcpy function"
[master e944e5a] Added my_strcpy function
1 files changed, 13 insertions(+), 0 deletions(-)
push操作之前,他验证通过查看日志信息提交。
[byron@CentOS project]$ git log
上面的命令会产生以下结果。
commit e944e5aab74b26e7447d3281b225309e4e59efcd
Author: Byron <xiaobosun@gridinfo.com.cn>
Date: Wed Sep 11 08:41:42 2015 +0530
Added my_strcpy function
commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Byron <xiaobosun@gridinfo.com.cn>
Date: Wed Sep 11 08:05:26 2015 +0530
Changed return type of my_strlen to size_t
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Sampson <sampson@12xue.com>
Date: Wed Sep 11 07:32:56 2015 +0530
Initial commit
Byron 为变化感到高兴,他想推他的变化
[byron@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 是不允许Byron 推他的变化。因为Git确定该远程仓库和Byron 的本地资源库不同步。正因为如此,他可能会失去项目的历史。因此,为了避免这种混乱的Git 对此操作失败。Byron 必须首先更新它的本地存储库,然后再只有他才能把他自己的变化。
## 取最新变化
Byron 执行git pull命令远程命令来同步自己的本地仓库。
[byron@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
抽取操作后Byron 检查日志消息,并找到 Sampson 的提交详细,提交ID为 cea2c000f53ba99508c5959e3e12fff493ba6f69
[byron@CentOS project]$ git log
上面的命令会产生以下结果。
commit e86f0621c2a3f68190bba633a9fe6c57c94f8e4f
Author: Byron <xiaobosun@gridinfo.com.cn>
Date: Wed Sep 11 08:41:42 2015 +0530
Added my_strcpy function
commit cea2c000f53ba99508c5959e3e12fff493ba6f69
Author: Sampson <sampson@12xue.com>
Date: Wed Sep 11 08:32:07 2015 +0530
Changed char yiibaier to const char yiibaier
commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Byron <xiaobosun@gridinfo.com.cn>
Date: Wed Sep 11 08:05:26 2015 +0530
Changed return type of my_strlen to size_t
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Sampson <sampson@12xue.com>
Date: Wed Sep 11 07:32:56 2015 +0530
Initial commit
现在,Byron 的本地存储库是完全同步的远程仓库。所以,他可以放心地将他的变化。
[byron@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