Git 更新操作

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)

Clone操作会在当前工作目录下创建一个新的目录。他会切换到新创建的目录并执行 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 +0530

Changed return type of my_strlen to size_t


commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 07:32:56 2013 +0530

Initial commit

观察日志后,他意识到文件string.c是Jerry添加的,用于实现基本的字符串操作。他对Jerry的代码感到好奇。于是他打开文本编辑器查看string.c,并立即发现了一个bug。在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 pointer
1 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 +0530

Changed char pointer to const char pointer


commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 08:05:26 2013 +0530

Changed return type of my_strlen to size_t


commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>
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 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 function
1 files changed, 13 insertions(+), 0 deletions(-)

在进行Push操作之前,他通过查看日志信息来验证提交。

[jerry@CentOS project]$ git log

上述命令将产生以下结果:

commit e944e5aab74b26e7447d3281b225309e4e59efcd
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 08:41:42 2013 +0530

Added my_strcpy function


commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 08:05:26 2013 +0530

Changed return type of my_strlen to size_t


commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>
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@tutorialspoint.com>
Date: Wed Sep 11 08:41:42 2013 +0530

Added my_strcpy function


commit cea2c000f53ba99508c5959e3e12fff493ba6f69
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 08:32:07 2013 +0530

Changed char pointer to const char pointer


commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <jerry@tutorialspoint.com>
Date: Wed Sep 11 08:05:26 2013 +0530

Changed return type of my_strlen to size_t


commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>
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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程