How do you view the commit history in a repository?
You can view the commit history in Git in different ways, but the main and most important one is the git log command.
Let's go from the simplest to the more practical.
1. Basic way - git log
git logThis command shows:
- the list of commits from the newest to the oldest
- the commit hash
- the author
- the date
- the commit message
This is the classic interview answer.
2. A short and convenient history
In real work, a shortened form is often used:
git log --onelineIt shows:
- the short hash
- the commit message one commit per line
Convenient when the history is large.
3. History with branches and merges
To see where branches and merges happened, use:
git log --graph --oneline --allHere:
--graphdraws the branching diagram--allshows all branches, not only the current one
It is a plus at an interview if you say that Git history is a graph, and it can be visualized.
4. History for a specific file
You can see how a single file changed:
git log file.txtThis is useful if you need to understand:
- who changed the file
- when the change appeared
- in which commit
5. Viewing the changes themselves
If you need to see what exactly changed, add:
git log -pGit will show the diff for each commit.
6. Limiting the history
Examples that interviewers like:
git log -5 # last 5 commits
git log --author=Ivan
git log --since="2024-01-01"Short answer for an interview
The commit history in Git is viewed with the
git logcommand. It shows all commits of the current branch with the author, date, and message. For convenience,git log --onelineandgit log --graphare often used.
A common mistake candidates make
"History can only be viewed in an IDE or on GitHub"
Git stores history locally, and git log works without an internet connection.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.