git commit saves your staged changes to the local repository as a permanent snapshot. It creates a new commit object with a unique identifier based on the changes made, along with a message that describes them. This is the fifth post in this site’s Git command guide, following Git Add . (All).
Basic Usage
Here’s how to use git commit in the typical workflow:
- After making changes to your files, add them to the staging area with
git add. - Once you’ve added all the changes you want to commit, run the
git commitcommand followed by a-mflag and a message inside quotes describing the changes made. For example:
git commit -m "Added new feature to login page"
- The commit is now saved to your local repository with a unique identifier, allowing you to easily track and revert to previous versions of your code.
It’s important to note that committing changes locally only saves them to your local Git repository. To share the changes with others or push them to a remote repository, you’ll need to run git push after committing.
How git commit Works
- Every commit records a snapshot of the staging area at that moment, not a diff. Git stores it as a tree of objects pointing at blobs (file contents), a parent commit hash, and the metadata you supply (author, timestamp, message).
- The
-mflag lets you write the commit message inline. Without it, Git opens your configured editor so you can write a multi-line message. - Committing does not affect remote repositories — that requires
git push, covered later in the series.
Useful Resources
See Also: This Site’s Git Command Guide
This post is part of a 13-part Git command guide on this site:
- Git Init — starting a brand-new repository
- Git Clone — copying an existing repository
- Git Add — staging specific files for a commit
- Git Add . (All) — staging all changes at once
- Git Status — checking what’s changed before you commit
- Git Branch — creating, listing, and renaming branches
- Git Checkout — switching branches and restoring files
- Git Push — sending your commits to a remote
- Git Pull — fetching and merging remote changes
- Git Log — reading commit history
- Git: Delete Last Commit — undoing a commit safely
- Git – Autocorrect Spelling — typo recovery and productivity config
Conclusion
git commit is the moment your staged changes become part of the permanent project history. With your work committed locally, the next thing to check is the overall state of your repository — covered in Git Status.