Commit
In short
A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
What is a commit in Git?
A commit records the state of your project at a specific moment. Each commit stores a snapshot of the tracked files, who made it, when it was made, a message explaining the change, and a link to the commit or commits that came before it. Chained together through those links, commits form the project's history.
Before committing, you choose which changes to include by adding them to the staging area with git add. Running git commit then saves exactly those staged changes. Every commit gets a unique identifier called a hash, a long string of letters and digits, which you can use to view, compare, or revert that exact version.
Commits work like save points in a document's history, each with a short note explaining what was done. Good commits are small and focused on one logical change, with a clear message like 'Fix login redirect loop' rather than 'updates'. This makes the history easy to read, review, and undo.
Committing is not the same as sharing. A commit exists only in your local repository until you run git push to send it to a remote repository. The word commit is also used in databases, where it means making a transaction's changes permanent.
At a glance
Key takeaways
- A commit is a snapshot of staged changes plus metadata: author, date, message, and parent.
- Each commit has a unique hash that identifies it.
- Commits stay local until you push them to a remote repository.
- Small, focused commits with clear messages make history easy to understand.
Example
# See what has changed
git status
# Stage a specific file for the next commit
git add src/login.ts
# Save the staged changes with a descriptive message
git commit -m "Fix login redirect loop"
# View the three most recent commits (short hash and message)
git log --oneline -3
# Reword the last commit's message (only before pushing)
git commit --amend -m "Fix redirect loop after login"Readers ask
How do I undo a commit in Git?
To undo a commit that has already been shared, use git revert <hash>, which creates a new commit that reverses it. To undo your most recent local commit while keeping its changes in your files, use git reset --soft HEAD~1.
What makes a good commit message?
A good commit message starts with a short summary line, around 50 characters, written as a command, such as 'Add password reset email'. If more context is needed, add a blank line followed by a longer explanation of why the change was made.
What is the difference between git commit and git push?
git commit saves a snapshot in your local repository. git push uploads your local commits to a remote repository so others can see them.
See also
- GitVersion Control, p. 10Git is a free, open-source distributed version control system that tracks changes to files over time, so developers can collaborate and undo mistakes.
- BranchVersion Control, p. 2A branch in Git is an independent line of development that lets you work on a feature or fix in isolation, without affecting the main code until you merge it.
- MergeVersion Control, p. 29A merge in Git combines the changes from one branch into another, joining separate lines of development back together into a single, shared history.
- RebaseVersion Control, p. 33A rebase in Git replays a branch's commits on top of another commit, rewriting history to produce a cleaner, linear sequence of changes without merge commits.
- Pull RequestVersion Control, p. 32A pull request is a proposal to merge changes from one branch into another, giving teammates a place to review, discuss, and test the code before it is merged.
- TransactionDatabases, p. 47A transaction is a group of database operations that succeed or fail as a single unit, so the data is never left in a half-finished, inconsistent state.
- Conventional CommitsVersion Control, p. 6Conventional Commits is a specification for structured commit messages like feat: add search, so tools can write changelogs and pick versions automatically.
Sources
Spotted a mistake or something missing on this page?Suggest an edit