1. What is a Commit?
A commit is a saved snapshot of staged changes in the local Git repository.
git add .
git commit -m "Added login feature"
Flow:
Working Directory
↓ git add
Staging Area
↓ git commit
Local Repository
Each commit contains information such as the changes, author, date/time, message, and a unique commit hash.
Interview Answer
A commit is a snapshot of staged changes saved in the local Git repository. It creates a point in the project history that we can track or return to later.
2. What is a Commit Hash?
A commit hash is a unique identifier generated by Git for each commit.
Example:
a4f82c91d5e7...
View commits using:
git log
You can use the hash to identify a specific commit:
git show a4f82c9
Key Point
Commit
├── Hash
├── Author
├── Date
├── Message
└── Changes
Interview Answer
A commit hash is a unique identifier for a Git commit. We use it to identify, inspect, compare, or reference a particular commit in the repository history.
3. What is HEAD in Git?
HEAD is a reference to the commit currently checked out. Normally, it points through the current branch to that branch's latest commit.
Example:
A ← B ← C
↑
main
↑
HEAD
Here, HEAD indicates that we are currently on main, whose latest commit is C.
Check the current branch:
git status
Important Point
After a new commit:
Before: A ← B ← C ← HEAD
|
main
After: A ← B ← C ← D ← HEAD
|
main
Interview Answer
HEAD represents the currently checked-out position in Git. Normally, it points to the current branch, which in turn points to its latest commit. When we create a new commit, the current branch and HEAD move forward to that commit.