How to Check If a Git Commit Is a Merge Commit

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if a Git commit is a merge commit. We will explore the git show command to examine commit details, specifically focusing on identifying parent commits, which is a key indicator of a merge.

You will also learn how to use the git log --merges command to efficiently list only merge commits in your repository's history. Finally, we will test these methods on non-merge commits to solidify your understanding of how to distinguish between different types of commits in Git.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git/BasicOperationsGroup -.-> git/add("Stage Files") git/BasicOperationsGroup -.-> git/status("Check Status") git/BasicOperationsGroup -.-> git/commit("Create Commit") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/add -.-> lab-560056{{"How to Check If a Git Commit Is a Merge Commit"}} git/status -.-> lab-560056{{"How to Check If a Git Commit Is a Merge Commit"}} git/commit -.-> lab-560056{{"How to Check If a Git Commit Is a Merge Commit"}} git/log -.-> lab-560056{{"How to Check If a Git Commit Is a Merge Commit"}} end

Use git show to Check Parents

In this step, we will explore how to use the git show command to examine the details of a commit, specifically focusing on its parent commits. Understanding parent commits is crucial for navigating the history of your project and understanding how different commits relate to each other.

First, let's make sure we are in our project directory. Open your terminal and navigate to the my-time-machine directory:

cd ~/project/my-time-machine

Now, let's use git log to see our commit history. We'll use the --oneline flag for a concise view:

git log --oneline

You should see output similar to this (your commit hash will be different):

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

This shows our first commit. Now, let's use git show to see the details of this commit. Copy the commit hash (the short string of letters and numbers, like a1b2c3d) from your git log output and replace YOUR_COMMIT_HASH in the command below:

git show YOUR_COMMIT_HASH

For example, if your commit hash was a1b2c3d, you would run:

git show a1b2c3d

The output will be quite detailed, showing information about the commit, including the author, date, commit message, and the changes introduced by the commit.

commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9
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..a1b2c3d
--- /dev/null
+++ b/message.txt
@@ -0,0 +1 @@
+Hello, Future Me

For our first commit, which is the very beginning of our project's history, you will notice that there is no "Parent" line in the output. This is because the initial commit has no parent โ€“ it's the root of the project's history.

In later steps, when we have more commits, we will use git show again to see how parent commits are displayed and how they link the history together. Understanding this linkage is fundamental to understanding how Git tracks changes over time.

Run git log --merges to Verify

In this step, we will learn about merge commits and how to use git log --merges to specifically view only these types of commits in our project's history. Merge commits are special commits that bring together changes from different branches.

Currently, our project history is very simple, with only one initial commit. We don't have any branches or merges yet. Let's first confirm this by running git log --merges.

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

cd ~/project/my-time-machine

Now, run the command:

git log --merges

Since we haven't performed any merges, this command will likely produce no output:

This is expected! The git log --merges command is designed to filter the commit history and only show commits that were created as a result of merging one branch into another.

To see this command in action, we would need to create a new branch, make some commits on that branch, and then merge it back into our main branch. We will explore branching and merging in future labs.

For now, it's important to understand that git log --merges is a powerful tool for understanding when and how different lines of development were combined in your project. This is particularly useful in collaborative environments where multiple people are working on different features simultaneously.

Test Non-Merge Commits

In this step, we will create a regular commit (a non-merge commit) and then use git log to see how it appears in our history compared to a merge commit (which we don't have yet, but will in future labs). This will help solidify your understanding of different commit types.

First, let's make a small change to our message.txt file. We'll add another line.

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

cd ~/project/my-time-machine

Now, use the echo command to append a new line to the file:

echo "Adding another line." >> message.txt

The >> operator appends the text to the file instead of overwriting it.

Let's check the content of the file:

cat message.txt

You should see:

Hello, Future Me
Adding another line.

Now, let's check the status of our repository:

git status

You should see that message.txt has been modified:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   message.txt

no changes added to commit (use "git add" and/or "git commit -a")

Git sees the change we made. Now, let's stage and commit this change.

git add message.txt
git commit -m "Add a second line to message"

You should see output confirming the commit:

[master a1b2c3d] Add a second line to message
 1 file changed, 1 insertion(+)

Now, let's look at our commit history again using git log --oneline:

git log --oneline

You should see two commits:

e4f5g6h (HEAD -> master) Add a second line to message
a1b2c3d Send a message to the future

The latest commit (e4f5g6h in this example, your hash will differ) is our new non-merge commit. It represents a single change made directly on the master branch.

If we were to run git log --merges again, it would still show no output because neither of these commits is a merge commit.

Understanding the difference between regular commits and merge commits is important for interpreting your project's history and collaborating effectively with others. Regular commits represent linear progress on a single line of development, while merge commits represent the integration of changes from different lines.

Summary

In this lab, we learned how to identify merge commits in Git. We started by using the git show command to examine the details of a commit, specifically focusing on the presence or absence of parent commits. We saw that the initial commit has no parent, while subsequent commits will display their parent(s).

We then explored the git log --merges command, which is a direct way to list only the merge commits in the repository's history. Finally, we tested these methods on non-merge commits to confirm that they are correctly identified as such, solidifying our understanding of how to distinguish merge commits from regular commits.