Keywords: Git Branch Management | Version Control | Branch Merge Verification
Abstract: This article provides a comprehensive guide to verifying branch merge status in Git version control system. It focuses on the working principles and application scenarios of git branch --contains command, with comparative analysis of various branch comparison techniques to help developers safely delete old branches. Includes complete code examples, operation steps, Windows environment special handling, multi-branch verification strategies, and best practices in real workflows.
Core Methods for Git Branch Merge Status Verification
In software development, branch management is a crucial aspect of version control. When needing to delete an old branch, ensuring all commits from that branch have been merged into other branches is an essential safety measure. Git provides multiple tools to verify branch merge status, with git branch --contains being the most direct and effective method.
Detailed Explanation of git branch --contains Command
The git branch --contains command lists all branches that contain commits from the specified branch. Its basic syntax is:
git branch --contains branch-to-delete
When executing this command, Git checks all branches in the repository and returns a list of branches that contain all commits from "branch-to-delete". If the result includes branches other than the one to be deleted, it indicates the branch's commits have been merged into other branches and can be safely deleted.
Practical Application Examples
Suppose we have an old branch named "feature-old" that needs deletion. First, verify its merge status:
git branch --contains feature-old
If the output shows:
develop
feature-old
main
This indicates commits from "feature-old" branch already exist in "develop" and "main" branches, allowing safe deletion. If only "feature-old" is displayed, further checks for unmerged commits are needed.
Supplementary Verification Methods
Besides git branch --contains, Git provides other branch comparison tools:
git log Comparison Method
Use git log to precisely view commit differences between two branches:
git log --no-merges branchA ^branchB
Or use more concise double-dot syntax:
git log branchB..branchA
Both methods show commits present in branchA but not in branchB. The --no-merges option excludes merge commits for clearer results.
Windows Environment Special Handling
In Windows command prompt, the ^ character is an escape character and requires double ^:
git log --no-merges oldbranch ^^newbranch
Multi-Branch Comparison
Git supports comparing multiple branches simultaneously:
git log --no-merges oldbranch1 oldbranch2 ^newbranch1 ^newbranch2
Advanced Application Scenarios
Commit Content Preview
To view commit contents from other branch not in current branch:
git show @..other-branch
Selective Commit Application
Use git cherry-pick to apply specific commits to current branch:
git cherry-pick @..other-branch
Difference Comparison
Use three-dot syntax for branch difference comparison:
git diff newbranch...oldbranch
Best Practice Recommendations
In actual development, recommend combining multiple verification methods:
- First use
git branch --containsfor quick check - For complex cases, use
git logfor detailed commit analysis - Before deleting branches, use
git show-branchfor visual branch relationship review - Establish team standards to ensure regular verification of important branch merge status
Conclusion
The git branch --contains command provides a simple yet powerful verification tool for Git branch management. By mastering these branch comparison techniques, developers can more confidently manage branch lifecycles and avoid accidental loss of important commits. Combined with other Git tools, it enables building more robust version control workflows.