Git 简明教程

Git - Perform Changes

Jerry 克隆了存储库并决定实现基本字符串操作。因此,他创建了 string.c 文件。添加内容后,string.c 将如下所示 −

Jerry clones the repository and decides to implement basic string operations. So he creates string.c file. After adding the contents, string.c will look like as follows −

#include <stdio.h>

int my_strlen(char *s)
{
   char *p = s;

   while (*p)
      ++p;

   return (p - s);
}

int main(void)
{
   int i;
   char *s[] =
   {
      "Git tutorials",
      "Tutorials Point"
   };

   for (i = 0; i < 2; ++i)

   printf("string lenght of %s = %d\n", s[i], my_strlen(s[i]));

   return 0;
}

他编译并测试了代码,一切都运行良好。现在,他可以安全地将这些更改添加到仓库中。

He compiled and tested his code and everything is working fine. Now, he can safely add these changes to the repository.

Git add 操作将文件添加到暂存区。

Git add operation adds file to the staging area.

[jerry@CentOS project]$ git status -s
?? string
?? string.c

[jerry@CentOS project]$ git add string.c

Git在文件名之前显示一个问号。显然,这些文件不属于 Git,这就是为什么 Git 不知道如何处理这些文件。因此,Git 在文件名之前显示一个问号。

Git is showing a question mark before file names. Obviously, these files are not a part of Git, and that is why Git does not know what to do with these files. That is why, Git is showing a question mark before file names.

Jerry 已将文件添加到暂存区,git status 命令将显示暂存区中的文件。

Jerry has added the file to the stash area, git status command will show files present in the staging area.

[jerry@CentOS project]$ git status -s
A string.c
?? string

为了提交这些更改,他使用了 git commit 命令,后跟 -m 选项。如果我们省略 -m 选项。Git 将打开一个文本编辑器,我们可以在其中编写多行提交消息。

To commit the changes, he used the git commit command followed by –m option. If we omit –m option. Git will open a text editor where we can write multiline commit message.

[jerry@CentOS project]$ git commit -m 'Implemented my_strlen function'

以上命令将生成以下结果 −

The above command will produce the following result −

[master cbe1249] Implemented my_strlen function
1 files changed, 24 insertions(+), 0 deletions(-)
create mode 100644 string.c

提交后,为了查看日志详情,他运行 git log 命令。它将显示所有提交的信息,包括提交 ID、提交作者、提交日期和提交的 SHA-1 哈希。

After commit to view log details, he runs the git log command. It will display the information of all the commits with their commit ID, commit author, commit date and SHA-1 hash of commit.

[jerry@CentOS project]$ git log

以上命令将生成以下结果 −

The above command will produce the following result −

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

Implemented my_strlen function


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

Initial commit