Branching and merging in Git

Branching and Merging are very powerful features in Git.

There are may branching strategies, my favorite is well explained at

Pick any model you like and stick to it for a while to better understand and evaluate it. If you do not like that try another one.

In this model when we start to work on a new feature we

  • Create a new branch (let’s call it feature) based on the develop branch and switch to it.
    • git checkout develop
      git branch feature
      git checkout feature
  • Develop the new feature in the feature branch and add the changes to the repository.
    • git add .
      git commit -m "Feature is done in feature branch"
  • Before merging the changes of the feature branch back into the develop branch get the latest changes from the develop branch, apply your changes on top of them and test the application
    • git rebase develop
  • If there are merge conflicts, launch the Merge Tool. See Merging in Git with four panels in Windows  to set up and use a great merge tool.
    • git mergetool
  • It is possible that you will encounter merge conflicts in the same file multiple times. Just repeat the git mergetool command as many times as needed.
  • When you are done with merging, complete the rebase
    • git rebase --continue
  • Add and commit your changes in the feature branch
    • git add .
      git commit -m "Feature is tested in feature branch"
  • When you have tested the code of the new feature, merge it back to the develop branch.
    Switch back to the develop branch and merge the changes from the feature branch.

    • git checkout develop
      git merge feature
  • If there are any merge conflicts, Git will ask you to resolve them.

Merging in Git with four panels in Windows

Git is a great repository for small and large projects. It is easy to create and merge branches to separate code for the features you work on. To make merging easier you can use a free 4 panel merging tool, Perforce P4Merge.


  • Download the Perforce P4Merge Visual Merge Tool from
  • Select the operating system of your computer and click the Download button.

Install Perforce P4Merge

  • Double click the downloaded EXE file
  • Unselect the Administration tool
    P4Merge 01
  • Leave the server as is, we will not use it
    P4Merge 02
  • Just click OK, the address is not important for us
    P4Merge 03
  • Click Next again on the Client Configuration page
    P4Merge 04
  • On a Windows machine P4Merge will be installed at C:\Program Files\Perforce. Git cannot access P4Merge if the path contains a space character, so once the installation is done move the Perforce folder to the root of the C: drive.

Add the Perforce P4Merge settings to the Git config file

  • To use the same tool in every repository navigate to C:\Users\[YOUR USER NAME] and open the .gitconfig file.
  • If you already have [merge] and [mergetool “p4merge”] entries update them, if not, add the following lines. Even on Windows machines you have to use forward slashes (/) in the path for Git to understand it.
      tool = p4merge
    [mergetool "p4merge"]
      cmd = "C:/Perforce/p4merge" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
      keepTemporaries = false
      trustExitCode = false
      keepBackup = false

Solve merge conflicts

  • If you use the Git command line merge the branches. Git is case sensitive.
    • Change to the target branch
      • git checkout master
    • Merge the changes from the feature branch
      • git merge Feature
    • If there is a merge conflict type
      • git mergetool
    • Git will display the name of the file that caused the merge conflict
      • $ git mergetool
        Normal merge conflict for 'Test1.txt':
          {local}: modified file
          {remote}: modified file
        Hit return to start merge resolution tool (p4merge):
    • Hit Enter and the P4Merge opens with four panels
      • The left (Local) panel shows the changes in the current (target) branch,
      • The middle panel shows the original (Base) version of the file before both changes,
      • The right panel shows the coming changes (Remote) that were made in the branch you are merging from,
      • The bottom shows the result of the merge.
    • Change the file to resolve the conflict and click the Save button to save the result.

Merging is safe, because Git creates backup copies of all three version of the file in the same folder. If there are multiple files with merge conflicts Git will open the merge tool for each of them one-by-one.

  • Add the changes to the next commit
    • git add .
  • Commit the changes
    • git commit -m “Merged with P4Merge”