Making Changes to a Git Repository: A Beginner's Guide

Learn how to modify files and add new content to a Git repository. This guide will walk you through the basic steps of making changes and preparing them for commit.



Git - Perform Changes

After cloning the repository, tom decides to implement some basic string operations. To do this, he creates a new file named string.c. Once he adds the necessary code, string.c will look like this:

Syntax

#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 length of %s = %d\\n", s[i], my_strlen(s[i]));

   return 0;
}
Output

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

After compiling and testing his code successfully, tom can now safely add these changes to the repository.

Adding Changes to the Staging Area

The git add operation is used to add files to the staging area. tom checks the status of his repository:

Syntax

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

Next, he adds string.c to the staging area:

Syntax

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

At this point, Git shows a question mark before the file names, indicating that these files are not yet part of Git's tracking. This is why Git displays a question mark before the file names.

After adding the file to the staging area, tom checks the status again:

Syntax

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

Committing Changes

To commit his changes, tom uses the git commit command with the -m option to include a commit message:

Syntax

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

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

After committing his changes, tom can view the log details by running the git log command, which displays information about all the commits, including their commit ID, author, date, and SHA-1 hash:

Syntax

[tom@CentOS project]$ git log
Output

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

    Implemented my_strlen function

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

    Initial commit