Signup/Sign In

Git Commit

Commit is a snapshot of the current state of our project which is permanently stored in the repository. We can use this snapshot in the future and compare it to our current version and eliminate complicated errors. Commit forms the foundation of version control because we can store a version of our project only after committing it. In this tutorial, we learn more about commit and how to commit changes in Git.

What is a Commit?

  • Commit is a way of permanently storing changes in our repository. We can also think of commit as a timestamp in the development journey of our project which captures the state of our project at that instant of time.

  • Git does this by capturing a snapshot of the state of the files present in the Staging Area and storing it permanently in the repository as a new version of the project.

  • At any point in time, we can move back to any of these versions that we committed.

  • Each commit has a pointer that points to its parent which is the commit that was made just before the current commit. The first or the initial commit does not have a parent.

Structure of commits

  • Some version control systems store the commits as differences in the project state rather than a complete snapshot of the project at that time. This proves to be inefficient as every time we have to move back to a previous version, we need to reconstruct the project from all the previous versions.

  • Each commit is identified by a unique string called Commit Hash. We can use this to get information about a commit or to revert to that commit. We can see this hash by running the git log command that shows us the history of our commits and other information about commits as well. The string shown in yellow color after the word commit is the hash of that commit.

Viewing the commit history using git log command

How to Commit?

Git uses the git commit command to commit the files present in the staging area permanently in the repository. We can only use the git commit command when we have at least one file present in the staging area or when we have tracked some file in the past. The snapshots which are captured by the commit are stored in the .git file. Let's see how to use this command.

  • The simplest way to commit is to use the -m flag along with the git commit command. We need to add a message to describe the commit.

$ git commit -m “Message  about the commit”
  • We can just use the git commit without the -m flag but this opens the default text editor where you can enter the message for the commit.

$ git commit
  • We also have the -a flag with which we can skip the staging part. But this will only work for the files which have been staged at some point in time in the past and then modified. Using just the -a flag will open the default editor where you can enter the commit message.

$ git commit -a
  • We can use both the -a and -m commands together to add a message to the commit.

$ git commit -am “Message about the commit”
  • We can also modify the most recent commit by using the --amend flag with the commit message. This can be used for various purposes like when you just want to change the message of the most recent commit or when you want to add more modified files to that commit. But we need to be sure that this most recent commit was not pushed to a remote directory.
$ git commit --amend -m “Updated Message”


Commit is something that we will use a lot while working with Git. Committing a change is like permanently storing that version of our project and we can go back to the version anytime in the future. We use the Git Commit command to commit changes in Git. We can use several flags with this command like the -m flag to add a message and the -a flag to skip the staging part. I hope this article was helpful and you learned something new. In the next tutorial, we will learn how to push our local repository to a remote repository.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight