乐闻世界logo
搜索文章和话题

How to output pretty git branch graphs?

2个答案

1
2

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:

sh
git log --graph --oneline --all
  • The --graph parameter displays an ASCII art representation of the branch graph.
  • The --oneline parameter shows each commit on a single line for a more compact output.
  • The --all parameter 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:

sh
git log --graph --oneline --all --decorate
  • The --decorate parameter adds pointers to branch names, tags, and other references.

Customized Branch Graph

You can customize the output format using --pretty=format:. For example:

sh
git 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.
  • %h shows the abbreviated commit hash.
  • %d shows refnames (branch names, tags).
  • %s shows the commit message.
  • %cr shows the relative time (e.g., '3 days ago').
  • %an shows the author's name.
  • --abbrev-commit shortens the hash length.

Using Aliases

To avoid lengthy commands, set aliases in Git. For example, create an alias named graph:

sh
git 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.

2024年6月29日 12:07 回复

For text output, you can try:

shell
git log --graph --abbrev-commit --decorate --date=relative --all

Or:

shell
git log --graph --oneline --decorate --all

Alternatively, you can use the alias for drawing the DAG graph with Graphviz, as described at this link.

2024年6月29日 12:07 回复

你的答案