rollen.io

Some useful git one-liners

2023-01-13

I have a few useful git one-liners that I reach for relatively often. I don't have them written down anywhere, but just search for them in my shell history (using the wonderful McFly) whenever I need them. Until now!

List the number of commits to a repo by day

 git log --date=short --pretty=format:%ad | sort | uniq -c
   8 2023-01-07
   2 2023-01-09
   1 2023-01-10
   2 2023-01-12
   4 2023-01-13

Create a sparkline chart of the commit history

Bonus! Requires spark

 git log --date=short --pretty=format:%ad | sort | uniq -c | awk '{print $1}' | spark

▁▄▃▁▁▆█▁▂▁▇▁▁▂▃▁▁▁█▂▁▂▄

List the number of commits to a repo by day, sorted by commits

 git log --date=short --pretty=format:%ad | sort | uniq -c | sort -r | head
   8 2023-01-07
   8 2021-10-19
   7 2022-04-23
   6 2021-10-18
   4 2023-01-13

Delete merged branches in a repo that squashes commits

If you work a lot in a repo that uses squashes merge commits to main, you might have found that your local git branch listing quickly builds up, and answers like this one don't actually work. Try this instead:

 git fetch -p ; git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -D
From github.com:SebRollen/test-repo
- [deleted]           (none)     -> origin/test-branch
Deleted branch test-branch (was f865934).

Let me know if you have any other good git tricks!

Comment via email Back to main page