W3docs

How to Preview a Merge in Git

This tutorial provides two solutions to the question of previewing the actual merge output in Git. Read about peculiarities of git merge and a lot more.

Sometimes, you want to see what would happen when you merge branches. Here, we will provide a solution to preview the result of the merge.

Merging on a temporary branch

Here we are going to merge our branches on a temporary branch to preview the merge result:

  1. First of all, you need to checkout the branch that you want to merge:
git checkout <branch-name>
  1. Then, create a new temporary branch and checkout to it:
git checkout -b <temporary-branch>
  1. The final step is merging the second branch with the temporary branch:
git merge <second-branch>

Performing these steps allows you to inspect the merge result. If you just want to see what the conflicts are, you can throw away the temporary branch afterward. You can switch back to the main branch and continue your work without leaving merge conflicts in the main branch.

tip

For a safer preview without creating a temporary branch, use git merge --no-commit --no-ff <second-branch>. This stages the merge result without committing it, allowing you to inspect changes or run git merge --abort to cancel.

Standard Merge with Abort

When you want to merge branches and need to handle potential conflicts, follow these steps:

  1. Checkout your branch by running the git checkout command:
git checkout <branch-name>
  1. Next, merge with the second branch:
git merge <second-branch>

If conflicts occur, you can run git merge --abort to reverse the merge actions:

git merge --abort
Warning

If the merge is carried out successfully, you cannot abort it, but can just git reset.

The git merge Command

The git merge command integrates independent lines of development into a single git branch. The <kbd class="highlighted">--abort</kbd> option cancels the merge process and attempts to reconstruct the pre-merge state. However, if there are uncommitted changes at the start of the merge, especially if they were modified after the merge began, the <kbd class="highlighted">--abort</kbd> option may not fully restore the original state.

The git log Command

The <kbd class="highlighted">git log</kbd> command displays committed snapshots. Its primary functions are listing and filtering project history, and searching for specific changes. Unlike git status, which monitors the working directory and staging area, <kbd class="highlighted">git log</kbd> only works with committed history.

The git diff Command

The git diff command compares changes in Git. It takes two input datasets and shows the differences between them. The <kbd class="highlighted">git diff</kbd> command supports two-dot (..) and three-dot (...) operators. Two dots indicate that the diff compares the tips of both branches. Omitting the dots and using a space yields the same result. The three-dot operator uses the shared common ancestor commit between the two branches as the reference for the first input, while the second input remains the tip of the last branch.