Git Merge
Git Merge is a straightforward method for integrating changes from one branch into another. When you merge a branch, Git creates a new commit that combines the changes from both the source and target branches. This new commit represents the merge point and has multiple parent commits. Merging preserves the complete history of both branches, including the individual commit details, and is especially useful for incorporating changes from feature branches or team collaboration.
In the following diagram, you will see we have the codebase main branch. We then create our own branch and do some work. Meanwhile on the main branch, others add additional commits, and so we see parallelized commits with diverging histories.
When you do finally merge commit, your history converges with the main branch. The end result when you do a git log
will show the split but it makes it hard to understand the sequence and order of the past.
Benefits of Git Merge:
- Preserves the original branch history.
- Creates a merge commit to merge the changes.
- Maintains a clear separation between the source and target branches.
- Easily identifies the source branch’s contribution to the codebase.
Git Rebase
Git Rebase, on the other hand, is a more advanced technique that allows you to modify the commit history of a branch. Instead of creating a new commit like in Git Merge, Git Rebase moves or “replays” the commits of one branch on top of another. By doing this, it appears as though the changes were made directly on the target branch, resulting in a linear commit history. Rebase is particularly useful for cleaning up a feature branch before merging it into the main branch or for maintaining a clean and streamlined commit history.
Git Rebase replays commits atop of new commits at the HEAD.
This approach prevents merge commits, instead giving us a linear sequenced history, when we run git log
.
Benefits of Git Rebase
- Rewrites the commit history of the branch.
- Moves the commits of one branch to sit on top of another branch.
- Creates a linear and cleaner commit history.
- Provides an opportunity to squash or modify commits during the rebase process.
Key Differences
1. Commit history: Git Merge preserves the entire commit history of both branches, while Git Rebase rewrites the commit history, resulting in a linear and cleaner history.
2. Merge commits: Git Merge creates a merge commit to incorporate changes, whereas Git Rebase does not create explicit merge commits, making the commit history appear as if the changes were made directly on the target branch.
3. Collaboration and branch management: Git Merge is commonly used for collaborative development, where multiple developers contribute to the same branch. Git Rebase is useful for maintaining a clean commit history when working on feature branches (maybe I’ll do an article on this) before merging them into the main branch.
4. Conflict resolution: During Git Merge, conflicts are handled within the merge commit, whereas Git Rebase presents conflicts on each individual commit, allowing for more granular conflict resolution.
How do do a Git Rebase
If you are so inclined to actually do your own Git Rebase, here is a typical scenario:
Let’s assume we have two branches: `main` and `feature`.
1. Start by creating a new directory and initializing a Git repository:
mkdir git-rebase-example
cd git-rebase-example
git init
2. Create a new file named example.txt
and add some content to it. Then, commit the changes to the main
branch:
echo "Initial content" > example.txt
git add example.txt
git commit -m "Initial commit on main branch"
3. Create a new branch called feature
and switch to it:
git checkout -b feature
4. Make some changes to the example.txt
file in the feature
branch:
echo "Feature branch content" >> example.txt
git commit -am "Add feature branch content"
5. Switch back to the main
branch:
git checkout main
6. Make some changes to the example.txt
file in the main
branch:
echo "Main branch content" >> example.txt
git commit -am "Add main branch content"
7. Now, let’s rebase the feature
branch onto the latest commit in the main
branch:
git rebase main
8. If any conflicts occur during the rebase, Git will pause and prompt you to resolve them. Edit the conflicting files manually, save the changes, and then continue the rebase:git add example.txt
git rebase - continue
9. After the rebase is complete, you can verify that the feature
branch has been updated with the latest changes from the main
branch:
git log - oneline - graph - decorate - all
The output should show a linear commit history with the changes from the feature
branch applied on top of the latest commit in the main
branch.
Note: It’s important to exercise caution when using Git Rebase, especially when working with shared branches or branches that have been pushed to a remote repository. Rebasing can modify the commit history and cause conflicts if not used properly.