Git merge is a command used to combine changes from different branches into a single branch in your version control system. It essentially merges the work done on separate branches into a unified history.
- Merging Branches: Imagine you’re working on a feature branch while the main codebase continues to be developed on the master branch. When your feature is complete, you can use
git mergeto integrate your changes from the feature branch into the master branch. - Fast-Forward vs. Three-Way Merge: There are two main ways Git can perform a merge:
- Fast-Forward: In simpler scenarios, where your feature branch incorporates changes directly following the master branch’s history, Git can simply fast-forward the master branch pointer to the commit of your feature branch. This is because there’s no conflicting work to merge.
- Three-Way Merge: When there are overlapping changes between the branches (i.e., both branches modified the same files), Git performs a more complex three-way merge. It analyzes the common ancestor commit (the last commit before the branches diverged) and the commits specific to each branch. It then attempts to automatically combine the changes.
- Merge Conflicts: If Git encounters conflicting changes where both branches modify the same lines of code, it will pause the merge process and flag the conflicts. You’ll then need to manually edit the files to resolve the conflicts and tell Git how you want the merged code to look.
In summary, git merge is a powerful tool for integrating work from different development branches. It can be a straightforward process for unconflicting changes, but it might involve manual conflict resolution for overlapping edits.
To merge a branch into another you simple use: git merge [branch]
Example
$ git switch main
Switched to branch 'main'
$ git merge b2
Updating 06f9046..5e58a4b
Fast-forward
file3.txt | 2 ++
file_2.txt | 1 +
2 files changed, 3 insertions(+)
create mode 100644 file3.txt
create mode 100644 file_2.txt