How to add and commit files in Git?

In this example, we will add a few files and commit them one by one and complete all of them, and check the logs

The commands we use here are:

  • touch <file>Create a file in bash terminal
  • git add .Stage the file to the git
  • git commit -m”<Commit message>”Commit the changes with a message

git add is a Git command used to add files or changes to the staging area. The staging area, also known as the index, is a crucial part of Git that allows you to prepare specific changes for inclusion in the next commit.

When you make changes to files in a Git repository, those changes are initially considered “unstaged.” The git add command allows you to stage these changes, which means you are selecting them to be included in the next commit. By adding files to the staging area, you are specifying that you want these changes to be part of the next commit.

Let’s get started

Step #1. Create the first file, stage the file and commit it.

Here we will use an Ubuntu system with git installed. Also, have a git repo created. Commands such as git add and git commit will be used. Let us dive in.

ubuntu@codetryout:~/codetryout-code$ touch file1
ubuntu@codetryout:~/codetryout-code$ 
ubuntu@codetryout:~/codetryout-code$ git add .
ubuntu@codetryout:~/codetryout-code$ git commit -m"commit 1"
[master (root-commit) b36945a] commit 1
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1
ubuntu@codetryout:~/codetryout-code$ 

We have just created one file, named: file1, added(staged) that new file to git, and committed that file with a comment: commit 1.

Step #2. Create the second file, stage the file and commit it.

Similar to step 1, we are going to add another file, stage, and commit it now.

ubuntu@codetryout:~/codetryout-code$ touch file2
ubuntu@codetryout:~/codetryout-code$
ubuntu@codetryout:~/codetryout-code$ git add .
ubuntu@codetryout:~/codetryout-code$ git commit -m"commit 2"
[master efc1357] commit 2
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file2
ubuntu@codetryout:~/codetryout-code$

Step #3. Create the third file, stage the file and commit it.

Repeating the steps similar to step one and two. Adding a new file, staging, and committing.

ubuntu@codetryout:~/codetryout-code$ touch file3
ubuntu@codetryout:~/codetryout-code$ 
ubuntu@codetryout:~/codetryout-code$ git add .
ubuntu@codetryout:~/codetryout-code$ git commit -m"commit 3"
[master 6815e84] commit 3
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file3
ubuntu@codetryout:~/codetryout-code$ 

Step #4, View your commit logs

git log is a Git command used to display the commit history of a repository. It provides a detailed log of all the commits that have been made, allowing you to review and analyze the project’s development timeline.

To review your commit history, use git log command. Here is an example:

ubuntu@codetryout:~/codetryout-code$ git log
commit 6815e845f13046570aaccdb66aee38caa68a6a60 (HEAD -> master)
Author: Ubuntu <[email protected]>
Date:   Tue Jan 5 22:03:36 2021 +0530

    commit 3

commit efc13573a8b93ce10d74980b0ad4ac510e63734b
Author: Ubuntu <[email protected]>
Date:   Tue Jan 5 22:03:20 2021 +0530

    commit 2

commit b36945a7bc5df78259a97a6bc6c90224e56c1434
Author: Ubuntu <[email protected]>
Date:   Tue Jan 5 22:02:54 2021 +0530

    commit 1
ubuntu@codetryout:~/codetryout-code$

When you run git log in your terminal or command prompt within a Git repository, it will present a list of commits in reverse chronological order, starting with the most recent commit first. Each commit entry typically includes the following information:

  • Commit hash: A unique identifier for the commit, represented by a long alphanumeric string.
  • Author: The name and email address of the person who made the commit.
  • Date: The date and time when the commit was made.
  • Commit message: A brief description or summary of the changes made in the commit.


The git log command provides valuable information about the commit history, such as the sequence of changes, who made them, and when they were made. It helps you understand the progression of the project and track the work done by different contributors.

By following these steps, you can add files to the staging area and commit them to the local repository in Git. Remember to regularly commit your changes to keep track of the project’s progress and make it easier to collaborate with others.

Please do let us know if you have questions. This test case is done in an Ubuntu terminal, however the same will work in any Git command line, such as Git Bash, Linux/CentOS, and Docker containers with Git client.