I have a git repository with multiple branches.
How can I know which branches are already merged into the master branch?
I have a git repository with multiple branches.
How can I know which branches are already merged into the master branch?
git branch --merged master lists branches merged into master
git branch --merged lists branches merged into HEAD (i.e. tip of current branch)
git branch --no-merged lists branches that have not been merged
By default this applies to only the local branches. The -a flag will show both local and remote branches, and the -r flag shows only the remote branches.
You can use the git merge-base command to find the latest common commit between the two branches. If that commit is the same as your branch head, then the branch has been completely merged.
Note that git branch -d does this sort of thing already because it will refuse to delete a branch that hasn't already been completely merged.
In order to verify which branches are merged into master you should use these commands:
git branch <flag[-r/-a/none]> --merged master list of all branches merged into master.git branch <flag[-r/-a/none]> --merged master | wc -l count number of all branches merged into master.Flags Are:
-a flag - (all) showing remote and local branches-r flag - (remote) showing remote branches only<emptyFlag> - showing local branches onlyfor example: git branch -r --merged master will show you all remote repositories merged into master.
Use git merge-base <commit> <commit>.
This command finds best common ancestor(s) between two commits. And if the common ancestor is identical to the last commit of a "branch" ,then we can safely assume that that a "branch" has been already merged into the master.
Here are the steps
git merge-base <commit-hash-step1> <commit-hash-step2>. More info on git merge-base https://git-scm.com/docs/git-merge-base.
I use git for-each-ref to get a list of branches that are either merged or not merged into a given remote branch (e.g. origin/integration)
Iterate over all refs that match <pattern> and show them according to the given <format>, after sorting them according to the given set of <key>.
Note: replace origin/integration with integration if you tend to use git pull as opposed to git fetch.
origin/integration branchgit for-each-ref --merged=origin/integration --format="%(refname:short)" refs/heads/
# ^ ^ ^
# A B C
branch1
branch2
branch3
branch4
A: Take only the branches merged into the remote origin/integration branch
B: Print the branch name
C: Only look at heads refs (i.e. branches)
origin/integration branchgit for-each-ref --no-merged=origin/integration --format="%(committerdate:short) %(refname:short)" --sort=committerdate refs/heads
# ^ ^ ^ ^
# A B C D
2020-01-14 branch10
2020-01-16 branch11
2020-01-17 branch12
2020-01-30 branch13
A: Take only the branches NOT merged into the remote origin/integration branch
B: Print the branch name along with the last commit date
C: Sort output by commit date
D: Only look at heads refs (i.e. branches)
Here is a little one-liner that will let you know if your current branch incorporates or is out of data from a remote origin/master branch:
$ git fetch && git branch -r --merged | grep -q origin/master && echo Incorporates origin/master || echo Out of date from origin/master
I came across this question when working on a feature branch and frequently wanting to make sure that I have the most recent work incorporated into my own separate working branch.
To generalize this test I have added the following alias to my ~/.gitconfig:
[alias]
current = !git branch -r --merged | grep -q $1 && echo Incorporates $1 || echo Out of date from $1 && :
Then I can call:
$ git current origin/master
to check if I am current.
To check whether a source branch has been merged into the master branch, the following bash command can be used:
git merge-base --is-ancestor <source branch name> master && echo "merged" || echo "not merged"