In Git, you can display a pretty branch graph by using different parameters with the git log command in the command line. Here are several methods:
Basic Branch Graph
The simplest branch graph can be generated with the following command:
shgit log --graph --oneline --all
- The
--graphparameter displays an ASCII art representation of the branch graph. - The
--onelineparameter shows each commit on a single line for a more compact output. - The
--allparameter shows all branches, not just the current one.
Branch Graph with More Information
If you want to display additional details such as the author's name and commit date in the branch graph, you can use:
shgit log --graph --oneline --all --decorate
- The
--decorateparameter adds pointers to branch names, tags, and other references.
Customized Branch Graph
You can customize the output format using --pretty=format:. For example:
shgit log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all
- The format string allows customization of colors, commit hashes, branch names, commit messages, commit dates, and author names.
%Cred,%Creset,%C(yellow), etc., are used to define color schemes.%hshows the abbreviated commit hash.%dshows refnames (branch names, tags).%sshows the commit message.%crshows the relative time (e.g., '3 days ago').%anshows the author's name.--abbrev-commitshortens the hash length.
Using Aliases
To avoid lengthy commands, set aliases in Git. For example, create an alias named graph:
shgit config --global alias.graph "log --graph --oneline --all --decorate"
Then, simply use git graph to display the pretty branch graph.
Example
Here is an illustrative example of the output when using git log --graph --oneline --all --decorate in a repository with multiple branches:
sh* e2a6f7b (HEAD -> master, origin/master, origin/HEAD) Merge pull request #2 from feature/xyz | | * 4e5c111 (feature/xyz) Added XYZ feature | * 5f4e3d2 Improved XYZ feature * | 9c0f3f9 Modified README |/ * c1f2e9e Initial commit
This simple tree structure shows the commit order and branch relationships. Each * and | character forms ASCII art representing commits and branches. The leftmost lines indicate the direct history of the current branch, while the right lines represent commits from other branches.