Git Protip: Learning from your history (git log)
I've been sending weekly "Protip" emails about Git to the rest of engineering here at Slide for a while now, using the "Protips" as a means of introducing more interesting and complex features Git offers. Below is the second Protip written to date.
One of the major benefits to using Git is the entirety of the repository being entirely local and easily searched/queried. For this, Git has a very useful command called git log which allows you to inspect revision histories in numerous different ways between file paths, branches, etc. There are a couple basic scenarios where git log has become invaluable, for me at least, in order to properly review code but also to track changes effectively from point A to point B.
- What's Dave been working on lately? (with diffs)
git log -p --no-merges --author=dave
The --no-merges option will prevent git log from displaying merge commits which are automatically generated whenever you pull from one Git branch to another
- Before I merge this branch down to my team master, I want to know what files have been changed and what revisions
git log --name-status master-topfriends...proj-topfriends-thing
Git supports the ability with git log and with git diff to provide unidirectional branch lookups or bidirectional branch lookups. For example, say the left branch has commits "A, B" and the right branch has commits "A, C". The ... syntax will output "C", whereas .. will output "B, C"
- I just got back from a vacation, I wonder what's changed?
git log --since="2 weeks ago" --name-status -- templates
At the tail end of a git log command you can specify particular paths to look up the histories for with the -- operator, in this case, I will be looking at the changes that have occured in the templates directory over the past two weeks
- Most recent X number of commits? (with diffs)
-
git log -n 10 --no-merges -p
-
All git log commands automatically filter into less(1) so you can page through the output like you would normally if you executed a svn log | less. Because git log is simply reading from the locally stored revision history you can quickly grep the history by any number of different search criteria to gain a better understanding of how the code base is changing and where.
For more specific usage of `git log` refer to the git log man page
Did you know! Slide is hiring! Looking for talented engineers to write some good Python and/or JavaScript, feel free to contact me at tyler[at]slide
Comments
Extent of log detail
Hi there.
Nice article, I wonder if you could answer a quick question? I am looking at moving to a Git repo from SVN, but need to understand something: I find the SVN log command useful to measure the project evolution, but that requires an *absolutely complete* log. When someone (say, Bob) clones a Git repo and makes a number of commits to it, then merges it back to the more authoritative Git repo, will the "git log" command, when used with the authoritative repo, be able to report details of all of Bob's commits, or will they show up just a single commit?
Thanks
Re: Extent of log detail
What `git log` prints will be the extent of the history it is aware of for your branch. Commits are atomic entities, so if you have A,B,C and you merge in D,E from Bob's repository, your `git log` will print out A,B,C,D,E
Nice tips, thanks! I wonder,
Nice tips, thanks!
I wonder, though, if you have '..' and '...' the wrong way around in the second part? I think in your example, '...' would show "B, C", and '..' would show 'C'.
Cheers,
Kevin