
Git log and history commands: Explore repository history using compact logs, patch views, per-line attribution and reflogs for local recovery. This guide explains what each command does, its syntax, practical examples, useful options, and common mistakes. Commands are shown for typical installations; flags can vary across operating-system and tool versions.
Before you start
Open a terminal appropriate for the operating system shown in the examples. Check that the required tool is installed and that you are running it with the permissions needed for your task. Replace sample names, paths, hostnames and IDs with values from your own environment.
Syntax at a glance
Square brackets usually indicate optional arguments, while uppercase words such as FILE, HOST and PACKAGE are placeholders that you replace. Keep quotes around paths or text that contains spaces. Commands should be tested on a safe sample before being used in production.
Quick command reference
| Command | What it does | Example | Useful options |
|---|---|---|---|
git log --oneline | Show compact commit history | git log --oneline --decorate -n 15 | -n limits commits; --graph visualizes branch structure |
git log --stat | Show commit history with file-change summaries | git log --stat -- path/to/file | Path filtering narrows history to a file or directory |
git show | Inspect one commit | git show HEAD~1 | HEAD~1 refers to the first parent commit |
git blame | See which commit last changed each line | git blame -L 20,45 src/app.js | -L limits line range; -w ignores whitespace-only changes |
git reflog | Review local branch/HEAD movement history | git reflog --all -n 20 | --all includes reflogs across refs |
Command syntax and examples explained
1. git log --oneline
Purpose: Show compact commit history.
Syntax
git log --onelineExample
git log --oneline --decorate -n 15Options and notes: -n limits commits; --graph visualizes branch structure.
The current branch and its ancestors are shown unless a different revision is specified.
2. git log --stat
Purpose: Show commit history with file-change summaries.
Syntax
git log --statExample
git log --stat -- path/to/fileOptions and notes: Path filtering narrows history to a file or directory.
Renames can affect historical path lookup.
3. git show
Purpose: Inspect one commit.
Syntax
git show COMMITExample
git show HEAD~1Options and notes: HEAD~1 refers to the first parent commit.
Use explicit hashes when you need a reproducible reference.
4. git blame
Purpose: See which commit last changed each line.
Syntax
git blame FILEExample
git blame -L 20,45 src/app.jsOptions and notes: -L limits line range; -w ignores whitespace-only changes.
Blame shows change history, not intent or personal responsibility by itself.
5. git reflog
Purpose: Review local branch/HEAD movement history.
Syntax
git reflogExample
git reflog --all -n 20Options and notes: --all includes reflogs across refs.
Useful during recovery, but it is local and not a permanent backup.
Common mistakes and troubleshooting
- Command not found: confirm the tool is installed and available on your PATH; open a new terminal after changing PATH.
- Permission denied: verify the current account, file permissions and target path. Use administrator/root privileges only when genuinely required.
- Unexpected output: check the command version, quoting, current directory and exact arguments. Consult the local help or manual for options supported by your build.
- Destructive operations: preview the target and keep a backup before deleting data, rewriting history, flashing a partition or changing system configuration.
Safety tips
Review every command before running it, especially commands that delete files, change permissions, flash firmware, alter a database or expose a network service. For network scanning and remote administration, work only on devices and systems you own or are authorized to test. Keep credentials, private keys, access tokens and log files private.
Frequently asked questions
Do these commands work on every system?
Not always. Some commands are specific to Windows, Linux, Android, a particular package manager or an installed tool version. Use the syntax shown for your platform and check local help when a flag is unavailable.
Can I copy and run every example exactly?
Use the examples as templates. Replace placeholders such as filenames, package IDs, hostnames and paths with your own values, and verify the effect before executing a command that changes data or system state.
Conclusion
Use this reference as a starting point, then confirm flags in the tool’s official help or manual for your installed version. A clear understanding of syntax, options and side effects makes command-line work more reliable.