Committing changes is a fundamental aspect of using Git. The commit process involves recording snapshots of your project at various stages, allowing you to track progress, revert changes, and collaborate with others efficiently. In this article, we’ll explore the complexity of committing in Git, from basic commands to best practices.
What are Commits?
A commit in Git is basically a snapshot of your project at a particular point in time. Each commit captures the state of the files in your working directory and includes a unique identifier (a SHA-1 hash), a commit message, and metadata such as the author and timestamp.
Key Components of a Commit
- SHA-1 Hash: A unique identifier for each commit.
- Commit Message: A descriptive message that explains the purpose of the commit.
- Author: The person who made the commit.
- Timestamp: The date and time when the commit was made.
- Parent Commit(s): The preceding commit(s) in the project history.
Steps include in Committing in Git
Suppose if somebody changed something in the file say “a.txt” we need to add those changes in the repository for that we will be using the command git add file_name, and to make a commit we will be using the command.Â
git commit -m "Commit_message" file_name

Adding and Committing a file
Shortcuts: Now if we have changed a lot of files in the directory, so to avoid using git add file_name for every file we can avoid this by writing some shortcuts so now let’s have a look at the commands. Following are some of the shortcuts to add the files.
Git Commands |
Action Performed |
git add –all |
Add all the files present in the repository |
git add . |
Add all the files present in the current repository |
git add -u |
To only add files that are currently tracked it will not track any other file which is not being tracked by the git |
Now if we have modified any already existing files we can use the command git commit -am “Commit_message_here” this command will add and commit a file at the same time.

Adding and Committing a file in a single command
Good commit messages: So now if someone is traversing through git logs he/she should understand are the changes being done, why it has been done, and how it has been done.

Some of the examples of the Good Commit messages
Amending a commit
If some latest commit has been made into the repository that is not yet pushed to the upstream repository on GitHub and if you think that the commit message is incorrect we can edit or amend the commit message by using the below command as follows:
git commit --amend
The above command will open the default text editor for e.g vi, vim, or emacs and when it gets open you can see the commit message which you have used earlier now you can edit that commit message to whatever message you find suitable. It is illustrated via below pictorial aids as follows:

Committing the file c.txt

Seeing git logs

Opens up an editor for changing the commit message

Using the command git commit –amend
If we want the previous commit message only without changing it.
git commit --amend --no-edit

Using  the command git commit –amend –no-edit
Committing without opening an editor:
Git usually opens up a default editor like vi, vim, or emacs on running the git commit command so to avoid this we can also type in the commit message in the git commit command only bypassing the -m option.
git commit -m "Commit message here"

Using the git commit with -m as an option
We can also multiple messages as arguments in one command using the below command as follows:
git commit -m "message_1" -m "message_2"

The syntax for passing multiple messages in a git commit command

Using git log we can see that the commit messages are in multiple lines now
Committing Changes Directly
For tracking the changes made to a file or a folder we first add it into the staging and then we commit that file but we can do this all in just one the command which will add the changes made to the file along with a commit message The command is basically as follows: Â
git commit -am "commit_message here"

Using the git commit -am “commit_message here”
For committing a particular file present in a folder for that we can use the below command as follows:Â
git commit /path_of_the_file -m "commit_message here"

For committing a particular file  git commit /path_of_the_file -m “commit_message here”
Now let us discuss which line should be staged for committing for selection
Suppose we have done many changes in one or more files but at last, we only want some of the changes to be committed so to select those changes we can use the below command so that we can select the desired changes.
The command is git add -p  file_name and whatever change we have done will be displayed individually for each change, we will be prompted to choose one of the following options. Let us understand the various options given by the command git add -p command
It is depicted below in tabular format below as follows:
Symbol |
Action Performed |
y |
Yes add this hunk |
n |
No don’t add this hunk |
d |
No, don’t add this hunk, or any other remaining hunks for this file. useful if you have already added what you want to, and want to skip over the rest |
s |
split the hunk into smaller hunks if possible |
e |
Manually edit the hunk. This is probably the most powerful option it will open the hunk in a text editor and you can edit it as needed. |

Exploring the -e option of git add -p
Creating an Empty Commit
As we know that while doing a commit we need to create a file or make changes in a file to commit the file but creating an empty commit will help us without having to edit or create a file we can easily create commits.
The –allow-empty will help us in creating the commit without having to edit or create a file.

Created an empty commit using the –allow-empty parameter
Committing Changes in Specific Files
As we know that for committing changes made to a file for that we need the file to be added in the staging area and then make a commit of those particular here in this explanation the files are already in the staging area that’s why on using the command git commit file_name1 file_name2 command is working.

Committing particular files
Committing at a Specific Date
While committing a file if you want to set a particular date that will appear in the standard output of the git log. For that, the command will be git commit -m ‘commit_message” –date YYYY-MM-date.Â

The syntax for committing a file at a particular date
Let us see our commit in the git log

Here we can see that in the git log command output the given commit has the date that we mentioned in the git commit command
The date parameter accepts a lot of flexible formats which are being supported in git.
git commit -m 'commit_message' --date yesterday

Here we can see in the git log it is showing us yesterday’s date
git commit -m 'commit_message' --date '3 days ago'

Using the command git commit -m ‘commit_message’ –date ‘3 days ago’

Using the command git log and here we can see that it showing us the commit date 3 days ago because today is 30th marchÂ
But when we don’t specify time git uses the current time and only the date changes then. Now if you want that the time should be changed.
Amending the Time of commit:
We can amend the time of the commit using the below command as follows:Â
git commit --amend --date="day_name month_name date time YYYY -0400"
Or even
git commit --amend --date="now"
The second command shows the current date and time

Here we can see in the git log it is showing the current date and time
Similar Reads
What is Git Commit?
Git is a powerful version control system that helps developers manage and track changes in their code. One of the fundamental concepts in Git is the "git commit." This command is important for recording changes to a repository and forms the backbone of version control. In this article, we will explo
5 min read
Git - Filtering the Commit History
Git source code versioning tool provides a lot of features. One of the most important and useful features is log or history. We can use git log command in order to list, filter, view commit history in different ways. Here we will examine git log command usage in detail with examples. List Commit His
4 min read
How to Delete Commit in Git?
Deleting a commit in Git can be done in several ways, depending on whether the commit is local or has already been pushed to a remote repository. Hereâs an article on how to delete a commit in Git, covering both recent and older commits, as well as considerations for working with remote repositories
3 min read
Git - Squash Commits
In Git, squashing refers to combining two or more commits into a single commit. This is typically done to improve the organization and clarity of the commit history, especially when working on a collaborative project with other developers. In this article, we will review the feature git squashing, w
2 min read
Using Git on CommandLine
Git is very important and powerful tool when it comes to the software development life cycle. Now in this article, we are going to see why git is used vastly in the software industry and what kinds of operations we can perform with git. So git is basically a distributed version control system for tr
5 min read
How to Back Commit in Git?
In this article, we are covering how to undo commits in Git. Sometimes, you might make mistakes or realize you need to revert changes you made in a previous commit. Luckily, Git offers a few ways to undo commits. Table of Content Approach 1: Using 'git revert'Approach 2: Using 'git reset' Approach 1
4 min read
How to Force Commit in Git?
Git is a powerful version control system used by developers worldwide to manage and track changes in their codebases. However, there are times when you might need to force commit changes in Git, overriding previous commits or pushing changes to a remote repository with conflicts. This guide will exp
3 min read
Examining Git
Git is a powerful version control system that helps to manage projects efficiently. Initially created by Linus Torvalds in 2005 for the development of the Linux kernel, Git has since become the go-to tool for version control in software development. Its distributed nature allows multiple developers
5 min read
How to Merge Commits in Git?
Merging commits in Git is a crucial part of version control, especially when working on complex projects with multiple contributors. Combining commits can help streamline the commit history, making it cleaner and easier to understand. In this article, weâll explore different methods to merge commits
3 min read
How to Squash Commits in Git?
Maintaining a clean and organized Git history is very important for collaboration and project management. One way to streamline your commit history is by squashing commits, which combines multiple commits into a single, more coherent commit. In this article, we will see how to squash commits in Git.
2 min read