In this tutorial, we will see what is “git commit” command, what are some of the available options and how to use it with practical examples.
Table of Contents
What is Git Commit Command?
First of all, let’s see what is the git commit command and why it is used.
The Git commit command is used to create a new commit, which is a snapshot of the current state of the repository. The commit includes a message that describes the changes made to the codebase.
This message is important because it helps other people or developers understand why the changes were made and what they do.
So simply understand ‘git commit’ as a command to track the change in a repository.
Common Git Commit Command Options
Git commit command has several options and here we’re going to discuss some of the most useful and most used commands in real life or production environment.
1. git commit -a
This command is used to stage the changes which you have made. It basically tells git to include the changes in the next commit.
This is a very important command to add new files, modified files, or deleted files to the next commit. Before pushing your changes to the remote branch this command must be run.
2. git commit -m
This command is used to add a message to your commit. Basically, it is a summary of the changes you have made to your repository.
3. git commit -am
This is a very common git commit command and is a combination of both the above two commands. It stages the changes and also adds a message to the commit
4. git commit –allow-empty
If you want to create an empty commit without any changes to your files, you can use the ” –allow-empty” option with the git commit command.
This command is used mostly for testing purposes in production or testing environments and you can push an empty commit without changing any files.
For more git commit options, please visit Git’s official man page.
Git Commit Command Practical Examples
We discussed some of the most used git commit options, now let’s see it in action through these simple live examples.
For this, we are going to use a demo git repository called “git-commit-with-examples“. If you want to follow along, just clone the below git repository.
1. Example: git commit -a
For this example, we are going to modify the README.md file.
It has an incorrect spelling of commit, so just correct the spelling and stage the changes using this command.
Once you have modified the readme file, you will see that changes are not staged like the below message when you run the `git status` command
➜ git-commit-with-examples git:(main) git status On branch main Your branch is up to date with 'origin/main'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.md
So to stage the commit, run ‘git commit -a’
➜ git-commit-with-examples git:(main) git commit -a [main 191fd22] Correcting the spelling of commit :) 1 file changed, 1 insertion(+), 1 deletion(-)
To stage a newly created file, the first track that file using
git add filename command and then run the `git commit -a` command
➜ git-commit-with-examples git:(main) ✗ git add a.txt ➜ git-commit-with-examples git:(main) ✗ git commit -am "New file" [main 1ea9f10] New file 1 file changed, 1 insertion(+) create mode 100644 a.txt ➜ git-commit-with-examples git:(main) git push origin main Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 365 bytes | 365.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:storagetutorials/git-commit-with-examples.git 11dfa32..1ea9f10 main -> main
Note: After running the above command you will have to add a commit message in the editor. If you don’t want to do that, check the next example.
2. Example: git commit -am
Now, let’s make another change and then stage and commit the change together with a message in a single line in our terminal.
Now, just add another line in the README.md file and stage & commit the changes like below:
➜ git-commit-with-examples git:(main) ✗ git status On branch main Your branch is ahead of 'origin/main' by 1 commit. (use "git push" to publish your local commits) Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.md no changes added to commit (use "git add" and/or "git commit -a") ➜ git-commit-with-examples git:(main) ✗ git commit -am "Readme: Added another line" [main ab7504c] Readme: Added another line 1 file changed, 1 insertion(+)
3. Example: git commit –allow-empty
Now to push an empty commit, you just have to use the “–allow-empty” flag with the git commit command.
➜ git-commit-with-examples git:(main) git commit --allow-empty -m "This is empty commit" [main 11dfa32] This is empty commit
Once you have any empty commit, you can push it to the remote.
An empty commit will look like the below image in the GitHub UI:
After applying all the commits in the example, your commit history in GitHub will look like below:
These are some of the basic git commit command options and the three examples we’ve explored should provide you with a solid understanding of how to use the Git commit command effectively in your own projects.
If you would like to learn about advanced git commands, don’t forget to subscribe to our free newsletter.
Here are some of the most asked questions related to git commit:
Q1. What is the difference between Git commit and Git push?
A. Git commit records changes made to the local repository, while Git push uploads those changes to a remote repository. Git commit is used to create a snapshot of the codebase, while Git push is used to share that snapshot with others.
Q2. How do I add a message to my Git commit?
A. Use the -m flag followed by a message in quotes to add a message to your Git commit. For example, git commit -m “Fixed bug in login form”.
Q3. Can I undo or modify a Git commit after it has been made?
Yes, you can undo or modify a Git commit after it has been made using the git reset, git revert, or git commit –amend commands. However, these actions can have consequences, such as losing changes or creating conflicts, so it is important to use them carefully.
Q4. What is the maximum length of a Git commit message?
A. There is no hard limit to the length of a Git commit message, but it is recommended to keep messages concise and informative.
Q5. Can I use Git commit to revert changes made to my codebase?
No, Git commit records changes made to the codebase, but it does not revert them. To revert changes, use the
git revert or git reset commands.
Q6. How do I sign my Git commit using GPG?
A. Use the -S flag followed by your GPG key ID to sign your Git commit using GPG.
git commit -S -m "Added new feature".