DEV Community

Cover image for I love git log
Andrew Stuntz
Andrew Stuntz

Posted on

I love git log

The title of this post should be IFLG (like IFLS, but ya know with Git).

But it's probably not great to title a post with the f*** word. Ya know.

Git is the best

Really, I love git. I think it is ridiculously useful and when used properly, it can speed up development on a team tremendously. When I was first learning git though, I always got so frustrated by not being able to see what was going on with git, like what were my teammates doing and how did my work fit in with what they were doing?

I tried a few UI's for working with git but was never satisfied, everything seemed clunky and you had to get out of the terminal to go look at the UI and then come back and figure out what you were doing again.

One of my favorite commands when I am working with git repo's is (hopefully unsurprisingly) git log.

This shows a log of all of your commits and the basic command outputs something along the lines of:

commit 8c2ca7c38f50e1d837485edd946916bfdciii20a1 (HEAD -> master, origin/master)
Author: Andrew Stuntz <andrewbstuntz@gmail.com>
Date:   Mon Aug 20 10:20:27 2018 -0700

    Fixes the resetting of form fields on the settings form

commit cc309a7cb45bfcfd7e94a49387086266dff54c05
Author: Andrew Stuntz <andrewbstuntz@gmail.com>
Date:   Mon Aug 20 10:03:46 2018 -0700

    Updates some styling issues and copy

commit 2d848cec12038475b9f12070547653ab212e4e6
Author: Andrew Stuntz <andrewbstuntz@gmail.com>
Date:   Mon Aug 20 08:17:13 2018 -0700

    Add config and format correctly

commit deec64859bc9b2ef74a987437b7864f2183940e
Author: Andrew Stuntz <andrewbstuntz@gmail.com>
Date:   Thu Aug 16 11:11:42 2018 -0700

    First commit, I never know what to put here?

So what do we have going on here?

commit 8c2ca7c38f50e1d837485edd946916bfdciii20a1 (HEAD -> master, origin/master)
Author: Andrew Stuntz <andrewbstuntz@gmail.com>
Date:   Mon Aug 20 10:20:27 2018 -0700

    Fixes the resetting of form fields on the settings form

Overall, it's pretty easy to see the ordering of the commits, top to bottom, first commit to last commit. You get the revision number which uniquely identifies the individual revision. Next to that, you see some more information about your branch in the parentheses, these reference your HEAD (which is what you have checked out now). You also origin/master which means that origin/master is at this commit as well. So your HEAD and origin/master are at the same revision. Then you get an Author and a Date. Finally, you get the commit message. So far so good. Nothing too complicated.

Diving into the git log command

If you do a git log --help you get ample output, in fact one reason I love git is the massive amount of documentation that allow you to really dive into and customize what you want. I'm gonna run through a few of my favorite options that let you really see what is going on in your repository.

The first is git log --graph

Which lets you see a graph of the branches.

* commit 8c2ca7c38f50e1d837485edd946916bfdciii20a1 (HEAD -> master, origin/master)
| Author: Andrew Stuntz <andrewbstuntz@gmail.com>
| Date:   Mon Aug 20 10:20:27 2018 -0700
|
|     Fix the resetting of form fields on the settings form
|
* commit cc309a7cb45bfcfd7e94a49387086266dff54c05
| Author: Andrew Stuntz <andrewbstuntz@gmail.com>
| Date:   Mon Aug 20 10:03:46 2018 -0700
|
|     Update some styling issues and copy
|
*   commit a9b263cacd3240f6d00e7ce401fc6f6c23988cee
|\  Merge: 2d848cec deec4187
| | Author: Andrew Stuntz <andrewbstuntz@gmail.com>
| | Date:   Mon Aug 20 08:19:44 2018 -0700
| |
| |     Merge branch 'master' of some/private/repo
| |
| *   commit deec398745bc9b2ef74a418287b7864f2183940e

Already we are starting to get a little more visual, you see the commit easily from one branch into the master branch. You can also see at the bottom the commit that was made onto that branch before it was merged into the master branch.

The next command I like a lot is git log --oneline

This takes the output and makes each commit a single line, so it vastly increases the amount of commits that you can see at any give time.

8c2ca7c3 (HEAD -> master, origin/master) Fix the resetting of form fields on the settings form
cc309a7c Update some styling issues and copy
a9b263ca Merge branch 'master' of https://github.com/drews256/some/private/repo
2d848cec Add config and format correctly
deec4187 Merge pull request #1303940 from drews256/some-other-branch

Now we have the same amount of information, but succinctly displayed in 5 lines.

You can even combine the two and get git log --oneline --graph.

* 8c2ca7c3 (HEAD -> master, origin/master) Fix the resetting of form fields on the settings form
* cc309a7c Update some styling issues and copy
*   a9b263ca Merge branch 'master' of https://github.com/drews256/some/private/repo
|\
| *   deec4187 Merge pull request #1303940 from drews256/some-other-branch

You get the best of both worlds, you see the graph and branching plus the commits and now its displayed succinctly.

If you have color globally turned on for your git repos, this will display in full color, with each branch having a unique color which makes it very easy to follow. If that isn't the case and you want to add some color to your output, you can run git log --oneline --graph --color and you will see each branch nicely colored for you to explore.

Now you can see what and where your branches are easily and make sure your not working in or on places you shouldn't be.

What are your favorite parts of git?

Thanks atlassian for the cover photo.

Top comments (25)

Collapse
 
dmfay profile image
Dian Fay

I forget where I found this:

alias gtree='git log --graph --abbrev-commit --decorate --date=relative --format=format:'\''%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)'\'' --all'

Then gtree in your repo.

Collapse
 
vguarnaccia profile image
Vincent Guarnaccia

I'm totally going to borrow this. I add a '%G?' to my format so it's easy to see which commits are signed or not.

Collapse
 
sunbingfeng profile image
sunbingfeng

Yeah, you could add git alias to long command.

Collapse
 
latentfuss profile image
John Thompson

OoOoOo me Gusta!

Collapse
 
aurelkurtula profile image
aurel kurtula

This is great, thanks for sharing.

Collapse
 
hoelzro profile image
Rob Hoelz

I'm with you, Andrew - I think git log is my favorite part about Git!

I personally love using git log -S $QUERY to find out "hey, when did $QUERY appear in/disappear from the repository?" I was trying to figure out some things about what was removed between GTK 2 and 3, and -S is perfect for finding things like that!

Another log feature I love answers the question "I know I made this commit recently, and it contains the substring 'hello' - but which branch was it on?" For that I use git log --author=hoelz -G hello --all --source, which will print commits I wrote whose diff contains "hello". It searches across all branches, and prints the branch it used to find each commit.

Collapse
 
tux0r profile image
tux0r • Edited

Overall, it's pretty easy to see the ordering of the commits, top to bottom, first commit to last commit.

Which VCS does not do that?

What are your favorite parts of git?

To be honest, I have none. Everything Git provides is done in a similar or nearly identical way with Mercurial, the one thing both don't do too well, namely: merging incompatible branches, is the unique selling point for Darcs/Pijul.

Whenever I need to use Git, I surely prefer GitKraken. -- Alright, there is one thing Git does very well: The ecosystem of GUIs is very mature, even better than SVN's. :-)

Collapse
 
hoelzro profile image
Rob Hoelz

tux0r - have you written any posts on Pijul or Darcs? If not, you totally should! I think it would be really interesting to see a fresh perspective on what else is out there in the VCS world, and it would be a nice chance to highlight what non-Git source control systems bring to the table. I know a lot of people feel like Git has "won", but I myself have a soft spot for Mercurial, and I think having competition is essential to innovation in the space!

Collapse
 
tux0r profile image
tux0r

I find it sad to see that Atlassian does not advertise their own support for Mercurial. All you can find in their ads is Git here, Git there...

I haven't worked with Pijul yet. I made an account in their "nest" a while ago, but it's all too alpha-quality just yet. I have some private code on Darcshub though. No, I have not written anything about Darcs (yet?), Haskell is not my usual habitat. But I recommend everyone to spend half an hour with it. If you're not sold after that, you never will.

Thread Thread
 
slavius profile image
Slavius • Edited

I find it sad to see that Atlassian does not advertise their own support for Mercurial.

Nah, Atlassian does only what makes sales go high. Got a serious bug in Atlassian product? Wait few years until we finish new shiny graphical UI first!

Collapse
 
drews256 profile image
Andrew Stuntz

Which VCS does not do that?

Just trying to start out on the right foot.

the one thing both don't to too well, namely: merging incompatible branches, is the unique selling point for Darcs/Pijul.

I haven't used Darcs/Pijul before. But, I plan on writing somethings about merging/rebasing difficult branches. Which, probably isn't too different than what most people do, but there are ways to make merging less painful.

Whenever I need to use Git, I surely prefer GitKraken.

Yeah, I have used GitKraken, its the closest thing to a usable git GUI that I would use.

Collapse
 
eljayadobe profile image
Eljay-Adobe

I was disappointed with SourceTree.

I like Cycligent.

I thought GitKraken had the prettiest UI of the front-ends I've seen.

The only two problems I experienced with GitKraken, which is from a year or two ago:

  • abysmally slow (hopefully fixed by now...)
  • no Git LFS support (hopefully fixed by now...)
Thread Thread
 
tux0r profile image
tux0r

It is still slow because it still uses Electron AFAIK.

Collapse
 
ozzyogkush profile image
Derek Rosenzweig

Sourcetree is pretty good, though they have some annoying bugs once in a while.

Collapse
 
porky11 profile image
Fabio Krapohl

No, Git and Mercurial are very different.
In Git, branches are just a point in a history, similar to tags, just with regular updates.
In Mercurial, every commit belongs to a specific branch and it matters, which branch you merge into which one.

Besides that, Git has many GUIs, but none is good.
They often are incomplete, difficult to use and do a lot of unintended stuff in the background.
Mercurial has TortoiseHG, which just works like the command line stuff, and you can use many advanced command line features in GUI as well.

Collapse
 
joelnet profile image
JavaScript Joel

I have found these git aliases to be helpful. Paste this into your ~/.gitconfig file.

[alias]
    lg = !"git lg1"
    lg1 = !"git lg1-specific --all"
    lg2 = !"git lg2-specific --all"
    lg3 = !"git lg3-specific --all"

    lg1-specific = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)'
    lg2-specific = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(auto)%d%C(reset)%n''%C(white)%s%C(reset) %C(dim white)- %an%C(reset)'
    lg3-specific = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset) %C(bold cyan)(committed: %cD)%C(reset) %C(auto)%d%C(reset)%n''          %C(white)%s%C(reset)%n''          %C(dim white)- %an <%ae> %C(reset) %C(dim white)(committer: %cn <%ce>)%C(reset)'

git lg
git lg

git lg2
git lg2

git lg3
git lg3

courtesy of: stackoverflow.com/a/34467298/504836

Collapse
 
adityavarma1234 profile image
Aditya Varma

There seems to be a typo in the last command for

git log --online --graph --color

it should be

git log --oneline --graph --color

Collapse
 
drews256 profile image
Andrew Stuntz

Yes! My bad. I'll fix it when I get a chance.

Collapse
 
wbern profile image
William Bernting

Maybe you'd enjoy git-screensaver. 🙂

 
drews256 profile image
Andrew Stuntz

slow because it still uses Electron AFAIK

Electron apps don't have to be slow....

Thread Thread
 
tux0r profile image
tux0r

Name one which is not...?

Thread Thread
 
drews256 profile image
Andrew Stuntz

Postman comes to mind.

Collapse
 
motss profile image
Rong Sen Ng

TIL --graph. Thanks.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.