- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to view merged and unmerged branches in Git?
When we have multiple branches in a git repository, we would need to bring the changes from all the branches to the main line of work that is the master branch. So, if we are currently in master branch and need to see which branches need to be merged, we can use the following commands.
$git branch --no-merged
We would also need to verify which branches are already merged, so that we can delete the unused branches
$git branch --merged
Example
Let us create an example to understand how to view the branches that are merged and unmerged. The following diagram shows that there are three branches − master, feature, bugfix. All three branches are diverged from the initial commit.
We will merge the feature branch to master and will apply --merged and --no-merged options to identify the branches that are already merged and the branches that are yet to be merged.
Let us understand this with an example.
Step 1 − Create a repository, add initial commit and create three branches master, feature, bugfix.
$ git init $ echo hello>hello1.txt $ git add . $ git commit -m 'initial commit' $ git branch feature $ git branch bugfix $ git branch
Output
The output shows that all three branches are created. *master indicates that it is the current branch.
bugfix feature * master
Step 2 − We will make a commit in each branch, so that all our branches are diverged.
$echo second>>second.txt $git add . $git commit -m 'second.txt' $git switch feature $echo third>third.txt $git add . $git commit -m 'third' $git switch bugfix $echo four>four.txt $git add . $git commit -m 'four.txt' $git switch master $git log --oneline --graph --all
Output
The output shows that there are 4 commits and all three branches have diverged from initial commit.
* f5c8a8f (bugfix) four.txt | * 97520af (feature) third |/ | * 075d9c2 (HEAD -> master) second.txt |/ * dcd8e55 initial commit
Step 3 − Check status of the merged and not merged branches from master using option --merged and --no-merged. The command and output are shown below. From the output it is clear that the branches bugfix and feature are to be merged to the master branch.
$ git branch --merged * master //output $ git branch --no-merged bugfix //output feature
Step 4 − Merge the feature branch to the master branch and check the merged and unmerged status of the branches.
$git merge feature Merge made by the 'recursive' strategy. //output third.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 third.txt $ git branch --merged feature //output * master $$ git branch --no-merged bugfix //output
The output shows that bugfix is the only branch to be merged and that the feature branch is already merged.