Types and Options of Git Merge
When using Git to create multiple branches, you will eventually need to merge them. There are various methods available for merging branches.
Let’s begin by assuming familiarity with concepts such as commit, push, pull, and fetch, as well as the ability to create branches.
1. The Difference between Merge and Rebase
1.1 Merge
Merge
combines two branches while preserving the flow from both branches. (specifically, 3 way-merge).
Pros
- Merge History Preservation: This method clearly shows in the Git history that the tow branches have been merged.
- Conflict Resolution: If conflicts occur, a merge commit is created, allowing the conflict resolution to be recorded.
Cons
- Cluttered History: The history can become cluttered. If there are many branches or frequent merges, the commit log may become messy.
1
git checkout main
1
git merge feature-branch
1.2 Rebase
Rebase
moves the current branch’s commits onto the latest commit of the target branch.
Pros
- Clear History: It doesn’t leave extra Git commit histories, keeping the commit log linear and clean.
Cons
- Creating New Commit: It generates new commits with new commit hashes, which can make tracking history more difficult.
1
git checkout feature-branch
1
git rebase main
1
2
git checkout main
git merge feature-branch
It should be a fast-forward merge.
2. Types of Git Merge
If you don’t specify a particular merge method, Git will automatically choose the appropriate method.
2.1 Fast-forward merge
A fast-forward merge is used when the two branches being merged can simply be added to the end of the current branch.
- No Merge Commit: The branch pointer is simply moved to new one.
- Occurs in specific situations: This occurs when the branches have not progressed in parallel.
2.2 3-way merge
An 3-way merge is used when the the two branches have different commit.
- Merge Commit: A merge commit is created. It compares the common ancestor commit of the two branches with the latest commit of each branch to generate a new merge commit.
3. Options of Git Merge
--ff
You can merge branches by a fast-forward with the --ff
option.
1
2
git checkout main
git merge feature-branch --ff
--no-ff
You can merge branches using a 3-way merge with the --no-ff
option. This creates a merge commit, preserving the merge history.
1
2
git checkout main
git merge feature-branch --ff
--squash
You can squash the commits from a branch into a single commit. This does not preserve the merge history.
Example
1
2
git checkout main
git merge feature-branch --squash