Horje
How to Check If a Branch Has Been Merged into Master?

When working with Git, it’s common to create and merge branches as part of your development workflow. However, it’s essential to ensure that your branches are merged into the main branch (often master or main) to keep the codebase up-to-date and avoid redundant work. This article will explore several methods to determine if a branch has already been merged into the master branch.

Why Check if a Branch Has Been Merged?

  • Avoid Redundancy: Ensure that work is not duplicated and that branches do not contain changes already integrated into the master branch.
  • Maintain Code Integrity: Keep the master branch clean and up-to-date with all relevant changes.
  • Facilitate Collaboration: Help team members know the status of branches and avoid conflicts.

Approach 1: Using git branch –merged

Step 1: First Checkout into the master branch using the command,

  git checkout master

Step 2: Use the git branch –merged command to list all branches that have been merged into the current branch.

git branch --merged
Screenshot-2024-06-03-103149

If your branch present in the list then the branch is successfully merged into master.

Approach 2: Using git log

Step 1: First Checkout into master branch using the command,

  git checkout master

Step 2: Use the git log command to see if commits from your branch are in the master branch’s history.

git log
Screenshot-2024-06-03-103551-(1)

If commits from your branch appear in the log then the branch has been merged into master.

Approach 3: Using git merge-base

Step 1: Use the git merge-base command to find the common ancestor of the two branches.

git merge-base master your-branch-name

Step 2: Use the git rev-parse command to get the SHA of the branch tip.

git rev-parse your-branch-name
Screenshot-2024-06-03-103825

If the output of two commands is same, then branch has been successfully merged into master.

Approach 4: Using Git hosting services (like GitHub or GitLab)

Go to the Git hosting service you are using, In the section Pull Requests(GitHub) or Merge Requests(GitLab) check the status of your Merge or Pull Request.

Screenshot-2024-06-03-104006

If the status is merged then the branch has been merged into Master.




Reffered: https://www.geeksforgeeks.org


Git

Related
How To Synchronizing A Local Git Repository With A Remote One? How To Synchronizing A Local Git Repository With A Remote One?
How To Update The Date Of An Old Commit In Git? How To Update The Date Of An Old Commit In Git?
How To Visualizing Branch Topology in Git? How To Visualizing Branch Topology in Git?
How To Clone a Repository From Gitlab? How To Clone a Repository From Gitlab?
How To Replace Local Branch With Remote Branch in Git? How To Replace Local Branch With Remote Branch in Git?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14