Commit
A snapshot of changes to files in a repository at a specific point in time, accompanied by a message describing what changed and why.
Detailed Explanation
A commit is Git's fundamental unit of work. Each commit has a unique SHA-1 hash, a pointer to the parent commit(s), a timestamp, the author, and the complete state of changed files. Commits form a directed acyclic graph (DAG), creating a complete history of the project.
Good commit messages are essential for maintainable history. They should be concise (50 chars for subject), descriptive, and explain why the change was made (not just what). Atomic commits—each commit containing one logical change—make code review, debugging, and reverting easier.
Why It Matters
Commits create the history of your project. Good commit practices make code review, debugging, and collaboration significantly easier.
Real-World Example
A developer makes three commits: "add password hashing with bcrypt", "implement user registration endpoint", and "add email validation middleware". Each commit is atomic, descriptive, and can be reverted independently.
When to Use
Commit frequently—ideally after each logical change. Small, focused commits are easier to review, understand, and revert than large monolithic commits.
Advantages
- Creates a complete project history
- Enables reverting changes safely
- Supports code review and collaboration
- Provides context through commit messages
- Enables bisecting to find bugs
Disadvantages
- Poor commit messages make history useless
- Large commits are hard to review and revert
- Interactive rebase can be confusing
- Commit history can become cluttered
- Force-pushing can destroy history
Related Terms
Frequently Asked Questions
How often should I commit?
Commit after every logical change. A good rule: if you can't describe the change in one sentence, it should be multiple commits. Aim for commits that compile, pass tests, and represent one coherent change.
What makes a good commit message?
Use the imperative mood ("add" not "added"), keep the subject under 50 characters, explain what and why (not how), and reference issues when relevant. Example: "Add rate limiting to API endpoints (fixes #123)".
What is an atomic commit?
An atomic commit contains one logical change that compiles, passes tests, and can be reverted independently. If you fix a bug and refactor nearby code, those should be separate commits.
Can I change a commit after pushing?
You can use `git commit --amend` to change the last commit, or `git rebase -i` to edit older commits. However, if others have already pulled those commits, you should create new commits instead of rewriting history.
What is a commit hash?
A commit hash is a 40-character SHA-1 identifier (abbreviated to 7-8 characters) that uniquely identifies each commit. It is used for referencing specific commits in commands, pull requests, and documentation.