git.pngGit

Git Commit

In this article, we will learn How to use git commit command in Git Bash

Total Views:   1991


In this article, we will learn How to use the git commit command in Git Bash. Git commit command is used to move the changes done in the staging area to the local repository. A commit is a snapshot that represents the current state of your code. A commit contains the commit hash which uniquely identifies the commit, committed by the author, a list of modifications done in the commit, etc.

In this article, we will cover the below commands:

1. git commit -m <Message>

2. git commit

3. git commit -a

4. git commit -amend -m <Message>

 

How to use git add command in git bash?

 

1. git commit -m <Message>

For demonstration, I already have a repository in which few files are already added to the staging area. Use the “git status” command in order to check the status in the current branch.

 

Use “git commit -m <Message>” in order to commit the changes in the local repository. The -m option specifies the message you are going to add along with the commit. Always use a meaningful message with commit which helps in understanding the reason for the changes done in the commit. A hash is generated which uniquely identifies the commit.

 

After commit, “git status” will show that there is nothing to commit.

 

The “git log” command is used to read the history logs for the repository. It gives details like commit hash, author details, commit date, and message details.

 

2. git commit

Let’s do some changes again in the working area and check the status again for the changes with the “git status” command.

 

Add those changes to the staging area with the “git add” command as shown below.

 

Now run the “git commit” command. 

 

An editor which is configured in git will open for the input of the commit message.

 

Put the meaningful comments, save & close the file. Changes will be successfully committed in the repository.

3. git commit -a

Let’s again do some changes in the working area. “git status” will show the changes which are not staged yet.

 

Use the “git commit -a” command in order to skip the adding changes to staging. It will directly perform the git add operation. It’s recommended to use this command carefully as this command only adds the tracked file. Newly added files (untracked files) are not included in the commit which may lead to mistakes/confusion.

 

Provide meaningful comments and save the file in order to commit the changes. After successful execution of the command, “git status” will show no pending changes for the commit.

 

Use the “git log” command in order to read the history logs.

 

4. git commit -amend -m <Message>

Git amend option is used to edit the last commit message. If accidentally a commit with the wrong message is done then the user can change the last commit message with this command.

 

Check the logs again with the “git log” command. You can clearly see that the message of the last commit is changed.