Git Commit

Divya Srinivasan

What is a Commit?

A commit acts like a save point in your project history.
It captures the current state of your files along with a message that explains what was changed.
If needed, you can always roll back to an earlier commit.

Some important commit commands:
  • git commit -m "message" — Create a commit with a message describing the changes you’ve stag
  • git commit -a -m "message" — Commit all modified and deleted tracked files directly, skipping the staging step
  • git log — View the history of commits in your repository

Commit with a Message using -m

To save the changes you’ve staged, use the git commit -m command followed by a short, descriptive message:

Example

git commit -m "Initial release of Hello World!"

Output:

sql
[master (root-commit) 221ec6e] Initial release of Hello World!
 3 files changed, 26 insertions(+)
 create mode 100644 README.md
 create mode 100644 bluestyle.css
 create mode 100644 index.html

Commit All Changes Without Staging (-a)

If you only want to commit changes to files Git is already tracking, you can skip the staging step by using:
git commit -a -m "Your commit message"
This command automatically stages all modified and deleted tracked files before committing them.
Note: It does not include new/untracked files—you still need to add those manually with git add.

Example

git commit -a -m "Update README formatting"
[master abc1234] Update README formatting
 1 file changed, 2 insertions(+)

What happens if you try to commit a new file with -a?

If you run:
git commit -a -m "Try to commit new file"
And you have a new (untracked) file like index.html, Git will respond with something like:
vbnet
On branch master
No commits yet
Untracked files:
  (use "git add ..." to include in what will be committed)
        index.html
nothing added to commit but untracked files present (use "git add" to track)
Explanation:
The -a flag only stages and commits changes to files that are already tracked by Git.
It does not include new, untracked files.
To commit a new file, you first need to add it to the staging area:
git add index.html
git commit -m "Add new file"

Write Multi-line Commit Messages

If you run git commit without -m, Git opens your default editor so you can write a detailed commit message:
git commit
Write a short summary on the first line (keep it under 50 characters), then leave a blank line before adding more details.

Commit Message Best Practices

 Keep the first line short (≤ 50 characters)
 Use imperative mood (e.g., Add feature instead of Added feature)
 Leave a blank line between summary and details
 Explain why the change was made, not just what changed

Other Useful Commit Options

  • Create an empty commit (no changes):
git commit --allow-empty -m "Start project"
  • Re-use the last commit message (without opening editor):
git commit --no-edit
  • Amend the last commit, keeping its message:
git commit --amend --no-edit

Troubleshooting Common Commit Mistakes

  • Forgot to stage a file?
Just add it and commit again, or add it and amend:
git add forgotten-file.txt
git commit --amend
  • Typo in your commit message?
Update the last message:
git commit --amend -m "Corrected message"
  • Committed the wrong files?
Undo the last commit, but keep changes staged:
git reset --soft HEAD~1

View Commit History

See full commit history:
git log

Example output:

pgsql
commit 09f4acd3f8836b7f6fc44ad9e012f82faf861803 (HEAD -> master)
Author: w3schools-test
Date:   Fri Mar 26 09:35:54 2021 +0100

    Updated index.html with a new line

commit 221ec6e10aeedbfd02b85264087cd9adc18e4b26
Author: w3schools-test
Date:   Fri Mar 26 09:13:07 2021 +0100

    First release of Hello World!

Shorter Commit Log

Show each commit on a single line:
git log --oneline

Example:

pgsql
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!
See What Changed in Each Commit
Show files changed and stats:
git log --stat

Example:

pgsql
commit 09f4acd3f8836b7f6fc44ad9e012f82faf861803 (HEAD -> master)
Author: w3schools-test
Date:   Fri Mar 26 09:35:54 2021 +0100

    Updated index.html with a new line

 index.html | 1 +
 1 file changed, 1 insertion(+)

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send