Fun with Git log & Unix commands
Git log is a pretty powerful tool if you want to look back and see your or somebody elses work. If you combine this tool with Unix command line tools such as pipes and xargs you can come up with some pretty powerful tools for searching the history.
Finding all the contributors
Say you wanted to see all of the authors for a given git repo, you could use git log and pipe it to sort
and then uniq
.
Exercise: Ruby contributors
From the command line clone the ruby git repo into a local directory and cd in to the folder. Now run:
git log --pretty=format:%an | sort | uniq
To give you a list of unique contributors to the project.
Now we want to count the total number of contributors, just add in wc -l
.
git log --pretty=format:%an | sort | uniq | wc -l
Searching the repo
If you want to search a git repo for an instance of something that you or a specific person has changed, you can do so with a simple command as follows:
git log -S aria --author='stevemartin'
Say I want to see the diff’s for each commit that the command above returned, we can do this with unix pipes like so:
git log --full-diff -S radio --author='stevemartin' --pretty=format:%H | xargs git show
Now, say I want to see all that match data but with the match highlighted, I can use a tool like ag
GIT_MATCH=col; git log --full-diff -S $GIT_MATCH --author='stevemartin' --pretty=format:%H | xargs git show | ag $GIT_MATCH --passthru
Or, say that I want to edit the output in a file, I can even pipe it to Vim!
GIT_MATCH=col; git log --full-diff -S $GIT_MATCH --author='stevemartin' --pretty=format:%H | xargs git show | vim -R -
Looking at a specific function or class
git log -L '/ def initialize/',/\^\ \ \ \ end/:lib/drb/acl.rb
Finding common ancestors
git merge-base
git rev-list develop..HEAD