Commit
Коммит
A saved version of a project in Git’s history. It records the files you prepared for saving, along with a message and details about who made the change.
What is a commit?
A commit gives you a version of a project that you can refer back to. Say you fix a bug in an email validator and add a test for it. You can commit the fix and test together, then keep editing those files without changing the version you just recorded.
What Git records
A plain git commit records what’s in the staging area, also called the index. Think of the index as the proposed next snapshot. When you run git add on a file, Git stages its contents as they are at that moment. If you edit the file again, Git doesn’t automatically stage the new edits. Use git add -p when you want to stage only selected changes within a file.
A commit points to a snapshot of the files included in that version, not just the ones that changed. Git can reuse stored content for unchanged files, so a snapshot doesn’t mean a fresh copy of everything on disk. A commit also contains metadata such as the author, timestamps, and a message. Most commits point to one parent; a root commit has none, and a merge commit has more than one. The commit ID is a hash of the commit object, so even changing just the message produces a different ID.
Example: save one change at a time
Suppose you update the setup instructions in README.md while also working on an unrelated bug. If nothing is staged yet, you can commit just the documentation update:
git add README.md
git diff --cached
git commit -m "Document local setup"
git show HEAD
Check git diff --cached before committing: it compares what’s staged with the current commit and will also reveal any unrelated edits you accidentally staged. git show HEAD then displays the new commit and, for this ordinary commit, its diff against its parent. The unstaged bug fix stays in your working tree.
How teams use commits
A commit is easier to review when it captures one logical change. A bug fix and its regression test belong together; unrelated formatting usually doesn’t. A message like Reject blank email addresses tells the next reader what to expect without making them read the diff first. Teams also use commit IDs to tie a build or test result to the exact version it ran against.
Don’t confuse committing with pushing. A commit is recorded locally; git push sends any objects the remote needs and updates a reference there. A branch is a reference to a commit, not the commit itself. When you commit on a checked-out branch, Git advances that branch to the new commit.
What comes up in interviews
You may be asked to predict what gets recorded when a file is edited after staging. Another common scenario is undoing a commit that teammates already have. For an ordinary commit on a shared branch, explain why you’d generally use git revert: it adds a new commit that undoes the change rather than rewriting history others are using.
“Commit” questions from real interviews
Asked at 83 companies