Git Commit

A commit is a permanent snapshot of the staged changes, saved into the repository's history. Once committed, that snapshot is always accessible — it can be viewed, compared to other commits, or restored at any time.

Every commit stores:

  • A unique identifier (a 40-character SHA-1 hash like a3f92b1c...)
  • The author's name and email
  • The date and time
  • A commit message describing the change
  • A pointer to the previous commit (parent)

Real-life analogy: Think of a commit as saving a video game progress. The exact state of the game is saved at that moment. If something goes wrong later, the game can be loaded back to that saved point.

Syntax

git commit -m "Your commit message here"

The -m flag stands for "message" — it allows typing the commit message directly in the terminal.

Basic Commit Example

# Step 1: Create and stage a file
echo "Welcome to my site" > index.html
git add index.html

# Step 2: Commit with a message
git commit -m "Add index.html with welcome message"

# Output:
[main (root-commit) a3f92b1] Add index.html with welcome message
 1 file changed, 1 insertion(+)
 create mode 100644 index.html

The output tells us: the commit was made on the main branch, the short commit ID is a3f92b1, and 1 file was changed.

Writing a Good Commit Message

A good commit message clearly explains what was done and why. This helps teammates (and future self) understand the history of the project.

Guidelines for a Good Commit Message

  • Start with a capital letter
  • Use present tense — "Add feature" not "Added feature"
  • Keep it short — ideally under 72 characters
  • Be specific — "Fix login button not responding on mobile" is better than "Fix bug"

Good vs Bad Commit Messages

Bad MessageGood Message
fixFix null pointer error in user login function
changesUpdate homepage layout for mobile screens
asdfAdd contact form with email validation
wipAdd initial structure for product listing page

Commit with a Detailed Multi-line Message

For important changes, a multi-line commit message can be written. Running git commit without -m opens the configured text editor:

git commit

In the editor, a message like this can be written:

Add user registration form

- Added name, email, and password fields
- Added client-side validation
- Styled with basic CSS

The first line is the short summary. A blank line separates it from the detailed description below.

Stage and Commit in One Step

For files that are already tracked (previously committed at least once), both staging and committing can be done in a single command using the -a flag:

git commit -am "Update homepage text"

Note: The -a flag does NOT work on brand new (untracked) files. Those must be staged with git add first.

Amending the Last Commit

If a mistake was made in the last commit — either in the message or by forgetting to include a file — it can be fixed using --amend:

Fix the Commit Message Only

git commit --amend -m "Corrected commit message"

Add a Forgotten File to the Last Commit

git add forgotten-file.txt
git commit --amend --no-edit

The --no-edit flag keeps the existing commit message unchanged.

Important: Only amend commits that have NOT been pushed to a remote repository. Amending a pushed commit causes problems for other collaborators.

Viewing Commit Information

After committing, the details of the latest commit can be viewed with:

git log --oneline

Sample output:

a3f92b1 Add index.html with welcome message
b7c3d45 Initial commit

Empty Commits

In rare cases (e.g., triggering a CI/CD pipeline), an empty commit with no changes can be made:

git commit --allow-empty -m "Trigger deployment"

The Full Commit Workflow

# 1. Create/modify files
echo "About Us" > about.html

# 2. Check status
git status
# Output: Untracked files: about.html

# 3. Stage the file
git add about.html

# 4. Confirm staging
git status
# Output: Changes to be committed: new file: about.html

# 5. Commit with a message
git commit -m "Add about.html page"

# 6. Confirm clean state
git status
# Output: nothing to commit, working tree clean

Summary

A commit is a permanent snapshot of staged changes saved to the repository. Every commit needs a meaningful message that describes what changed. The most common form is git commit -m "message". Use --amend to fix the most recent commit before pushing. Commits form the entire history of the project and can always be referred back to.

Leave a Comment

Your email address will not be published. Required fields are marked *