A note about git merge
Stefan Marx | 2 minutes
Overview:
git merge
is a command that lets you take the independent lines of development created by git branch
and integrate them into a single branch. When you merge two branches, Git tries to automatically join the diverged paths of development.
For the purpose of this explanation:
- The branch you want to merge from is called the
source
branch. - The branch you want to merge into is called the
target
branch.
Steps to merge your development branch into the main branch:
-
Switch to the Target Branch: Before you can merge your development branch into the main branch, you should be on the main branch.
git checkout main
-
Merge the Source Branch: Once you’re on the main branch, you can merge your development branch into it.
git merge development
-
Handle Merge Conflicts (If Any): If there are no conflicting changes, the merge will be successful. However, if there are conflicts, you’ll see a message indicating this. You’ll then need to resolve those conflicts manually.
- Open the files with conflicts. Conflicts will be marked with markers such as
<<<<<<<
,=======
, and>>>>>>>
. - Edit the file to resolve the conflict. Remove the conflict markers once done.
- After resolving conflicts, commit the changes:
git add filename git commit -m "Resolved merge conflicts"
- Open the files with conflicts. Conflicts will be marked with markers such as
-
Push the Changes: If you’re working with a remote repository (e.g., on GitHub, GitLab, etc.), you’ll want to push your merged changes to update the remote main branch.
git push origin main
Fast-forward Merges:
When the main branch hasn’t progressed since you branched off, Git performs a “fast-forward” merge. This means the main branch pointer is just moved forward to point to the same commit as the development branch.
Merge Commits:
If the main branch has progressed since you branched off, Git will create a new commit to capture the merge. This new commit has two parent commits, which signifies that it has merged the histories of the two branches.
Conclusion:
Merging is a fundamental part of working with Git and collaborating with others. Over time, you may also encounter other merge strategies or options (like `rebase`), but the basics presented here will help you get started with standard merging processes.