| You can absolutely view a Git commit graph in ASCII art directly within your terminal for recent commits. This is a powerful feature of git log that helps visualize branching, merging, and commit history. Basic Command ExplanationThe core command to generate an ASCII graph uses the --graph option with git log. To make it more concise and readable, it's often combined with other options: git log --graph --oneline --decorate --all
--graph: Instructs Git to draw the commit graph in ASCII.--oneline: Condenses each commit entry to a single line, showing the shortened SHA-1 hash and the commit subject. This significantly improves readability for the graph.--decorate: Shows branch and tag names pointing to specific commits. This is crucial for understanding where your HEAD, branches, and tags are located in the graph.--all: By default, git log only shows the history of the current branch. --all includes commits from all local and remote-tracking branches, providing a complete picture of your repository's history.
Its result will be like below: 
Limiting CommitsTo focus on "recent commits," you can use various options with git log: git log --graph --oneline -n 10 # Shows the last 10 commits
git log --graph --oneline --since="2 weeks ago" # Commits within the last two weeks
git log --graph --oneline --after="2023-01-01" # Commits after a specific date
git log --graph --oneline HEAD~5..HEAD # Shows the last 5 commits from HEAD
This shows commits reachable from HEAD but not from HEAD~5. Customizing Output FormatFor even more control over what information is displayed alongside the graph, you can use the --pretty=format: option. This allows you to define a custom format string using placeholders. Common placeholders: %h: Abbreviated commit hash%H: Full commit hash%d: Ref names (branches, tags)%s: Subject%cr: Relative commit date (e.g., "2 weeks ago")%an: Author name%C(...): Color options
Example for a detailed custom format with color: git log --graph --pretty=format:'%C(yellow)%h%Creset -%C(auto)%d%Creset %s %C(green)(%cr) %C(blue)<%an>%Creset' --all
This format would output: - Yellow commit hash
- Auto-colored ref names (branches/tags)
- Subject
- Green relative commit date
- Blue author name
Creating Git AliasesTyping these long commands repeatedly can be cumbersome. Git aliases allow you to create shortcuts in your ~/.gitconfig file. Open or create ~/.gitconfig and add an [alias] section: [alias]
lg = log --graph --oneline --decorate --all
lga = log --graph --pretty=format:'%C(yellow)%h%Creset -%C(auto)%d%Creset %s %C(green)(%cr) %C(blue)<%an>%Creset' --all
lg10 = log --graph --oneline -n 10 --decorate
After saving, you can then simply type: git lg to see the full graph.git lga for the detailed, colored graph.git lg10 for the last 10 commits with the basic graph.
Putting It All Together (Examples)Here are some common and useful combinations: 1. Standard concise graph for all branches: git log --graph --oneline --decorate --all
(Alias: git lg) 2. Last 15 commits on the current branch (with decorations): git log --graph --oneline --decorate -n 15
3. Detailed, colored graph for all branches (alias lga): git log --graph --pretty=format:'%C(auto)%h%Creset %C(bold yellow)%d%Creset %s %C(green)(%cr) %C(bold blue)<%an>%Creset' --all
4. Graph showing only commits since a specific date on all branches: git log --graph --oneline --decorate --all --since="2023-10-01"
5. Graph showing only your own commits: git log --graph --oneline --decorate --all --author="Your Name"
Using these options and aliases, you can tailor your terminal's Git graph view to precisely what you need to see, making it an invaluable tool for understanding your repository's history. |