Git Merge: Combining Branches
git merge
is used to combine changes from one branch into another. This command is essential for integrating work from different branches, such as feature branches or remote branches, into the main project.
1. Basic Git Merge
1.1 Merge Another Branch into Your Current Branch
To merge changes from a different branch into your current branch:
🔹 This combines the commits from branch-name
into your current branch.
🔹 After merging, if there are no conflicts, Git will create a merge commit.
2. Merge Conflicts
2.1 What are Merge Conflicts?
A merge conflict happens when changes in the two branches are in conflict (i.e., both branches modify the same part of a file). Git will not be able to automatically merge the conflicting changes and will ask you to resolve them manually.
2.2 Resolving Merge Conflicts
- Identify Conflicts: After running
git merge branch-name
, Git will show the conflicting files. - Open Conflicted Files: Open the files with conflicts. Git marks conflicts with:
- Resolve Conflicts: Choose which changes to keep (or combine the changes) and delete the conflict markers (
<<<<<<<
,=======
,>>>>>>>
). - Stage the Resolved Files: After resolving the conflicts, stage the resolved files:
- Commit the Merge: Once all conflicts are resolved and staged, commit the merge:
3. Fast-Forward Merges
If the branch you're merging into is directly behind the branch you're merging from (no divergent commits), Git will perform a fast-forward merge.
- In a fast-forward merge, Git will simply move the pointer of the current branch forward to the latest commit on the merged branch, and no merge commit is created.
Example:
🔹 If there are no other commits on the current branch, this will result in a fast-forward merge.
4. Merging Remote Branches
To merge changes from a remote branch, first fetch the changes, and then merge:
🔹 This pulls in the latest changes from the remote branch and merges them into your current branch.
5. Merge Strategies
Git supports several merge strategies to handle specific cases:
5.1 No Fast-Forward Merge
If you want to force a merge commit even if a fast-forward merge is possible, use:
🔹 This creates a merge commit even when it could be fast-forwarded.
5.2 Squash Merging
A squash merge combines all commits from the branch into a single commit before merging:
🔹 After running the command, you’ll need to commit the squashed changes with git commit
.
6. Checking the Merge Status
To check the status of your merge, you can run:
🔹 This will show you if there are any conflicts or if the merge was successful.
7. Summary of git merge
Commands
Action | Command |
---|---|
Merge another branch into the current branch | git merge branch-name |
Resolve merge conflicts | Open conflicted files, resolve, git add path/to/file , git commit |
Force a non-fast-forward merge | git merge --no-ff branch-name |
Squash commits before merging | git merge --squash branch-name |
Check merge status | git status |
8. Conclusion
git merge
is a powerful tool to combine work from different branches. While most merges are straightforward, you should be prepared to resolve conflicts if they arise.
🔹 Need further clarification or a specific example? Let me know! 😊