How to Make Commits in Git

In the previous tutorial, we covered how to add modified files to the staging area in Git and in this tutorial we will learn how to make commits in Git.

 

Making a Commit

To make a commit we will use the Git commit command and pass in the -m flag so we can provide a short description of the commit. Let's try this out.

 

git commit -m "Fixed a create file bug"
[main a4b0b85] testing out staging
 1 files changed, 36 insertions(+)
 create mode 100644 download.py

 

If everything goes well you will see some information about what you committed in the output such as the number of files changed, the number of lines in each of the files that were modified and the ID of the commit.

 

The important thing to observe is that you provide a useful message describing what the purpose of your commit is. Otherwise, you will end up with a bunch of commits that you barely know the purpose of.

 

How to Review the Commit History

Once you have performed one or many commits for a project in Git you may want to review the commit history. We can do this by using the Git log command.

 

git log
commit ca5e3d960ac953c55ea529d36e81da2316e2cf70
Author: John <[email protected]>
Date:   Tue Oct 13 23:02:53 2020 +0100

    test file

commit e2cfbec36f88181973d1b18e5097f7d956a776a5
Author: John <[email protected]>
Date:   Tue Oct 13 22:58:45 2020 +0100

    first commit

 

As you see from the output above the Git log command returns a list of all the previous commits for a repository. The first piece of data for each item is the full ID of the commit followed by the author, the date committed and finally the commit message.

 

Display The Commits On One Line

You may just want to view the short ID and message for each historical commit to make them easier to view. You can do this by passing --oneline after the Git log command.

 

git log --oneline
ca5e3d9 test file
e2cfbec first commit

 

Conclusion

You now know how to make commits in Git and view the commit history.

history commit