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

  • Download the Perforce P4Merge Visual Merge Tool from https://www.perforce.com/downloads/integrations
  • 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.
    [merge]
      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
        Merging:
        Test1.txt
        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”

Leave a comment

Your email address will not be published. Required fields are marked *