DEV Community

Cover image for Git and Github: git log
Joseph Trettevik
Joseph Trettevik

Posted on

Git and Github: git log

Song of the Week

Introduction

One of the first things you'll find a need for once you begin creating a commit history is the ability to view that history. This where git log comes in handy. There numerous options you can use with git log to display the commit history in different ways, but in post I'll cover some of the more useful options.

Display Options

git log

The basic option, is no options. If you enter git log with any options it will print out the commit history for the current repository. The numbers and letter you see on the left are the first 7 digits of the 40 characters commit ID. Next to that is the message associated with that commit, and on the right you can see the author and relative date. You can tell from the relative dates that the commits are listed in reverse chronological order. So the most recent commit is printed first and the commits get older and you move down.

// ♥ git log
067a4ee correct linting errors (Joseph Trettevik, 2 days ago)
b48fdd7 create ScrollContainer component (Joseph Trettevik, 2 days ago)
366b9c1 Merge pull request #85 from FoodIsLifeBGP/client-and-donor-validations (Edgar Martinez, 4 days ago)
d845da0 newline for linting (bastion6429, 4 days ago)
f76b0ce made changes to reflect pre-alpha requirements (bastion6429, 4 days ago)
Enter fullscreen mode Exit fullscreen mode

git log --stat

If you'd like to see an overview of what was modified in each commit you can us the --stat option. This will list the files that were modified, how many lines of code were deleted or added in those files, and summary of that information for each commit.

// ♥ git log --stat
067a4ee correct linting errors (Joseph Trettevik, 2 days ago)

 src/elements/ScrollContainer/ScrollContainer.tsx | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
b48fdd7 create ScrollContainer component (Joseph Trettevik, 2 days ago)

 src/elements/ScrollContainer/ScrollContainer.tsx | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
366b9c1 Merge pull request #85 from FoodIsLifeBGP/client-and-donor-validations (Edgar Martinez, 4 days ago)
d845da0 newline for linting (bastion6429, 4 days ago)

 src/util/constraints/donorRegistration.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Enter fullscreen mode Exit fullscreen mode

git log --graph

If you'd like to see a graphical representation of the commit history you can use the --graph option. This use simple graphics to display the branches and merges in the commit history in a visual way.

// ♥ git log --graph
* 067a4ee correct linting errors (Joseph Trettevik, 2 days ago)
* b48fdd7 create ScrollContainer component (Joseph Trettevik, 2 days ago)
*   366b9c1 Merge pull request #85 from FoodIsLifeBGP/client-and-donor-validations (Edgar Martinez, 4 days ago)
|\  
| * d845da0 newline for linting (bastion6429, 4 days ago)
| * f76b0ce made changes to reflect pre-alpha requirements (bastion6429, 4 days ago)
| * 783ffb5 made business license field not mandatory (bastion6429, 4 days ago)
| * 39c7f08 added image in donor registration and donation (bastion6429, 6 days ago)
Enter fullscreen mode Exit fullscreen mode

git log --all

To see all commits in the history, even the ones that are not associated with the current branch, you can use the --all option. In the example below we see that the third commit listed is new compared to that last example. This commit is on a branch off the the master that is no associated with this current branch, but it shows up because we used the --all option.

// ♥ git log --all
067a4ee correct linting errors (Joseph Trettevik, 2 days ago)
b48fdd7 create ScrollContainer component (Joseph Trettevik, 2 days ago)
3e68f7c refactored register to also return formErrors (bastion6429, 3 days ago)
366b9c1 Merge pull request #85 from FoodIsLifeBGP/client-and-donor-validations (Edgar Martinez, 4 days ago)
d845da0 newline for linting (bastion6429, 4 days ago)
f76b0ce made changes to reflect pre-alpha requirements (bastion6429, 4 days ago)
Enter fullscreen mode Exit fullscreen mode

Limiting and Search Options

git log --before

The --before option will allow you to what to print all the commits that were created before a given date. You can specify a specific date or a relative date like I did in the example below.

// ♥ git log --before='3 days ago'
366b9c1 Merge pull request #85 from FoodIsLifeBGP/client-and-donor-validations (Edgar Martinez, 4 days ago)
d845da0 newline for linting (bastion6429, 4 days ago)
f76b0ce made changes to reflect pre-alpha requirements (bastion6429, 4 days ago)
783ffb5 made business license field not mandatory (bastion6429, 4 days ago)
39c7f08 added image in donor registration and donation (bastion6429, 6 days ago)
Enter fullscreen mode Exit fullscreen mode

git log --after

You can allows limit the log output to commits that were created after a certain date with the --after option.

// ♥ git log --after='3 days ago'
067a4ee correct linting errors (Joseph Trettevik, 2 days ago)
b48fdd7 create ScrollContainer component (Joseph Trettevik, 2 days ago)
Enter fullscreen mode Exit fullscreen mode

git log --before --after

One of the coolest things about git log is that you can use multiple options to get exactly the output that you want. This allows use to print out the commits that created in a certain range by use both the --before and the --after option.

// ♥ git log --before='5 days ago' --after='8 days ago'
39c7f08 added image in donor registration and donation (bastion6429, 6 days ago)
803f9f1 added donation validation constraint (bastion6429, 7 days ago)
Enter fullscreen mode Exit fullscreen mode

git log --author='name'

Another useful search option is the --author option. This allows you to print out only the commits that were created by a given author.

// ♥ git log --author='Joseph Trettevik'
067a4ee correct linting errors (Joseph Trettevik, 2 days ago)
b48fdd7 create ScrollContainer component (Joseph Trettevik, 2 days ago)
e066704 rename scroll component to EndOfScrollWrapper (Joseph Trettevik, 2 weeks ago)
ae18eaf add ScrollContainer to index (Joseph Trettevik, 2 weeks ago)
9bfb1b6 create scroll container component (Joseph Trettevik, 2 weeks ago)
b3a6295 add ScrollContainer to index (Joseph Trettevik, 2 weeks ago)
a1d7bc0 create scroll container component (Joseph Trettevik, 2 weeks ago)
d3f1ab0 make typechecking corrections (Joseph Trettevik, 4 weeks ago)
883382d optimize Checkbox component (Joseph Trettevik, 4 weeks ago)
bf74c33 make lint and typechecking corrections (Joseph Trettevik, 4 weeks ago)
1269f2f create functional Checkbox component (Joseph Trettevik, 4 weeks ago)
Enter fullscreen mode Exit fullscreen mode

git log -S

Finally, the -S option lets you search all commits that contain changes the occurrences of that given string. This is great for the situations where you want to see every commit that changes an certain block of code. In the example between I given the name of a component and all the commits that modify that component are printed.

// ♥ git log -S 'FormTextInput'
a2ec4fa add contributionGuide for hackathon and following development (duskcloudxu, 6 weeks ago)
bea4470 Revamp Login Screen UI and form interaction (#40) (Ernest Bruno, 6 weeks ago)
bb988f4 Decouple Donations from DonationOrClaim (rahulrajaram, 6 weeks ago)
f7cad70 Client registration screen, absolute imports fixed, made register bidirectional (Abe Dolinger, 5 months ago)
877430c Added use-global-hook state management (Abe Dolinger, 5 months ago)
566cd53 Donations screen loads existing donations, create new donation posts to backend (Abe Dolinger, 7 months ago)
e6d506b Registration, login, approval, dashboard (Abe Dolinger, 7 months ago)
3e7eb9a Added Terms, Header, Icon, Back, Menu (Abe Dolinger, 7 months ago)
c0095b7 Registration UI (Abe Dolinger, 7 months ago)
Enter fullscreen mode Exit fullscreen mode

Takeaways

  • git log - will print out the commit history for the current repository in reverse order (most recent commit at the top of list).
  • git log --stat - will list the files that were modified, how many lines of code were deleted or added in those files, and summary of that information for each commit.
  • git log --graph - will use simple graphics to display the branches and merges in the commit history in a visual way.
  • git log --all - will print all commits in the history, even the ones that are not associated with the current branch.
  • git log --before='date' - will print all the commits that were created before a given date.
  • git log --after='date' - will print all the commits that were created after a given date.
  • git log --author='name' - will only print the commits that were created by a given author.
  • git log -S <string> - will print all commits that contain changes to the occurrences of that given string.

References

Viewing the Commit History - Git Docs
Git Log - Git Docs

Top comments (0)