Hoạt động Update trong Git

Tùy chỉnh các chức năng đang tồn tại

Tom thực hiện hoạt động mô phỏng và thấy một file mới string.c. Anh ta muốn biết ai đã thêm file này vào kho chứa và với mục đích gì, vì thế anh ta chạy lệnh git log.

[tom@CentOS ~]$ git clone gituser@git.server.com:project.git

Lệnh này sẽ tạo ra kết quả sau:

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)

Hoạt động mô phỏng sẽ tạo một thư mục mới bên trong thư mục làm việc hiện tại. Anh ta thay đổi thư mục tới thư mục vừa tạo mới và chạy lệnh git log.

[tom@CentOS ~]$ cd project/

[tom@CentOS project]$ git log

Lệnh trên sẽ tạo ra kết quả sau:

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

Sau khi quan sát log, anh ta nhận thấy rằng tệp string.c được thêm bởi Jerry để thực hiện các hoạt động chuỗi cơ bản. Anh ta muốn biết về đoạn code của Jerry. Vì thế anh ta mở string.c trong text editor và ngay lập tức tìm thấy một bug (lỗi). Trong chức năng my_strlen, Jerry không sử dụng điểm trỏ hằng số. Vì thế, anh ta quyết định chỉnh sửa code của Jerry. Sau khi thực hiện chỉnh sửa, đoạn code trông như sau:

[tom@CentOS project]$ git diff

Lệnh trên sẽ tạo ra kết quả sau:

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;
}

Sau khi kiểm tra, anh ta commit các thay đổi của anh ta.

[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

Lệnh trên sẽ tạo ra kết quả sau:

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 sử dụng lệnh git push để push các thay đổi của anh ta.

[tom@CentOS project]$ git push origin master

Lệnh trên sẽ tạo ra kết quả sau:

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

Thêm chức năng mới

Trong khi ấy, Jerry quyết định thực hiện chức năng string compare. Vì thế anh ta chỉnh sửa string.c. Sau khi tùy chỉnh, file trông như sau:

[jerry@CentOS project]$ git diff

Lệnh trên sẽ tạo ra kết quả sau:

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;
}
}

Sau khi kiểm tra, anh ta sẵn sàng push thay đổi này.

[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(-)

Trước khi thực hiện hoạt động push, anh ta chỉnh sửa commit bằng cách quan sát các thông báo log.

[jerry@CentOS project]$ git log

Lệnh trên sẽ tạo ra kết quả sau:

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 vui mừng với những thay đổi này và anh ta muốn thực hiện push những thay đổi đó.

[jerry@CentOS project]$ git push origin master

Lệnh trên sẽ tạo ra kết quả sau:

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.

Nhưng Git không cho phép Jerry push những thay đổi của anh ta. Bởi vì Git nhận định rằng repository từ xa và repository nội bộ của Jerry là không đồng bộ. Vì điều đó, anh ta có thể lạc mất lịch sử của dự án. Để tránh mớ hỗn độn này, Git quên hoạt động này. Bây giờ, Jerry đầu tiên phải cập nhật repository nội bộ và chỉ khi sau đó, anh ta có thể push nhưng thay đổi riêng này.

Gọi ra những thay đổi mới nhất

Jerry chạy lệnh git pull để đồng bộ hóa repository nội bộ của anh ta với repository từ xa.

[jerry@CentOS project]$ git pull

Lệnh trên sẽ tạo ra kết quả sau:

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

Sau khi thực hiện hoạt động pull, Jerry kiểm tra các thông báo log và tìm thấy các chi tiết commit của Tom với ID commit cea2c000f53ba99508c5959e3e12fff493ba6f69.

[jerry@CentOS project]$ git log

Lệnh trên sẽ tạo ra kết quả sau:

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

Bây giờ repository nội bộ của Jerry đã được đồng bộ đầy đủ với repository từ xa. Vì thế anh ta có thể push những thay đổi một cách an toàn.

[jerry@CentOS project]$ git push origin master

Lệnh trên sẽ tạo ra kết quả:

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

Theo Tutorialspoint

Bài trước: Hoạt động Push trong HTML

Bài tiếp: Hoạt động Stash trong Git

Thứ Tư, 22/08/2018 11:42
4,73 👨 1.455
0 Bình luận
Sắp xếp theo
    ❖ Học Git