Skip to content

Instantly share code, notes, and snippets.

@bdunagan
Created November 13, 2011 19:05
Show Gist options
  • Save bdunagan/1362507 to your computer and use it in GitHub Desktop.
Save bdunagan/1362507 to your computer and use it in GitHub Desktop.
git log with colors and lines changed
# See colored 'git log' with lines changes in Terminal on Mac
# => * 1234567 - 2001-01-01 - NNNNN - description <user>
# Based on http://www.jukie.net/bart/blog/pimping-out-git-log
# git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
# To install as 'gl' alias (not overriding 'git log'):
# 1) save this as ~/.gitlog.rb
# 2) add to ~/.bash_profile: 'alias gl="ruby ~/.gitlog.rb | more -r"'
# Get the pretty log with spaces for the lines changes.
commits_cmd = "git log --graph --pretty=format:'%Cred%h%Creset - %Cgreen%ad - -%C(yellow)%d%Creset %s %C(bold blue)<%an>%Creset' --abbrev-commit --date=short"
commits=%x[#{commits_cmd}]
# Iterate over each commit to replace spaces with lines changed.
commits.split("\n").each do |commit|
# Get the current commit's hash.
hash = /([0-9a-f]{7})/.match(commit)
# Get the lines changes for this commit.
lines_cmd = "git log #{hash} --shortstat -n 1 | sed -n -e 's/\\([0-9]*\\) files changed, \\([0-9]*\\) insertions(+), \\([0-9]*\\) deletions(-)/\\2 \\3/p' | awk '{printf \"%6i\",($1 + $2)}'"
lines = %x[#{lines_cmd}]
# Replace the spaces with the lines changed.
commit = commit.gsub(" ", lines) if !lines.empty?
# Print out the new commit log.
puts commit
end
@bdelespierre
Copy link

Cool script thanks 👍 I can't see colors tho 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment