git merge
git merge
: merge changes from another branch into the current branch.
The git merge
command is used to integrate changes from one branch into another. The purpose of merging is to combine the work done on different branches into a single branch, typically the main branch of development.
Merging is a common operation in Git workflows, especially when working on features or bug fixes in separate branches. Once the work on a branch is complete and has been reviewed, it can be merged back into the main branch (or any other target branch) to incorporate those changes.
There are two main types of merges in Git:
-
Fast-forward merge: This type of merge occurs when the target branch has not diverged from the source branch. In this case, Git simply moves the target branch pointer to the latest commit of the source branch. No new merge commit is created because the source branch is already ahead of the target branch.
-
Merge commit: When the target branch has diverged from the source branch (i.e., both branches have new commits), Git creates a new commit that combines the changes from both branches. This is called a merge commit, and it has two parent commits: one from the target branch and one from the source branch.
Fast-forward merges are the best option to keep the commit history clean and linear. Curiously, this is often achieved without git merge
by using the git rebase
command. Instead of creating a commit salad, where commits from your branch are mixed with the commits coming from the other branch, rebasing takes all the commits from the other branch and applies all of your commits on top of them, creating a linear history.
Merging can sometimes result in conflicts if the same lines of code have been modified in both branches. In such cases, Git will mark the conflicting lines in the affected files, and you'll need to manually resolve the conflicts by editing the files and deciding which changes to keep.
Remember, before merging, it's often good practice to make sure your current branch is up-to-date with the latest changes from the remote repository by doing a git fetch
followed by a git merge
or git rebase
with the remote branch. This can help prevent conflicts and keep your history cleaner.
git merge
vs git rebase
Imagine you're making a sandwich (branch: feature/sandwich
) while your friend reorganizes the kitchen (branch: feature/kitchen-reorg
). Then you try to combine your work.
With git merge
:
HISTORY: YOU-THEM-THEM-YOU-⚠️-THEM-YOU-⚠️-THEM-YOU-⚠️-THEM-YOU-MERGE
With git rebase
:
HISTORY: THEM-THEM-THEM-THEM-⚠️-YOU-YOU-YOU-YOU
The key difference:
- Merge preserves the chaotic reality of parallel work with multiple conflicts.
- Rebase creates a clean timeline with one conflict resolution point, then smooth sailing.
Both end with the same result (sandwich + organized kitchen), but rebase tells a simpler story by pretending you started after the kitchen was already reorganized.
Examples
Merge the branch new-feature
into the current branch:
git merge new-feature
Merge the branch login-form-bugfix
into the current branch, creating a merge commit even if it could be a fast-forward merge:
git merge --no-ff login-form-bugfix
Abort a merge that has conflicts:
git merge --abort
After resolving conflicts, finalize the merge:
git add .
git merge --continue
Merge a specific commit (rather than a whole branch) into the current branch:
git merge 1a2b3c4d