Open In App

Committing in Git

Last Updated : 05 Apr, 2022
Like Article

A commit is a point in git which is treated as a “Save point” which keeps records of all save points, so if we want to go back to the previous save point we can switch back. it is the backbone of developing and updating software according to the passage of time and client demands. As per the requirement, we can toggle back to make changes. Commits are like a snapshot of all the files and folders present in the repository at a particular point in time. Let us discuss it with help of an illustration that is as follows: 


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

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads