Git Branch management¶
In a Git project, one can easily identify branches that have not been merged into your current branch or a specific branch. This is crucial for maintaining a clean and understandable project history. Here are the primary commands to view unmerged branches:
Checking for Unmerged Local Branches¶
To see a list of local branches that have not been merged into your current checked-out branch, use the following command:
git branch --no-merged
This command will display all local branches that contain commits that are not present in the current HEAD.
If you want to check for branches that are unmerged into a specific branch (like master
or main
), you can specify the branch name after the --no-merged
flag:
git branch --no-merged main
This is particularly useful to ensure all feature branches have been integrated before a release.
Including Remote Branches in Your Check¶
To check for unmerged remote-tracking branches, you can add the -r
(or --remotes
) flag:
git branch -r --no-merged
This will show you the branches that exist on the remote repository but have not been merged into your current local HEAD.
To get a comprehensive view of both local and remote unmerged branches, use the -a
(or --all
) flag:
git branch -a --no-merged
The Opposite: Viewing Merged Branches¶
Conversely, to see which branches have been merged into your current branch, you can use the --merged
flag. This can be helpful for identifying branches that are safe to delete.
git branch --merged
Similar to --no-merged
, you can also specify a branch name to see which branches have been merged into that particular branch:
git branch --merged master
Getting more insights¶
Here’s a comprehensive guide on how to investigate an unmerged branch to assess the importance of its changes.
1. List the Commits Exclusive to the Unmerged Branch¶
First, you need to see the commits that are on the unmerged branch but not on your main branch (e.g., master
or main
). The git log
command is perfect for this.
To see the commits in your-branch
that are not in master
, use the following command:
git log master..your-branch
For a more concise view, you can use the --oneline
flag:
git log --oneline master..your-branch
This will give you a list of commit hashes and their corresponding messages, providing a quick overview of the work done on that branch.
2. Examine the Content of the Commits¶
Once you have the list of commits, you can inspect the actual changes introduced by each commit.
View a summary of changes:
To see a summary of what files were changed in the commits, you can use git diff
with the --stat
option:
git diff --stat master...your-branch
The three-dot notation (...
) shows the changes on your-branch
since it diverged from master
.
Inspect the full code changes:
To see the complete code changes (the “diff”) between the tip of the unmerged branch and the master
branch, use git diff
:
git diff master..your-branch
To view the changes introduced by a single, specific commit, use git show
:
git show <commit-hash>
3. Check for Cherry-Picked Commits¶
Sometimes, the commits from a feature branch might have been cherry-picked into the main branch. This means the changes are present, but the branch itself is not merged. The git cherry
command is excellent for this, as it compares changesets rather than just commit hashes.
While on your main branch (e.g., master
), run:
git cherry -v your-branch
This command will show a list of commits in your-branch
that have not been applied to the current branch. The -v
flag includes the commit message. Commits with a +
next to them are not in the upstream branch, while those with a -
have an equivalent commit.
Alternatively, you can use git log
with the --cherry-pick
option to see commits that are on one branch but not the other, while ignoring commits that were cherry-picked.
git log --oneline --left-right --cherry-pick master...your-branch
4. Visualize the Branch History¶
A graphical representation of the commit history can make it much easier to understand the relationship between branches.
You can use the following git log
command for a text-based graph:
git log --graph --oneline --all
For a more focused comparison between your current branch and the unmerged branch, you can use git show-branch
:
git show-branch your-branch master
This command provides a detailed view of the commits on each branch and their relationship.
Summary of Key Commands¶
Command | Description |
---|---|
git log master..your-branch |
Lists the commits on your-branch that are not on master . |
git diff master...your-branch |
Shows the code changes on your-branch since it diverged from master . |
git show <commit-hash> |
Displays the detailed changes for a specific commit. |
git cherry -v your-branch |
Lists the commits on your-branch that haven’t been applied to the current branch. |
git log --graph --oneline --all |
Provides a visual graph of the entire commit history. |
Page last modified: 2025-10-04 11:51:12