How Can I Know if a Branch Has Been Already Merged Into Master?

Better Stack Team
Updated on August 12, 2024

To determine if a branch has already been merged into the master branch in Git, you can use several methods. Here are a few common approaches:

1. Using git branch --merged

This command lists the branches that have been merged into the currently checked-out branch. If you're on master, it will show you which branches have been merged into master.

  1. Checkout to master (or the branch you want to check against):

     
    git checkout master
    
  2. List merged branches:

     
    git branch --merged
    

    If your branch appears in the list, it means it has been merged into master.

2. Using git log

This command checks the commit history to see if the tip of your branch is present in the master branch history.

  1. Make sure you are on the master branch or the branch you want to compare against:

     
    git checkout master
    
  2. Check the log for the branch in question:

     
    git log --oneline master | grep <branch_name>
    

    Replace <branch_name> with the name of your branch. If you see commits from your branch, it means the branch has been merged.

3. Using git merge-base

This command finds the common ancestor of two branches. If the common ancestor is the same as the tip of the branch in question, it means the branch has been fully merged.

  1. Check the merge base between master and your branch:

     
    git merge-base master <branch_name>
    
  2. Compare the result with the tip of your branch:

     
    git rev-parse <branch_name>
    

    If the output of both commands is the same commit hash, it means the branch has been merged into master.

4. Using GitHub or GitLab

If you are using a hosting service like GitHub or GitLab, they often provide an interface to check if a branch has been merged.

  1. Go to the pull requests or merge requests section.
  2. Look for the branch in question.
  3. If the pull request/merge request is marked as merged, then the branch has been merged into the target branch (e.g., master).

Example

Let’s say you want to check if feature-branch has been merged into master.

  1. Checkout to master:

     
    git checkout master
    
  2. Use git branch --merged:

     
    git branch --merged
    

    If feature-branch is listed, it has been merged.

Alternatively, use git merge-base:

 
git merge-base master feature-branch
git rev-parse feature-branch

If both commands return the same commit hash, feature-branch has been merged into master.

By using these methods, you can confidently determine if a branch has already been merged into master.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github