Making a Git Commit

Consider that you have added all the required changes to the staging area. To read more about staging changes, refer to this blog.

If you run git status, the status of your repository looks like this: image.png

It says:

  • You are on the main branch.
  • There are no commits yet.

Making a commit

Now, to store them safely, you can move the changes from the staging area to the local repository. In order to do so, you can run the git commit command. image.png

You can provide a commit message using the -m option:

git commit -m "<some commit message>"

When you do this, a commit gets created. image.png

Git tells that this is a root commit which means it is the first commit.

Git log

You can check that a commit is created and see it's details by running the following command:

git log

image.png

This shows metadata like the author information, date at which the commit was made, the commit message, etc.

You can press q to quit this view.

What happens when we make a commit?

image.png

  • There are blobs which represent the contents of the files.
  • Git checksums every subdirectory and stores it as a tree object.
  • Git creates a commit object which has metadata and points to the root tree object.

To see the details of the commit object, you can copy the commit id obtained from running the git log command and run:

git cat-file -p <commit id>

When you do this, you can see the metadata of the commit and also the root tree object that it points to: image.png

You can run the same command with the tree object's id. When you do this, you can see that this tree object points to pom.xml file (blob) and also points to another tree object which represents the src directory: image.png

Do note that this commit that is made is only present on the local machine. In order to share the project with others easily, we would need a remote repository.