Git ~ Merge - rohit120582sharma/Documentation GitHub Wiki

Merging is Git's way of putting a forked history back together again. The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.

There are two main ways Git will merge: Fast Forward and 3-way.



Preparing to merge

Make sure the receiving branch and the merging branch are up-to-date with the latest remote changes. Execute git fetch to pull the latest remote commits. Once the fetch is completed ensure the receiving branch and the merging branch has the latest updates by executing git pull.

Ensure that HEAD is pointing to the correct merge-receiving branch. If needed, switch to the receiving branch. In our case we will execute git checkout master.

Initiate merge by executing git merge <branch-name> where <branch-name> is the name of the branch that will be merged into the receiving branch.

# Fetch all remote updates and pull all remote commits in specific branches
$ git fetch
$ git pull origin <receiving branch>
$ git pull origin <merging branch>

# Check-out <receiving branch> (master) branch
$ git status
$ git checkout <receiving branch>

# Merge in the <branch name> (new-feature) branch
$ git merge <branch name>


Fast Forward Merge

In certain situations, Git does a fast forward when you merge a branch that is ahead of your checked-out (master) branch at the point of the merge. The master had no commits; It never diverged from the branch.

So, the fast-forward happens when there are no commits on the base branch that occurred after the feature branch was created.

After the merge, users can delete the feature branch as it no longer needed – the base branch points to the same place. This is perfectly acceptable if the branch was never pushed to the remote cloud repository.

# Merge in the new-feature branch
$ git checkout master
$ git merge new-feature
$ git branch -d new-feature

Another variant of the fast forward merge is to use --no-ff option (it stands for no fast-forward).

To retain the history of a merge commit, even if there are no changes to the base branch. In this case, the history looks slightly different, there is an additional commit emphasising the merge. This commit even has the right message informing us about the merged branch.

# Merge in the new-feature branch with a merge commit for record purpose
$ git checkout master
$ git merge --no-ff new-feature


3-way merge

Merging requires a 3-way merge because master branch progresses while the feature branch is in-progress.

However, a fast-forward merge is not possible if the branches have diverged. When there is not a linear path to the target branch, Git has no choice but to combine them via a 3-way merge. 3-way merges use a dedicated commit to tie together the two histories.

# Merge in the new-feature branch
$ git checkout master
$ git merge new-feature
$ git branch -d new-feature


Resolving conflict

If the two branches you're trying to merge both changed the same part of the same file, Git won't be able to figure out which version to use. When such a situation occurs, it stops right before the merge commit so that you can resolve the conflicts manually.

The great part of Git's merging process is that it uses the familiar edit/stage/commit workflow to resolve merge conflicts.



⚠️ **GitHub.com Fallback** ⚠️