In Git, branching is a powerful feature that allows developers to work on different features or bug fixes simultaneously. However, once the work on different branches is completed, these branches must be merged back into the main branch or integrated into a single branch. Git provides two methods for merging branches - merge and rebase.
The merge
command in Git combines changes from different branches into a single branch. It creates a new commit that includes the changes from both the current branch and the branch being merged.
To merge a branch into the current branch, follow these steps:
Checkout to the branch where you want to merge the changes (usually the main branch).
git checkout main
Use the merge command followed by the branch name you want to merge.
git merge feature-branch
This will create a new merge commit that joins both branches together.
Git will try to automatically merge the changes, but in case of conflicts where changes made on both branches conflict with each other, a manual resolution is required. Git will mark the conflicted files and highlight the conflicting lines.
Once the conflicts are resolved, save the changes and commit the merge commit.
git commit -m "Merge feature-branch into main"
The merge command is simple and provides a clear history of branch integration, but it introduces merge commits that can clutter the branch history.
The rebase
command in Git provides an alternative approach to integrate changes from one branch into another. Instead of creating a new commit like merge does, rebase moves the entire feature branch and re-applies the changes on top of the main branch.
To rebase a branch onto another branch, follow these steps:
Checkout to the branch that you want to rebase.
git checkout feature-branch
Use the rebase command followed by the branch name you want to rebase onto.
git rebase main
Git will pause the rebase process if there are any conflicts. Similar to the merge command, you need to manually resolve the conflicts.
After resolving any conflicts, continue the rebase process by executing the following command:
git rebase --continue
Git will move each commit from the feature branch and apply it on top of the main branch.
Once the rebase is complete, you can safely merge the rebased feature branch into the main branch using the fast-forward merge.
git checkout main
git merge feature-branch
Rebasing provides a more linear and clean commit history as compared to merging. It helps in keeping the commit history readable and avoiding unnecessary merge commits. However, it is essential to note that rebasing modifies the commit history, which can cause issues if the rebased branch is shared with other developers or already pushed to a remote repository.
Merging and rebasing are two methods for integrating changes from one branch into another in Git. The merge command creates new merge commits while preserving the branch history, while the rebase command provides a cleaner commit history by moving changes on top of another branch. Both methods have their advantages and considerations, and the choice between them depends on the specific use case and the desired commit history for the repository.
noob to master © copyleft