Git In-depth Git Log
Transcript from the "Git Log" Lesson
>> Nina Zakharenko: To examine history, we can use git log, its the basic command that shows the history of your repository. The vanilla getlog isn't very helpful, I don't know how many of you use it by default. But, there's a lot of really cool features and flags, that help us navigate our repository a lot easier.
[00:00:19] For example, we can pass in --since, and pass on arguments like, since yesterday, since two weeks ago. I tend to use this most frequently, but git also has flags for before and after. For example, when I'm working, I can say, what work did I do in the past day?
[00:00:43] Or we noticed a bug in production 20 minutes ago, what commits went in since 20 minutes ago? We can also use the follow flag, that lets us log files that have been moved or renamed. If you move or rename a file git is great about knowing that that file has the same content.
[00:01:08] But when you rename files it can be easy to lose track of them, especially when they get moved around frequently. If you try to do a basic git blame on the newly renamed file, it's not gonna show you the full story of how that code might've changed. So, using git log with follow, it allows you to specify that file name, and it's gonna track it as that file changes or moves inside different directories.
[00:01:36] If we use the --name-status flag, it's also gonna show you the name of the file, what that was when the changed happened.
>> Nina Zakharenko: Git log --grep, lets us search for commit messages that match a regular expression. This can be used with, it can be mixed and matched with lots of other git flags.
[00:01:58] In this example, I'm grepping for the term mail, the author is Nina. Mail like email, the author is is Nina and since of two weeks.
>> Nina Zakharenko: We can also use diff-filter. This allows us to selectively include or exclude files that have been added, deleted, modified. There are quite a few diff-filters.
>> Nina Zakharenko: In this case I'm parsing in R for renamed.
>> Nina Zakharenko: Another useful example is, I'm looking for a file, it's been renamed in a previous commit, how would I find it again? I would use the diff-filter with the m flag for modifying.
>> Nina Zakharenko: Now, this is something that I see confuse people all the time, over and over again, hat.
[00:03:00] How many of you know about and use the hat or the tilde?
>> Nina Zakharenko: But you've seen it on stack overflow and exercises, and you're like what does this mean? It's a way of referencing commits. So the hat with no arguments, is the same as the hat with the number 1 is an argument, which means the first parent commit, n it nth parent commit.
[00:03:32] So, in modern workforce, in most merges we only have two parents, but git allows lots and lots of parents. So this is how you would choose which parent to refer to. The tilde is how many commits back to go, only following the first parent.
>> Nina Zakharenko: And the hat and the tilde can be combined, and I'm gonna show you an example.
>> Nina Zakharenko: We're gonna go through some exercises based on this graph. So, A is the very latest commit. D, E, and F are the oldest commits in this repository. And commit nodes B and C are parents of commit A. That means that A is a merge commit of B and C.
[00:04:30] Let's do some exercises.
>> Nina Zakharenko: How would we reference A, or A, or hat 0? Pretty easy.
>> Nina Zakharenko: How would we reference B? We can do it with A hat no arguments, right? Because that's the default. Or we can say A tilde 1, give me your first commit only following the first parent.
[00:05:05] When you do a git merge, git keeps track of which parent was first, which parent was second. Those don't change, they're static values. How would we reference C?
>> Nina Zakharenko: With A hat 2, right? We're asking for the second parent of C. B is the first parent, C is the second parent.
[00:05:32] If we wanted to navigate to commit F using this format, we would need to do it through C, because none of these, I'm sorry B also goes to F. But if we wanted to take a shortcut, we would have to go through commit C. Now, one more example, D.
[00:05:58] We can say A hat hat, meaning the first parent, first parent. We can do that with arguments, or we can say A tilde 2. So, follow two commits back on the first parent. I recommend checking out this graph again after today. I promise it will make sense if you stare at it long enough.
[00:06:23] And these, the hat, and the tilde, I probably use the tilde more frequently than the hat. They're very useful if you want to, say, I need to revert, but I need to revert to three commits ago. Instead of going, and finding that commit number, and copying it and pasting it, you can easily say tilde 3 to go three commits back.
>> Audience member: Is the tilde designed to be used always with a number, or can you do it without parsing anything?
>> Nina Zakharenko: If you don't pass a number, it uses 1 by default.
>> Audience member: Okay.