How to Preview a Merge in Git

  1. Merging on the temporary branch
  2. Merging on the main branch
  3. The git merge Command
  4. The git log Command
  5. The git diff Command

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 the temporary branch

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

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

Performing these steps above you can throw away the temporary branch if you just want to see what the conflicts are. You can switch to the main branch and get to your work without bothering to have merge conflicts in the main branch.

Merging on the main branch

When you want to merge branches in case there are no conflicts, you should take the steps below:

  1. Check out to your branch by running git checkout command:
    git checkout <branch-name>
  2. Next, merge with the second branch like this:
    git merge <second-branch>

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

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

The git merge Command

The git merge command will integrate the independent lines of development into a single git branch. The --abort option will abort the merge process trying to reconstruct a pre-merge state. But in case of uncommitted changes at the beginning of merge and particularly if those changes were modified after the merge was started, the --abort option won’t be able to reconstruct the original pre-merge changes.

The git log Command

The git log command displays the committed snapshots. Primary functions that git log carries out is listing and filtering the project history, searching for particular changes. The git log only works on the committed history in comparison with git status which controls the working directory and the staging area.

The git diff Command

The git diff has multiple functions. Mainly, it is used to compare changes committed in Git. It allows taking two input data sets and shows what changes are made between them. The git diff command suggests two and three dots operators. The two dots indicate that the diff input is the tip of both branches. You will have the same result if the dots are left out, and space is used between the branches. The three-dot operator transforms the first branch into a reference of the shared common ancestor commit between the two diff inputs. The last input parameter stays the same as the tip of the last branch.


Do you find this helpful?

Related articles