Git log and git show: Explore a Project's History
History is the reason to use Git. Once a project has a few commits, git log lets you answer "what changed recently?", "who wrote this?" and "when did this break?".
git log
Plain git log lists commits newest first, each with its full hash, author, date and message. For daily use, shorter views are easier:
git log --onelineputs each commit on one line: short hash and summary.git log -n 3limits the output to the three latest commits.git log --statadds the files each commit touched.git log --author="Ethan"andgit log --grep="login"filter by author or by words in the message.git log -- path/to/fileshows only commits that changed that file.
When the list is longer than the screen, Git opens it in a pager; press q to get back to the prompt.
Hashes and HEAD
Every commit is identified by a 40-character hash such as 3f2a9c1.... You can refer to a commit by the first seven or so characters, as long as they are unique in the repository.
HEAD is Git's name for "the commit you are on now", usually the latest commit of your current branch. HEAD~1 means one commit before it, and HEAD~2 two before. These relative names are often easier than copying hashes.
git show
git show displays one commit: its details plus the exact lines it changed. With no argument it shows HEAD. git show HEAD~1 shows the previous commit, and git show HEAD:notes.txt prints a file as it was in that commit.
git init -q
echo "v1" > app.txt
git add app.txt
git commit -q -m "Create app"
echo "v2" >> app.txt
git commit -q -am "Add second line"
echo "v3" >> app.txt
git commit -q -am "Add third line"
git log --oneline
git log -n 1 --format="%h %an: %s"
git show HEAD~1 --format="%s" --stat
git show HEAD~2:app.txt Transcript (verified by our test suite in a fresh temp folder)
$ git init -q $ echo "v1" > app.txt $ git add app.txt $ git commit -q -m "Create app" $ echo "v2" >> app.txt $ git commit -q -am "Add second line" $ echo "v3" >> app.txt $ git commit -q -am "Add third line" $ git log --oneline f862d68 Add third line 313ac69 Add second line 72d853c Create app $ git log -n 1 --format="%h %an: %s" f862d68 Ethan Cole: Add third line $ git show HEAD~1 --format="%s" --stat Add second line app.txt | 1 + 1 file changed, 1 insertion(+) $ git show HEAD~2:app.txt v1
The transcript makes three small commits, then explores them. The --format option chooses exactly what to print: %h is the short hash, %s the subject line and %an the author name.
Custom formats
--format (also written --pretty=format:) is handy in scripts and for quick reports. git log --format="%h %an %s" gives a compact table of who did what. Run git log --oneline --graph --all once you have branches to see them drawn as a tree.
Common mistakes
- Getting stuck in the pager. Press
q. - Confusing
HEAD~1(the parent commit) withHEAD(the current one). - Copying a hash that is too short to be unique in a big repository; seven characters is usually enough.
Summary
git log --onelineand-ngive a quick overview; filters narrow it down.HEADis the current commit;HEAD~1is its parent.git showreveals one commit's details and changes.
Quiz: Viewing history