Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Git Tutorial

Git HOME Git Intro Git Install Git Config Git Get Started Git New Files Git Staging Git Commit Git Remove and Move Git Tagging Git Stash Git History Git Diff Git Blame Git Help Git Branch Git Merge Git Workflow Git Best Practices

Git and GitHub

GitHub Get Started Git What is SSH? GitHub Add SSH GitHub Set Remote GitHub Edit Code Pull from GitHub Push to GitHub GitHub Branch Pull Branch from GitHub Push Branch to GitHub GitHub Flow GitHub Pages Git GUI Clients

Git Contribute

GitHub Fork Git Clone from GitHub GitHub Send Pull Request

Git Undo

Git Revert Git Reset Git Clean Git Amend Git Rebase Git Reflog Git Recovery

Git Advanced

Git .gitignore Git .gitattributes Git Large File Storage (LFS) Git Signing Commits/Tags Git Cherrypick & Patch Git Bisect Git Merge Conflicts Git CI/CD Git Hooks Git Submodules Git Worktree Git Remote Advanced

Git and Bitbucket

Bitbucket Get Started Bitbucket Add SSH Bitbucket Set Remote Bitbucket Edit Code Bitbucket Branch Bitbucket Pages Bitbucket Fork Git Clone from Bitbucket Bitbucket Send Pull Request

Git and GitLab

GitLab Get Started GitLab Add SSH GitLab Set Remote GitLab Edit Code GitLab Branch GitLab Pages GitLab Fork Git Clone from GitLab GitLab Send Pull Request

Git Cert

Git Certificate

Git Reference

Git Glossary

Git Exercises

Git Exercises Git Challenges Git Quiz Git Syllabus Git Study Plan

Git Commit


Share

What is a Commit?

A commit is like a save point in your project.

It records a snapshot of your files at a certain time, with a message describing what changed.

You can always go back to a previous commit if you need to.

Here are some key commands for commits:

  • git commit -m "message" - Commit staged changes with a message
  • git commit -a -m "message" - Commit all tracked changes (skip staging)
  • git log - See commit history

How to Commit with a Message (-m)

To save your staged changes, use git commit -m "your message":

Example

git commit -m "First release of Hello World!"
[main (root-commit) 712c39a] First release of Hello World!
 3 files changed, 17 insertions(+)
 create mode 100644 README.md
 create mode 100644 bluestyle.css
 create mode 100644 index.html
Try it Yourself »

Always write a clear message so you and others can understand what changed.



Commit All Changes Without Staging (-a)

You can skip the staging step for already tracked files with git commit -a -m "message".

This commits all modified and deleted files, but not new/untracked files.

Example

git commit -a -m "Quick update to README"
[main bbb2392] Quick update to README
 1 file changed, 1 insertion(+)
Try it Yourself »

Warning: Skipping the staging step can make you include unwanted changes. Use with care.

Note: git commit -a does not work for new/untracked files. You must use git add <file> first for new files.

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

git commit -a -m "Try to commit new file"
On branch main

Initial commit

Untracked files:
  (use "git add &lt;file&gt;..." to include in what will be committed)
        index.html

nothing added to commit but untracked files present (use "git add" to track)
Try it Yourself »

Write Multi-line Commit Messages

If you just type git commit (no -m), your default editor will open so you can write a detailed, multi-line message:

Example

git commit

Write a short summary on the first line, leave a blank line, then add more details below.


Commit Message Best Practices:

  • Keep the first line short (50 characters or less).
  • Use the imperative mood (e.g., "Add feature" not "Added feature").
  • Leave a blank line after the summary, then add more details if needed.
  • Describe why the change was made, not just what changed.

Other Useful Commit Options

  • Create an empty commit:
    git commit --allow-empty -m "Start project"
  • Quickly add staged changes to last commit, keep message:
    git commit --amend --no-edit

Troubleshooting Common Commit Mistakes
  • Forgot to stage a file?
    If you run git commit -m "message" but forgot to git add a file, just add it and commit again. Or use git commit --amend to add it to your last commit.
  • Typo in your commit message?
    Use git commit --amend -m "Corrected message" to fix the last commit message.
  • Accidentally committed the wrong files?
    You can use git reset --soft HEAD~1 to undo the last commit and keep your changes staged.

View Commit History (git log)

To view the history of commits for a repository, you can use the git log command:

Example

git log
commit dceae35c9d24836eb69ce58671132960f2b0576e (HEAD -&gt; main)
Author: W3Schools &lt;test@w3schools.com&gt;
Date:   Wed Jan 15 10:00:00 2025 +0000

    Updated index.html with a new line

commit 712c39a18de4a88c3ea87acf90c6246a69009bf6
Author: W3Schools &lt;test@w3schools.com&gt;
Date:   Wed Jan 15 10:00:00 2025 +0000

    First release of Hello World!
Try it Yourself »

For a shorter view, use git log --oneline:

Example

git log --oneline
dceae35 (HEAD -&gt; main) Updated index.html with a new line
712c39a First release of Hello World!
Try it Yourself »

To see which files changed in each commit, use git log --stat:

Example

git log --stat
commit dceae35c9d24836eb69ce58671132960f2b0576e (HEAD -&gt; main)
Author: W3Schools &lt;test@w3schools.com&gt;
Date:   Wed Jan 15 10:00:00 2025 +0000

    Updated index.html with a new line

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

commit 712c39a18de4a88c3ea87acf90c6246a69009bf6
Author: W3Schools &lt;test@w3schools.com&gt;
Date:   Wed Jan 15 10:00:00 2025 +0000

    First release of Hello World!

 README.md     |  2 ++
 bluestyle.css |  3 +++
 index.html    | 12 ++++++++++++
 3 files changed, 17 insertions(+)
Try it Yourself »



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.