How to View Merged and Unmerged Branches in Git
This article discusses how we can list merged and unmerged branches in Git. Git branching encourages the convergent evolution of code.
This is where we create a branch as a temporary space to work on a feature and then merge the branch with its origin. How do we go about listing merged and unmerged branches in Git?
View Merged and Unmerged Branches in Git
We have created multiple branches off the master
branch. How can we list all the branches that we have merged to our master
branch?
We can use the git branch
command to display a list of all the branches merged to the master
branch, as illustrated below.
$ git branch --merged master
This will list all the local branches merged to the master
branch. What if we want to list the branches merged to our HEAD
, i.e., the current checkout branch?
We can run the git branch
command, as shown below.
$ git branch --merged
This will list all the branches that have been merged into the currently checked-out branch. Note that this command only displays local branches.
As illustrated below, you can add the -r
flag to display remote branches.
$ git branch -r merged
Let’s say we wanted to list all the branches that have not yet been merged. How do we go about it?
We can add the --no-merged
flag to the git branch
command, as shown below.
$ git branch --no-merged
This will display all the branches that have not yet been merged into your current HEAD
.
In conclusion, we can use the git branch
command to list merged and unmerged branches, as discussed above. Adding the -r
flag will display the remote branches only.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn