FREELessons: 24Length: 3.1 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.2 Viewing the Log

History could be the very motto of version control systems, so you should have a good way to track that. Never fear, `git log` is here, not only showing us that’s happened, but allowing us to generate graphs to get a better view.

4.2 Viewing the Log

The next tool we'll be looking at is git-log, which is a great way to view the history that you've already made. Of course, we'll begin by opening our terminal, and going into our project directory. And up until now, we've been running git status, as soon as we get in, and oh, it looks like I haven't actually committed this README from last time. So let me do that right now. I'll just commit everything and we'll say updated README. And have to remember not to use the shortcuts I'm used to using. All right, so now if we clear that and run git status, it looks like we're clean. So we wanna view our history, and git comes with git-log, which I mentioned, which is an excellent tool to view our history. If I hit Enter here, you can see that we get a history of all our commits, starting with the most recent one that we made right now, updated README, and all the way back to our initial commit. So we have made five commits as you can see. And this fits just perfectly in the height of our terminal here. However, if your terminal was not high enough to show all the commits, which of course will be the case eventually once you get well along in a project. Then this will actually be paged, so you'll hit J and K to up and down. And then right now, I'm just gonna hit Q to get back to my terminal which is quit. Similar to vim in that way. So really, it's that simple. Just get log and we get a bunch of commits. So lets look at what information we get here. So you can see we get our commit hash, right there like that. Then we have the author, author name and author email address. We have the date that the commit was made, and then we have the commit message. And we just have that for each one of the five commits that we've made so far. So that seems pretty straightforward. What more could we do with that? There's actually a lot more we can do with git-log, through some flags and options. Let me clear my screen again. And let's say we wanna show a little more information. As we're logging this out. We can use the --stat flag to put some statistics out with it. So now you can see that we have a list of the files that were changed, what was changed, and then a summary. One file change, one insertion, two deletions. If I scroll down here to maybe one of our larger commits, you can see down at the bottom here, in our first commit, README added eight lines, TODO added one line, so two files were changed, and all together nine lines were inserted. If we go up here, we can see that the TODO file, three lines were adjusted, looks like we have two plus signs and one minus sign, so two lines were added, one was subtracted. And that is consistent with what we have down here in the summary, one file changed, two lines inserted, one line deleted. So you can see just by adding the stat flag like that, we get a little bit more information about what actually went into our commits. However, sometimes you actually want a little bit less information, so we can do something like git-log, and actually let me clear my screen here for you first. We can do git log --oneline and we just get a single line. We get our abbreviated hash and the commit message. This is helpful if you want to compare a commit. Maybe you want to run a diff on an older version of our to do list. So for example, let's say, I can see right here we updated the to do list. This was the last time that we updated it. If I wanna copy that, now I can run get diff. Based on that hash and I want to compare the to do file, I can see, okay, the to do file has not changed at all, since that commit. And of course, we kind of already knew that, but that's the kind of comparison you can do and that might be one place where running a git-log with one line might be useful. If your projet has a lot of branches and merges and a lot of stuff going on in that way. And we'll be talking about branches in the next video. You can run git log --graph to get a graph of it. As you can see here, we have this line up the side, and since we have only been working on the master branch, there's a single line here. However, if we'd been working on more things, this would be branching out. And we'll come back to that when we actually start looking at branches. But then we can actually combine some flags here. So I can say, git log --oneline --graph, and I get a one line graph, and you might think that looks the same as just the one line output, but we have these * at the beginning, that would actually show a graph if we had enough content to make a graph. Of course, there's many other flags that do different things with the git-log output, but one thing to think about is other commands that might piggyback off the git-log command. Maybe in your development workflow, you will need to generate some custom reports based on the commits that have been made in your repository. You could actually take the output from git-log and feed it into another command lined application, and if that's the case, you're gonna want to manipulate this git-log output a bit more. This is where the git log -prettyflag, comes in handy. After the pretty flag, we use an equals sign and then inside the quotes, we can actually pass a format, that will use some variables to define what output will be put out to our terminal. So for example, lets say that we have a tool that takes comma separated value out from our git-log command here, and we input it to a tool that takes comma separated values, and it produces a report or something. So let's say we want a committer name, let's start with the abbreviated hash, then we'll have the committer name, and then we will have the relative commit date. So three comma separated values just like that. Now I can hit Enter, and you can see that we have exactly what we asked for, the abbreviated hash, a comma, the name of the committer, a comma, and the time since that commit has taken place. While you might think, that's great, but how do I know what variables like that are actually available? Here's a list of all the available variables that you could use in a formatting string like that, and down at the bottom there, you can see a link to the git documentation, just so you can verify that if you'd like to. That's all we're gonna do with the git-log command for now. However, since we're talking about logging and showing commits, when you installed git, it actually installed a graphical tool called gitk, or gittk that uses tk to bring up a little bit of a UI to actually show us a little bit more information on our actual git history. So if you just run gitk as one word, you can see back here that we get a UI and this actually gives us a little more flexibility. As you can see over here, we have our commits listed and here's our most recent commit. And if you wanna see what's changed down here, we have the diff selected, and we can see down here, one line was added and two were deleted. We could choose to look at the old version here and just see that's what the old version looked like. Of course, it still shows those lines were deleted. That is what has changed. We could look at the new version here, and you can see that the new version is just with this single line added. Over on this side here, you can see what files were changed. So if we come back here to a commit, where more than one file was changed we could choose. Look at the read me, look at the to do and of course this is small enough, so this left panel here doesn't have to scroll. We're not actually seeing, nothing actually changes when I click on these. Because you can, see here's the read me file. And heres the to do file. Also, cross the commit names here, you can see here, is the name of the committor, and the actual date and time that it was committed. So you have a lot of information here, that if you want to view this in a pretty permissive but sometimes helpful user interface, you can do that. And I'm just gonna click that to get back to our command line. And that is how you can look at your history in git.

Back to the top