
Branch Merge in Git
Merging branches in Git is a common practice used to combine the changes from one branch into another. This is often done when you're ready to integrate a feature, fix, or update back into a main branch (like main
or master
).
Here’s a detailed guide on how to merge branches in Git:
1. Understanding Git Merge
The git merge
command combines the changes from one branch into another. You typically use it to merge a feature branch into the main branch.
2. How to Merge Branches
To merge one branch into another, follow these steps:
Step 1: Switch to the Branch You Want to Merge Into
Typically, this will be the main
or master
branch, but it could be any branch. First, make sure you're on the target branch (the branch that will receive the changes from the other branch):
git checkout main # or the branch you want to merge into
Alternatively, you can use git switch
(newer command):
git switch main
Step 2: Merge the Other Branch
Next, run the git merge
command to merge the changes from the branch you want to integrate. For example, if you want to merge the feature/login
branch into main
:
git merge feature/login
This will apply the changes from feature/login
to main
.
3. Merge Conflicts
Sometimes, when merging, Git may encounter conflicts — when changes in the two branches contradict each other. In these cases, Git will stop the merge and mark the conflicted files so you can manually resolve them.
How to Resolve Merge Conflicts:
Git will mark the conflicts in the files with special markers, like this:
<<<<<<< HEAD// Changes in the current branch (main)=======// Changes from the branch being merged (feature/login)>>>>>>> feature/login
Open the conflicted files and decide how to resolve the conflict. You can keep one version, combine them, or make any other necessary changes.
After resolving the conflict, remove the conflict markers (
<<<<<<<
,=======
,>>>>>>>
) and save the file.Add the resolved files to the staging area:
git add <file-name>
Complete the merge by committing the changes:
git commit
Git will usually provide a default commit message for the merge, but you can modify it if needed.
4. Fast-Forward Merges
If the branch you are merging hasn’t diverged from the target branch, Git can perform a fast-forward merge. This happens when the target branch is directly behind the branch being merged (i.e., there are no commits on the target branch that aren’t already on the branch you're merging).
In such a case, Git simply moves the pointer of the target branch forward to the tip of the branch you're merging. There will be no merge commit in the history.
git merge feature/login
If the branch is behind and can be fast-forwarded, the merge will happen automatically without any conflicts.
5. Creating a Merge Commit (Non-Fast-Forward)
If you want to ensure that a merge commit is always created, even in the case of a fast-forward, use the --no-ff
option. This is useful for keeping a clear history of merges, particularly in collaborative projects.
git merge --no-ff feature/login
This forces Git to create a merge commit, even if a fast-forward is possible. It will preserve the history of the feature branch.
6. Merge Strategies
Git provides different strategies for merging branches, depending on the complexity of the changes:
- Recursive (default): This is the default strategy used by Git for most merges. It’s efficient and works well for typical merges.
- Octopus: Used for merging more than two branches at once.
- Ours: This strategy keeps the changes from the current branch and ignores the changes from the merged branch.
- Theirs: This strategy does the opposite, keeping the changes from the merged branch and ignoring the changes in the current branch.
For example, to use the "ours" merge strategy:
git merge -s ours feature/login
7. After Merging
Once the merge is complete, you can push the merged changes to the remote repository:
git push origin main # or the target branch
Example Workflow:
Here’s an example workflow for merging a feature branch into the main
branch:
Create and work on a feature branch:
git checkout -b feature/login# Work on the feature and commit changesgit add .git commit -m "Add login feature"
Switch to the main branch:
git checkout main
Merge the feature branch:
git merge feature/login
Resolve any merge conflicts (if necessary) by editing the files, staging them, and committing the merge.
Push the merged changes to the remote repository:
git push origin main
8. Merge vs Rebase
While merging combines the branches and creates a new merge commit, rebasing rewrites the commit history and can make the history look linear.
- Merge is useful for preserving the history of a feature branch.
- Rebase is typically used when you want to apply your changes on top of another branch, making the history linear and cleaner.
If you’re deciding between merge and rebase, use merge when you want to preserve a branching history and rebase when you want to maintain a clean, linear history.