Git Merge

Juspreet Kaur
3 min readJan 18

Explaining git merge with visual aid

git merge

Git merge is a command used in Git to combine changes made in multiple branches into a single branch. When you merge one branch into another, you are essentially taking the changes from one branch and applying them to another branch. This can be useful when you want to bring changes from a development branch into a production branch, for example.

Here is the general process for merging one branch into another using Git:

  1. Checkout the branch that you want to merge into. This is the branch that will receive the changes from the other branch.
  2. Use the git merge command followed by the name of the branch that you want to merge. For example: git merge development
  3. Git will then attempt to automatically merge the changes from the development branch into the current branch.
  4. If there are any conflicts between the two branches, Git will prompt you to resolve them before the merge can be completed. Conflicts occur when the same lines of code have been modified in both branches.
  5. Once the conflicts are resolved, you can commit the merge, which will finalize the changes and bring the two branches together.

It’s also important to note that there are two types of merge: Fast-forward and 3-way merge. In fast-forward merge, Git simply moves the branch pointer to the commit that the other branch points to. In 3-way merge, git will create a new commit with two parent commits, one for each branch being merged.

A visual representation of a Git merge can help to understand the process better.

Imagine you have two branches: branchA and branchB. branchA is the branch that you want to merge changes from branchB into.

Before the merge, the branches look like this:

branchA: A - B - C - D (HEAD)
branchB: E - F - G - H (HEAD)

Where each letter represents a commit, and the HEAD indicates the current position of the branch.

When you perform a merge, you are essentially taking the changes from branchB and applying them to branchA. In the case of a Fast-forward merge, Git simply moves the pointer of the branchA to the commit that branchB is pointing to. So the final state of…