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)
克隆操作将在当前工作目录中创建一个新目录。他将目录更改为新创建的目录并执行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 +0530Changed return type of my_strlen to size_tcommit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 07:32:56 2013 +0530Initial commit
观察日志后,他意识到文件string.c是Jerry添加的,用于实现基本的字符串操作。他对 Jerry 的代码很好奇。于是他在文本编辑器中打开 string.c 并立即发现了一个错误。在 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 pointer1 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 +0530Changed char pointer to const char pointercommit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <jerry@tutorialspoint.com>Date: Wed Sep 11 08:05:26 2013 +0530Changed return type of my_strlen to size_tcommit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 07:32:56 2013 +0530Initial 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.c30Git 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 function1 files changed, 13 insertions(+), 0 deletions(-)
在推送操作之前,他通过查看日志消息来验证提交。
[jerry@CentOS project]$ git log
上述命令将产生以下结果
commit e944e5aab74b26e7447d3281b225309e4e59efcd
Author: Jerry Mouse <jerry@tutorialspoint.com>Date: Wed Sep 11 08:41:42 2013 +0530Added my_strcpy functioncommit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <jerry@tutorialspoint.com>Date: Wed Sep 11 08:05:26 2013 +0530Changed return type of my_strlen to size_tcommit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 07:32:56 2013 +0530Initial 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 +0530Added my_strcpy functioncommit cea2c000f53ba99508c5959e3e12fff493ba6f69
Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 08:32:07 2013 +0530Changed char pointer to const char pointercommit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <jerry@tutorialspoint.com>Date: Wed Sep 11 08:05:26 2013 +0530Changed return type of my_strlen to size_tcommit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>Date: Wed Sep 11 07:32:56 2013 +0530Initial 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
“Git如何更新”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341