git: search commits by date, branch, author

2021-01-21

 | 

~2 min read

 | 

293 words

Recently, I lost a commit. I couldn’t find it anyway.

Fortunately, I knew which day I’d made the commit, so in theory, I would be able to search that way.

To search commits within a window of dates, you can use the --before and --after flags of git log1, for example:

% git log --after="2021-01-01" --before="2021-01-20"

This is when I found the second wrinkle: I had some how lost the branch — which was surprising given that I use a standard naming convention to organize my commits.

To fix this problem, I had to learn how to search for commits across all branches simultaneously (the default behavior is to only search for commits on the active branch).

Again, this was as simple as learning about the options available on the git log command. In this case, it was the --all flag that did the

% git log --after="2021-01-01" --before="2021-01-20"--all

Now that I’m looking across all of the branches, however, in a busy repo, this can still be noisy on a busy repo. Fortunately, you can filter by author to see only your commits:

% git log --after="2018-06-30" --before="2018-07-03" --oneline --author=stephen

Ultimately, I found my commit. I had mistakenly tagged it to the wrong ticket and placed it on the wrong branch as a result! But, thanks to these tools, I was able to track it down and fix it.

Footnotes

  • 1 git log in its generic form here is for simplicity only. I prefer a prettier one line version of git log, which I wrote about in Git: Pretty One Line Logs. If you adopt this approach, you can simply pass in the dates as arguments following the alias:

    % gl --after="2021-01-01"


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!