Merging branches in Git is a routine yet crucial task in every developer's workflow. Whether you're integrating a feature branch into main or resolving conflicts, it's important to understand how to do merges properly—across different tools.
In this blog, I’ll walk you through how to perform a Git merge using:
Git command line (for CLI lovers)
TortoiseGit (for GUI enthusiasts on Windows)
IntelliJ IDEA (for JetBrains fans)
Let’s dive in.
---
1. Merging Using Git Command Line
Scenario:
You have a feature branch feature/login and want to merge it into main.
Steps:
# Step 1: Switch to the target branch
git checkout main
# Step 2: Make sure your branch is up to date
git pull origin main
# Step 3: Merge the feature branch
git merge feature/login
If everything goes well, Git will fast-forward or perform a three-way merge automatically.
Handling Conflicts:
If there are merge conflicts, Git will pause and notify you. You'll need to manually edit the conflicted files, then:
# After resolving conflicts
git add .
# Finalize the merge
git commit
Best Practices:
Always pull before you merge.
Run git status to monitor merge progress.
Use git log --graph to visualize merges.
---
2. Merging Using TortoiseGit (Windows GUI Tool)
Steps:
1. Right-click your local repository folder.
2. Select TortoiseGit > Switch/Checkout to move to the target branch (e.g., main).
3. Once on main, again right-click and choose TortoiseGit > Merge.
4. In the merge dialog:
Set the branch to merge from (e.g., feature/login)
Click OK to start the merge.
TortoiseGit will:
Perform the merge
Show a diff viewer if there are conflicts
Handling Conflicts:
TortoiseGit provides a merge tool for resolving conflicts visually. Once resolved:
Save changes
Click Commit to finalize the merge
Best Practices:
Use the built-in diff viewer to understand conflicts
Use Log Dialog to inspect branch history before merging
---
3. Merging Using IntelliJ IDEA
Steps:
1. Go to Git > Branches (bottom-right corner)
2. Checkout your target branch (e.g., main)
3. Again, go to Git > Branches, then hover over feature/login and select Merge into Current
IntelliJ will:
Perform the merge
Show a conflict resolution UI if needed
Handling Conflicts:
If there are conflicts:
IntelliJ opens a 3-way merge editor
You can accept changes from left/right/both
Click Apply after resolving conflicts
Best Practices:
Use IntelliJ’s Version Control tab to view commits
Run tests before and after merge to ensure nothing breaks
---
Final Tips for Clean Merges
Keep commits small and focused: Makes conflicts easier to resolve.
Rebase before merge (optional): For a cleaner history if preferred.
Run tests locally: Validate functionality before pushing merged changes.
Use meaningful commit messages: e.g., Merge feature/login into main
---
Conclusion
Whether you’re a command-line ninja, a TortoiseGit GUI fan, or an IntelliJ power user—Git gives you flexibility in how you merge branches. Choose the method that fits your workflow best, and follow best practices to keep your repository clean and conflict-free.
0 Comments