You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改git log中提交哈希的显示颜色?含自定义格式与默认场景

调整Git Log中提交哈希的颜色:两种场景的解决方案

Got it, let's break down your two Git log color needs clearly—no overcomplicating things, and we'll keep your preferred formats intact where possible.

1. 自定义Git Log格式时修改提交哈希颜色

When building a custom log format, you can directly embed color controls right into the format string using Git's %C() syntax. This lets you target just the commit hash without affecting other parts of the log.

  • Basic syntax: Wrap the hash placeholder (%h for short hash, %H for full hash) with %C(your-color) to set the color, and %Creset to revert back to the default color for subsequent content.
  • Example command:
    git log --format="%C(green)%h%Creset %an - %s"
    
    This will make the short hash green, followed by the author name and commit subject in your default terminal colors.
  • Save as an alias (for reuse):
    If you want to use this format regularly, set up a global alias:
    git config --global alias.mylog "log --format='%C(blue bold)%h%Creset %ad %an: %s' --date=short"
    
    Now you can just run git mylog to get your colored, custom log output.

2. 修改默认--pretty=medium(及其他内置格式)的提交哈希颜色

If you don't want to switch to a custom format (and avoid redundancy/breaking other built-in formats), you can tweak Git's core color configuration to change the commit hash color across all log formats.

Git uses the color.log.commit config variable to control the color of commit hashes in log output. Here's how to set it:

  • Set a global color (applies to all repos):
    git config --global color.log.commit "magenta"
    
  • Use a hex color or styled color:
    You can also use hex codes or add style modifiers like bold or dim:
    # Bold orange using hex code
    git config --global color.log.commit "bold #ff7f00"
    # Dim blue
    git config --global color.log.commit "dim blue"
    
  • Verify it works:
    Just run your usual git log --pretty=medium command—your commit hashes should now show up in the color you set, without changing any other part of the medium format.

Note: Make sure Git's color output is enabled (it's on by default for most setups). If not, run git config --global color.ui auto to turn it on.

内容的提问来源于stack exchange,提问作者cjs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:35:25