A Git commit is an immutable snapshot of the staged changes in a repository at a specific point in time, recorded with a unique identifier and metadata describing who made the change and why.
Key Points: • Each commit is identified by a SHA-1 hash computed from its content, parent commit(s), author, and message, giving it a unique fingerprint. • A commit stores author, committer, timestamp, a reference to its parent commit(s), and a message describing the change. • Commits form a directed acyclic graph, with each one pointing back to its parent, which is how Git reconstructs history. • Only staged changes (added with git add) are included in a commit; unstaged changes are left out. • Good practice is to keep commits small and focused on a single logical change, with a clear, descriptive message.
Example: After editing a file and staging it with git add UserService.java, running git commit -m "Add null check to getUser" records a snapshot of that file along with the message, author, and timestamp.
Code Example:
git add UserService.java
git commit -m "Add null check to getUser"Interview Tip: A concise interview answer is:
"A commit is a snapshot of staged changes, identified by a unique hash and recorded with the author, timestamp, and a message. Commits link back to their parent, which is how Git builds up the full project history over time."