Commit in Git



Git Commit is referred to the act of submiting your code and versioning it in the Git repository for future reference. In more simpler terms, it's like adding a checkpoint which you can refer later.



Why do I need Git commit?


Using git commit has many benefits such as:

  • Checkpoints: You can add these checkpoints in your code, so that you don't lose the older versions of codes when you make some breaking changes in the newer versions.
  • Quick Fall back: Let's suppose your new code is breaking in production, and you want to quickly fall back to your older build. You just have to create a new branch from the previous commit and deploy it in the production environment, without the need to do live debugging and changes on the production environment (which may cause downtime for users and may affect business).
  • Tracing the code & commiteer: You found some piece of code in your project but not sure of the idea behind adding that code, then you can refer to the commit history to read the commit messages describing the change.
  • Tracing the Requirements: The commit messages can also be used to track the User story ticket numbers like JIRA ticket to track the detailed requirements of the feature being submitted in the PR.

Git Image

Commiting Changes


To commit your changes to git, you can use the below command:

git commit -m "your commit message"                       
                    

Replace "your commit message" with the actual message you want to use. You can add a relevant title here mentioning the change or feature being submitted as part of this commit.

Before committing your changes, we need to ensure that the changes are staged first. Otherwise you may get an error mentioning "no changes to commit...".



Stage and Commit in one go


If you want to stage and commit all the files in one go. Then you can skip that extra staging step by using this shortcut command:

git commit -a -m "second commit without staging"                       
                    

This will automatically stage all the tracked files and commit them to the repo with the given commit message. All the interim steps will be executed in one go.

A point to remember is that this command will only work for the files which are already being tracked, i.e. the files which were earlier committed atleast once. If you just created a new file which is yet to be commited for the first time, then it will be skipped. For those new files you may be required to stage them first before commiting.



Multi-line Commit Messages


In the earlier commands, the commit message spanned across just one single line. What if you want to add a longer description? That's when you can make use of multi-line comments by using this command:

git commit    

Yes, that's it. No commit message...

You may ask, then where do I add the commit message?

When you run that command, an editor will open up on your terminal with some pre-written messages (which are commented using # keywords) like this:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Changes to be committed:
#       modified:   file.txt
#
~
~
~
~                      
                    

You can just add your commit message there without using the # key (as that will ignore the commit message).

If the editor opens in your terminal, it might be in read only mode initially. You just have to press "INSERT" button to make it editable. Once you are done adding the commit messages in multiple lines, click on the "Escape" button on your keyboard, and then press the key combination ":wq" to perform the commit action.



Viewing commit history


If you want to view the history of recent few commits, then you can use this command:

git log    

Once you enter this command you will see the meta data of the recent commmits like this:

commit d222919719b2e72016510ec479f96236c642a38d (HEAD -> master)
Author: Your name 
Date:   Fri Apr 3 11:23:48 2026 +0530

    My third commit

commit e49e9aca74e111f8413633cb6ba3ee2b051f6386
Author: Your name 
Date:   Fri Apr 3 09:14:33 2026 +0530

    My commit message line 2
    My commit message line 3
    My commit message line 4

commit e19139d29d7798aa4847b1896d970f20bf7a7576
Author: Your name 
Date:   Fri Apr 3 09:09:11 2026 +0530
:        
    

You can scroll through the list for older commits by using the down arrow key on your keyboard. Once you are done, you can enter the ":wq" key combination to exit the logs.



That's all for this tutorial, I hope you liked reading this tutorial. Do share this among your friends or colleagues.

Recommendations
FREE Sales CRM Software

Fully customizable CRM Software for Freelancers and Small Businesses

Signup for Free

Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount