How to Check If a Git Branch Is Merged

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively check if a Git branch has been merged into another branch. We will explore the primary method using the git branch --merged command, demonstrating its usage and interpreting its output.

Furthermore, we will verify the merge status by examining the Git log for the presence of merge commits and test the behavior of the commands with unmerged branches to solidify your understanding. This hands-on approach will equip you with the skills to confidently manage your Git branches and maintain a clean repository.


Skills Graph

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

Use git branch --merged to Check

In this step, we will learn how to use the git branch --merged command to see which branches have been fully integrated into the current branch. This is a very useful command for cleaning up your repository and understanding the state of your project.

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 create a new branch and make a commit on it. This will simulate a common workflow where you work on a feature in a separate branch.

git branch feature/add-greeting
git checkout feature/add-greeting
echo "Greetings from the feature branch!" >> greeting.txt
git add greeting.txt
git commit -m "Add greeting file"

You should see output similar to this after the commit:

[feature/add-greeting a1b2c3d] Add greeting file
 1 file changed, 1 insertion(+)
 create mode 100644 greeting.txt

Now, let's switch back to the master branch:

git checkout master

You should see output indicating you switched branches:

Switched to branch 'master'

Now, let's merge the feature/add-greeting branch into master:

git merge feature/add-greeting

You should see output indicating the merge was successful:

Updating a1b2c3d..e4f5g6h
Fast-forward
 greeting.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 greeting.txt

Now that we have merged the feature/add-greeting branch into master, let's use git branch --merged to see which branches are now merged into the current branch (master).

git branch --merged

You should see output similar to this:

* master
  feature/add-greeting

The * indicates the current branch (master). The output shows that both master and feature/add-greeting are listed. This means that all commits from feature/add-greeting are now present in the master branch.

Understanding which branches are merged is important because it helps you identify branches that are no longer needed and can be safely deleted. This keeps your repository clean and organized.

Verify with git log for Merge Commit

In the previous step, we merged the feature/add-greeting branch into master. Now, let's use the git log command to see the commit history and confirm that the merge commit is present.

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

Run the git log command:

git log

You should see a list of commits, with the most recent one at the top. Look for a commit message that indicates a merge. It will likely look something like this:

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

    Merge branch 'feature/add-greeting'

commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 (feature/add-greeting)
Author: Jane Doe <[email protected]>
Date:   Mon Aug 7 10:03:00 2023 +0000

    Add greeting file

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

    Send a message to the future

Notice the commit with the message "Merge branch 'feature/add-greeting'". This is the merge commit that Git automatically created when we merged the feature/add-greeting branch into master. This commit has two parent commits, indicated by the "Merge:" line. One parent is the tip of the master branch before the merge, and the other is the tip of the feature/add-greeting branch.

The git log command is essential for understanding the history of your project. It allows you to see all the commits, who made them, when they were made, and what changes were included in each commit. This is incredibly valuable for debugging, tracking progress, and collaborating with others.

Press q to exit the log view.

Test with Unmerged Branches

In the previous steps, we saw how git branch --merged shows branches that have been fully integrated. Now, let's see what happens when we have a branch that has not been merged into the current branch.

Make sure you are in the ~/project/my-time-machine directory and on the master branch. You can verify this with git status.

Let's create a new branch called feature/add-farewell and switch to it:

git branch feature/add-farewell
git checkout feature/add-farewell

You should see output indicating you switched branches:

Switched to branch 'feature/add-farewell'

Now, let's create a new file and make a commit on this branch:

echo "Farewell from the farewell branch!" > farewell.txt
git add farewell.txt
git commit -m "Add farewell file"

You should see output similar to this after the commit:

[feature/add-farewell a1b2c3d] Add farewell file
 1 file changed, 1 insertion(+)
 create mode 100644 farewell.txt

Now, let's switch back to the master branch:

git checkout master

You should see output indicating you switched branches:

Switched to branch 'master'

We are now on the master branch, and the feature/add-farewell branch contains a commit that is not present in master.

Let's use git branch --merged again:

git branch --merged

You should see output similar to this:

* master
  feature/add-greeting

Notice that feature/add-farewell is not listed in the output. This is because the commit we made on feature/add-farewell has not been merged into the master branch.

Now, let's use the git branch --no-merged command. This command shows branches that have not been merged into the current branch.

git branch --no-merged

You should see output similar to this:

  feature/add-farewell

This output correctly shows feature/add-farewell because it contains commits that are not in the master branch.

The git branch --merged and git branch --no-merged commands are powerful tools for managing your branches. They help you keep track of which branches are finished and can be deleted, and which branches are still under development and need to be merged.

Summary

In this lab, we learned how to check if a Git branch has been merged into the current branch. We started by using the git branch --merged command, which lists all branches whose tips are reachable from the current branch's tip, effectively showing which branches have been fully integrated. We demonstrated this by creating a new branch, making a commit, merging it into the master branch, and then using git branch --merged to confirm the merge.

We also explored how to verify the merge using git log by looking for the merge commit, and tested the command with unmerged branches to understand the difference in output. This hands-on experience provided a practical understanding of how to use Git commands to manage and understand the state of branches in a repository.