Updating a Git Repository: A Step-by-Step Guide

Learn how to update your local Git repository with the latest changes from a remote repository. This guide will cover the git pull command and other relevant operations.



Git - Update Operation

Modify Existing Function

Tom starts by performing a clone operation on the repository and discovers a new file, string.c. Curious about who added this file and its purpose, he executes the git log command.

Syntax

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

The command results in the following output:

Output

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)

The clone operation creates a new directory within the current working directory. Tom navigates into the newly created directory and executes the git log command.

Syntax

[tom@CentOS ~]$ cd project/
[tom@CentOS project]$ git log

The output of the git log command reveals:

Output

commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: tom Mouse 
Date: Wed Sep 11 08:05:26 2013 +0530

Changed return type of my_strlen to size_t

commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat 
Date: Wed Sep 11 07:32:56 2013 +0530

Initial commit

After reviewing the log, Tom finds that tom added string.c to implement basic string operations. Intrigued by tom’s code, Tom opens string.c in a text editor and immediately spots a bug. In the my_strlen function, tom is not using a constant pointer, prompting Tom to modify tom's code.

Syntax

[tom@CentOS project]$ git diff

The output of the git diff command shows:

Output

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

After testing the modification, Tom commits his changes:

Syntax

[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'

The commit produces the following output:

Output

[master cea2c00] Changed char pointer to const char pointer
1 files changed, 2 insertions(+), 2 deletions(-)

Tom then checks the log again:

Syntax

[tom@CentOS project]$ git log

The output displays:

Output

commit cea2c000f53ba99508c5959e3e12fff493b
Author: Tom Cat 
Date: Wed Sep 11 08:32:07 2013 +0530

Changed char pointer to const char pointer

commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: tom Mouse 
Date: Wed Sep 11 08:05:26 2013 +0530

Changed return type of my_strlen to size_t

commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat 
Date: Wed Sep 11 07:32:56 2013 +0530
Initial commit

Next, Tom uses the git push command to push his changes:

Syntax

[tom@CentOS project]$ git push origin master

The output of the push command is:

Output

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

Add New Function

Meanwhile, tom decides to implement a string compare functionality. He modifies string.c, which now appears as follows:

Syntax

[tom@CentOS project]$ git diff

The output of this command shows:

Output

index 7da2992..bc864ed 100644
--- a/string.c
+++ b/string.c
@@ -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 length of %s = %lu\n", s[i], my_strlen(s[i]));
+   strcpy(p1, s[0]);
+   printf("string copied: %s\n", p1);
}

Next, tom adds the changes to the staging area:

Syntax

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

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

[tom@CentOS project]$ git commit -m 'Added my_strcpy function'

The output of this commit shows:

Output

[master 1234567] Added my_strcpy function
1 files changed, 11 insertions(+), 2 deletions(-)

Subsequently, tom pushes his changes:

Syntax

[tom@CentOS project]$ git push origin master

The push command generates the following output:

Output

Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 283 bytes, done.
Total 2 (delta 1), reused 0 (delta 0)
To gituser@git.server.com:project.git
cea2c00..1234567 master −> master

Next, Tom pulls the latest changes made by tom:

Syntax

[tom@CentOS project]$ git pull origin master

The output shows:

Output

From gituser@git.server.com:project
   cea2c00..1234567 master -> origin/master
Updating cea2c00..1234567
Fast-forward
 string.c | 11 ++++++++---
 1 files changed, 9 insertions(+), 2 deletions(-)

Tom then tests the changes made by tom. To verify tom's implementation, he executes the updated string.c file:

Syntax

[tom@CentOS project]$ gcc string.c
[tom@CentOS project]$ ./a.out

The output of the test shows:

Output

string length of Git tutorials = 15
string length of Tutorials Point = 14
string copied: Git tutorials

After confirming that everything functions correctly, Tom is satisfied with tom's modifications.