Git

Git Commit Options

When developers make changes to their projects, they must add those changes to the staging area. After that, it is required to push those changes to the local repository. For this purpose, the “git commit” command can be utilized. This command pushes the staging area’s changes to the local repository and generates the commit id. Moreover, different options can also be used with this command to perform different operations.

This article will explain different Git commit options.

What are the Different Git Commit Options?

The important Git commit options are as follows:

How to Use Git Commit “-m” Option?

The “-m” option in the “git commit” command is utilized to specify the commit message. It allows users to write/type the desired commit message using commands instead of the default text editor.

Check out the following steps to see how to utilize this option.

Step 1: View Git Status

First, view the current status of the working repository:

$ git status

It can be seen that there is one “demo.txt” file that needs to be committed:

Step 2: Commit Changes

Now, type out the “git commit” command along with the “-m” flag and the desired commit message to commit changes:

$ git commit -m "Demo file modified"

Step 3: View Commit Message

Next, check the commit history to view the commit message:

$ git log

In the below image, the highlighted commit message can be seen:

How to Use Git Commit “-a” Option?

The “-a” option can also be used with the “git commit” command to specify the commits. This option only commits all the already added files in Git. More specifically, this option does not commit the new untracked files. For a better understanding, look at the following steps.

Step 1: View Current status

First, view the status of the current directory through the following command:

$ git status

According to the below-provided output, the current repository contains one modified and one new untracked file:

Step 2: Commit Changes

Next, enter the below-listed command to commit changes:

$ git commit -a

After executing the above-stated command, the text editor will open up and it will ask to write the commit message. Type the desired commit message, save changes and close the editor:

The below image indicates that one file has been committed:

Step 3: Verify Changes

To verify whether the files have been committed or not, check the Git status:

$ git status

It can be observed that only the existing file has been committed and the new untracked file could not be committed:

How to Use Git Commit “–amend” Option?

The “–amend” option with the “git commit” command is utilized to rewrite the very last commit. It permits users to edit/modify the commit message. If the user has committed a wrong commit message, then this option can be used to edit the commit message.

Try out the following instructions to see how this option works.

Step 1: View Commit History

First, check the Git log to view the latest commit message:

$ git log

Step 2: Edit Commit Message

Then, write out the given-provided command to rewrite the last commit message:

$ git commit --amend

After executing the above-listed command the text editor will be opened. Rewrite the desired commit message, save changes, and close the editor:

Step 3: Verify Changes

Lastly, ensure whether the commit message has been modified or not by viewing the commit history:

$ git log

As you can see the commit message has been changed successfully:

We have efficiently explained important options of the “git commit” command.

Conclusion

The options commonly used with the “git commit” command are “-m”, “-a” and “–amend”. The “-m” option is utilized to set the desired commit message on the command line. Whereas, the “-a” option is used to specify the commits and write commit messages on the default text editor. Moreover, the “–amend” option rewrites the very last commit. This article explained different Git commit options.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.