What is Git Commit?

Git is a distributed version control system widely used by developers to manage and track changes in their codebase. One of the most fundamental and essential commands in Git is git commit.

This command is important for recording changes to a repository and forms the backbone of version control. In this article, we will explore what a Git commit is, how it works, and best practices for using it effectively.

Table of Content

  • What is Git Commit?
  • Role of Commits in Git
  • Insert items from your Staging Area into your Local Repository
  • Working with Git Commit
  • Flags in Git Commit
  • Revert Commit Changes

What is Git Commit?

A Git commit is basically a snapshot of your project at a specific point in time. When you commit changes, you are telling Git to record the current state of your project, including any modifications you have made since the last commit. Each commit is uniquely identified by a SHA-1 hash, which ensures the integrity and uniqueness of the commit.

Role of Commits in Git

  • Track Changes: Every commit records changes made to the repository, enabling you to see what was modified, added, or deleted over time.
  • Collaborate: Commits are important for collaboration, as they allow multiple developers to work on the same project and merge their changes.
  • Revert Changes: If you need to undo changes, you can revert to a previous commit, ensuring that you can always return to a known good state.
  • Document Progress: Commit messages serve as a log of what changes were made and why, helping others (and your future self) understand the evolution of the project.

Insert items from your Staging Area into your Local Repository

For this git commit is used and the command is:

git commit -m “commit message”

The git commit command is used to move files from the staging area to your local repository. This command is run after git add and it can be seen as a checkpoint. After executing the git commit, your staging area will be empty.

Working with Git Commit

Suppose, you are on the master branch and you did a commit now git internally will create a block i.e., a node, of a commit you did. In git, we have a pointer called HEAD (It is the reference to the commit in the current branch in our case i.e., the master branch). When you commit something the HEAD will be pointed to the new commit and a hash key is assigned to that new commit. If you make another commit, again HEAD will move and point to the commit having another hash key. It can be assumed as a right-to-left singly linked list, whenever a new commit happens, a new node is created and points to HEAD, and after that HEAD is updated and comes over the new commit i.e., the latest one.

Flags in Git Commit

There are certain flags in git commit like -a, -m, -am.

  • Git commit -m “commit message”: A command that creates a commit with a commit message written under quotation.
  • Git commit -a: The command only includes modification to the files that have been added with git add at any time i.e., all the tracked files.
  • Git commit -am “commit message”: It is a combination of both the -m and -a commands.

If you now find that you accidentally left something out of your last commit, be it a file or an extra change to a file that you just committed, then you can fix that also. Let’s now make some modifications in the “file.txt” i.e., I write “Hello Crio” in that and now we have to repeat the process of add and commit.

After this, the file “file.txt” has been modified and you can check that using the git command git show.

If you want to view the history of everything that happens to a repository then you can use the git log command. A Git log is a running record of commits.

As you can see above there are two committing histories, let’s say you did a mistake in “file.txt” so you have the repeat the process once again and hence there is one more committing history generated. For the small number of history, it is fine, but if you are making mistakes several times and committing them then there is a huge number of history created and it is a bit confusing for the other developers when working with complex types of projects. So to handle that, git commit –-amend command is used. git commit –amend -m “commit-msg” where -m is a flag used for the commit message is a command used to doesn’t create another commit history and is just overrides the previous commit.

Here in the above image, you can see that there is no third history created. What did here is the previous commit is overridden and hence the commit history remains the same.

Revert Commit Changes

If you want to revert a commit you have made you have to use:

git reset --hard HEAD^

This command will revert the latest commit means the top commit.


Contact Us