How to Check If a Git Commit Was Made by a Specific Author

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a Git commit was made by a specific author. We will explore two primary methods: filtering the commit history using git log --author to view commits from a particular individual, and using git show with a commit hash to verify the author details of a single commit.

Through hands-on exercises, you will practice applying these commands within a sample Git repository. We will demonstrate how to filter commits by author name and then use git show to examine the author information of a specific commit, ensuring you can effectively identify the contributor of any given change in your Git history.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/SetupandConfigGroup(["Setup and Config"]) git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git/SetupandConfigGroup -.-> git/config("Set Configurations") git/BasicOperationsGroup -.-> git/add("Stage Files") git/BasicOperationsGroup -.-> git/commit("Create Commit") git/BasicOperationsGroup -.-> git/diff("Compare Changes") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/config -.-> lab-560070{{"How to Check If a Git Commit Was Made by a Specific Author"}} git/add -.-> lab-560070{{"How to Check If a Git Commit Was Made by a Specific Author"}} git/commit -.-> lab-560070{{"How to Check If a Git Commit Was Made by a Specific Author"}} git/diff -.-> lab-560070{{"How to Check If a Git Commit Was Made by a Specific Author"}} git/log -.-> lab-560070{{"How to Check If a Git Commit Was Made by a Specific Author"}} end

Run git log --author to Filter

In this step, we will learn how to filter the commit history using the --author flag with the git log command. This is useful when you want to see only the commits made by a specific person.

First, let's make sure we are in our project directory. Open your terminal and type:

cd ~/project/my-time-machine

Now, let's use git log with the --author flag to see commits made by "Jane Doe". Remember, we configured the author name in the setup step.

git log --author="Jane Doe"

You should see the commit we made in the previous lab:

commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 (HEAD -> master)
Author: Jane Doe <[email protected]>
Date:   Mon Aug 7 10:00:00 2023 +0000

    Send a message to the future

If you had commits from other authors, they would not appear in this output. This demonstrates how you can easily filter the commit history to focus on contributions from a specific individual.

Being able to filter the commit log by author is a powerful tool, especially when working in a team. It allows you to quickly see who made specific changes and understand the contributions of each team member.

Press q to exit the log view.

Use git show to Verify Author

In this step, we will use the git show command to view the details of a specific commit, including the author information. This is helpful when you want to examine a single commit in detail.

Make sure you are still in the ~/project/my-time-machine directory.

First, let's get the commit hash (the unique identifier) of our commit. We can use git log --oneline to get a shorter version of the log:

git log --oneline

You should see output similar to this:

a1b2c3d (HEAD -> master) Send a message to the future

The first part, a1b2c3d (your hash will be different), is the short commit hash. Copy this hash.

Now, use the git show command followed by the commit hash you copied:

git show <paste_your_commit_hash_here>

For example, if your hash was a1b2c3d, you would type:

git show a1b2c3d

The output will show the details of that specific commit, including the author:

commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 (HEAD -> master)
Author: Jane Doe <[email protected]>
Date:   Mon Aug 7 10:00:00 2023 +0000

    Send a message to the future

diff --git a/message.txt b/message.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/message.txt
@@ -0,0 +1 @@
+Hello, Future Me

You can clearly see the Author: line confirming that "Jane Doe" made this commit. The git show command is a powerful way to inspect individual commits, see exactly what files were changed, and view the content of those changes.

Press q to exit the view.

Test with Multiple Authors

In this step, we will simulate having another author make a commit to see how git log --author works with multiple contributors.

First, let's change the Git author configuration for the next commit. We can do this temporarily for this repository:

git config user.name "John Smith"
git config user.email "[email protected]"

Now, let's create a new file as "John Smith":

echo "Hello from John" > johns-file.txt

Add the new file to the staging area:

git add johns-file.txt

And commit the change as "John Smith":

git commit -m "Add a file from John"

You should see output indicating a new commit was created by "John Smith".

Now, let's use git log to see the full history:

git log

You will see two commits, one by "Jane Doe" and one by "John Smith".

Now, let's filter the log to only show commits by "Jane Doe":

git log --author="Jane Doe"

This will only show the first commit.

Finally, let's filter the log to only show commits by "John Smith":

git log --author="John Smith"

This will only show the second commit.

This demonstrates how git log --author is effective in filtering commits based on the author, which is essential for tracking contributions in collaborative projects.

Press q to exit the log views.

Summary

In this lab, we learned how to check if a Git commit was made by a specific author. We first used the git log --author command to filter the commit history and display only commits made by a designated author, demonstrating its utility for focusing on individual contributions within a team environment.

Subsequently, we explored using the git show command with a specific commit hash to view detailed information about that commit, including the author's name and email, providing a method to verify the author of a particular change. Finally, we tested these methods with multiple authors to confirm their effectiveness in distinguishing contributions from different individuals.