Git is a tool that many developers use to track changes and collaborate on projects. Merging branches is something you might do often, like when you need to add the work from the main part of your project into the part where you’re adding a new feature.
Sometimes Git can combine changes from different branches all by itself. But there are times when you might want to have more say over what happens. This guide will show you how to merge changes without letting Git decide everything for you.
What to do
- Understand Manual Merge
- Explore Git Options
- Perform Manual Merge
- Resolve Conflicts
- Stage Resolved Changes
- Commit the Merge
Step-by-Step: Manual Merge in Git
When Git puts branches together, it usually tries to do it automatically. But if you think there might be conflicts or you just want to check everything yourself, you can merge things manually. Here’s how to do that:
Step 1: Switch to Your Feature Branch
First, go to the branch where you’re working on your new feature:
git checkout feature-branch
Step 2: Start Merging Without a Commit
Now, bring in the changes from the main branch but don’t finish the merge right away:
git merge --no-commit --no-ff main
Using --no-commit
stops Git from finishing the merge automatically, and using --no-ff
makes sure Git records this merge even if it could have just moved everything forward without combining histories.
Step 3: Look for Conflicts and Fix Them
Git will tell you if any files have conflicts—that’s where both branches have changes that can’t be put together easily. If that happens, open those files in a text editor or an IDE like Visual Studio Code, and sort out the differences yourself.
Step 4: Get Ready to Finish the Merge
After fixing any conflicts, let Git know they’re taken care of:
git add .
This step shows that you’ve resolved the issues.
Step 5: Complete the Merge
Last, make the merge official by committing it:
git commit -m "Merge main into feature-branch"
Now you’ve merged the main branch into your feature branch on your terms.
Merging manually with --no-commit
and --no-ff
helps when you want to double-check everything before combining branches. It’s helpful because you can spot and fix issues yourself instead of letting Git decide.
Always talk with your team when you’re merging manually. Keep everyone updated on the process and any problems you run into. This way, everyone knows what’s happening, and you keep the workflow smooth.
Conclusion
Manual merging in Git provides precise control over code integration and is beneficial for resolving conflicts and ensuring each change is intentional. Despite being more time-consuming, it enhances clarity and accuracy in the development process.