Git合并特性分支:将branchA合并到branchB并删除branchA的方法
Hey there, let's work through this Git scenario step by step. Since branchB was originally created from branchA, merging branchA into branchB should be smooth—here's exactly how to ensure branchB ends up with all changes from both branches, ready for future merging to master:
Step 1: Switch to branchB and update it
First, make sure you're working on the latest version of branchB:
git checkout branchB # If branchB exists remotely, pull the latest changes first git pull origin branchB
Step 2: Merge branchA into branchB
Since branchB is a child of branchA, Git will usually perform a fast-forward merge (no new merge commit needed) by default. Run:
git merge branchA
If you encounter conflicts:
If Git flags merge conflicts (this happens if the same lines in a file were modified in both branches), follow these steps:
- Open the conflicted files and look for the markers:
<<<<<<<,=======,>>>>>>> - Edit the file to keep the correct content (remove the markers once done)
- Stage the resolved files:
git add <conflicted-file-name> - Complete the merge with a commit:
git commit
Git will auto-populate a merge commit message—you can save and exit the editor to finalize it.
Step 3: Verify the merge was successful
Check the commit history to confirm all changes from branchA are now part of branchB:
git log --oneline
You should see all commits from branchA followed by branchB's commits (or a merge commit if conflicts were resolved).
Step 4: Delete branchA
Delete local branchA:
git branch -d branchA
If Git warns that branchA has unmerged commits (this shouldn't happen if you completed the merge correctly), you can force delete it with:
git branch -D branchA
Delete remote branchA (if it was pushed to the remote repo):
git push origin --delete branchA
Final Note
Once merged, branchB will contain all changes from both branchA and its own development. When you're ready to merge to master, you can follow the same standard workflow: checkout master, pull the latest, merge branchB (resolve any conflicts if needed), then push to remote.
内容的提问来源于stack exchange,提问作者Kim2018

