Sourcetree Compare Two Branches

  • Do you have a diff tool installed on your system? I've been a mac user for some years, but as far as I know, Windows does not come with a diff tool preinstalled but Mac does. You can try WinMerge for Windows, I've used it in the past and it's pretty nice, and open source. You can also try the P4 Merge tool from Perforce which is offered for free.
  • You can select 2 commits (cmd click) and compare in sourcetree. To compare 2 branches select the tip of the branches in log view, you can do the same for local and origin too. But I usually use terminal since it is easier.
  • In order to compare two branches easily, you have to use the “git diff” command and provide the branch names separated by dots. $ git diff branch1.branch2 Using this command, Git will compare the tip of both branches (also called the HEAD) and display a “diff” recap that you can use to see modifications.
  • Compare branches. Compare your current branch with another branch - local or remote. View the commits that differ between this branch and the other. Merge the changes into your branch to stay up to date. Learn more here. Download for macOS Download for Windows (64bit) Download for macOS or Windows (msi) Download.

Version control systems are all about managing contributions between multiple distributed authors ( usually developers ). Sometimes multiple developers may try to edit the same content. If Developer A tries to edit code that Developer B is editing a conflict may occur. To alleviate the occurrence of conflicts developers will work in separate isolated branches. The git merge command's primary responsibility is to combine separate branches and resolve any conflicting edits.

Compare branches. Compare your current branch with another branch - local or remote. View the commits that differ between this branch and the other. Merge the changes into your branch to stay up to date.

Understanding merge conflicts

Merging and conflicts are a common part of the Git experience. Conflicts in other version control tools like SVN can be costly and time-consuming. Git makes merging super easy. Most of the time, Git will figure out how to automatically integrate new changes.

Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it. In these cases, Git cannot automatically determine what is correct. Conflicts only affect the developer conducting the merge, the rest of the team is unaware of the conflict. Git will mark the file as being conflicted and halt the merging process. It is then the developers' responsibility to resolve the conflict.

Types of merge conflicts

A merge can enter a conflicted state at two separate points. When starting and during a merge process. The following is a discussion of how to address each of these conflict scenarios.

Git fails to start the merge

A merge will fail to start when Git sees there are changes in either the working directory or staging area of the current project. Git fails to start the merge because these pending changes could be written over by the commits that are being merged in. When this happens, it is not because of conflicts with other developer's, but conflicts with pending local changes. The local state will need to be stabilized using git stash, git checkout, git commit or git reset. A merge failure on start will output the following error message:

Git fails during the merge

A failure DURING a merge indicates a conflict between the current local branch and the branch being merged. This indicates a conflict with another developers code. Git will do its best to merge the files but will leave things for you to resolve manually in the conflicted files. A mid-merge failure will output the following error message:

Creating a merge conflict

In order to get real familiar with merge conflicts, the next section will simulate a conflict to later examine and resolve. The example will be using a Unix-like command-line Git interface to execute the example simulation.

This code example executes a sequence of commands that accomplish the following.

  • Create a new directory named git-merge-test, change to that directory, and initialize it as a new Git repo.
  • Create a new text file merge.txt with some content in it.
  • Add merge.txt to the repo and commit it.

Now we have a new repo with one branch master and a file merge.txt with content in it. Next, we will create a new branch to use as the conflicting merge.

The proceeding command sequence achieves the following:

  • create and check out a new branch named new_branch_to_merge_later
  • overwrite the content in merge.txt
  • commit the new content

With this new branch: new_branch_to_merge_later we have created a commit that overrides the content of merge.txt

This chain of commands checks out the master branch, appends content to merge.txt, and commits it. This now puts our example repo in a state where we have 2 new commits. One in the master branch and one in the new_branch_to_merge_later branch. At this time lets git merge new_branch_to_merge_later and see what happen!

Sourcetree compare two branches near me

BOOM 💥. A conflict appears. Thanks, Git for letting us know about this!

How to identify merge conflicts

As we have experienced from the proceeding example, Git will produce some descriptive output letting us know that a CONFLICT has occcured. We can gain further insight by running the git status command

Sourcetree compare two branches list

The output from git status indicates that there are unmerged paths due to a conflict. The merge.text file now appears in a modified state. Let's examine the file and see whats modified.

Here we have used the cat command to put out the contents of the merge.txt file. We can see some strange new additions

  • <<<<<<< HEAD
  • >>>>>>> new_branch_to_merge_later

Think of these new lines as 'conflict dividers'. The line is the 'center' of the conflict. All the content between the center and the <<<<<<< HEAD line is content that exists in the current branch master which the HEAD ref is pointing to. Alternatively all content between the center and >>>>>>> new_branch_to_merge_later is content that is present in our merging branch.

BranchesTwo

How to resolve merge conflicts using the command line

The most direct way to resolve a merge conflict is to edit the conflicted file. Open the merge.txt file in your favorite editor. For our example lets simply remove all the conflict dividers. The modified merge.txt content should then look like:

Once the file has been edited use git add merge.txt to stage the new merged content. To finalize the merge create a new commit by executing:

Git will see that the conflict has been resolved and creates a new merge commit to finalize the merge.

Git commands that can help resolve merge conflicts

General tools

The status command is in frequent use when a working with Git and during a merge it will help identify conflicted files.

Passing the --merge argument to the git log command will produce a log with a list of commits that conflict between the merging branches.

diff helps find differences between states of a repository/files. This is useful in predicting and preventing merge conflicts.

Tools for when git fails to start a merge

checkout can be used for undoing changes to files, or for changing branches

reset can be used to undo changes to the working directory and staging area.

Tools for when git conflicts arise during a merge

Executing git merge with the --abort option will exit from the merge process and return the branch to the state before the merge began.

Git reset can be used during a merge conflict to reset conflicted files to a know good state

Summary

Merge conflicts can be an intimidating experience. Luckily, Git offers powerful tools to help navigate and resolve conflicts. Git can handle most merges on its own with automatic merging features. A conflict arises when two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other. Conflicts will most likely happen when working in a team environment.

Ready to try branching?

Try this interactive tutorial.

Next up:

Git Merge Strategies

Start next tutorial

Sourcetree Compare Two Branches Chart

In Git, branching is a powerful mechanism that allows you to diverge from the main development line, for example, when you need to work on a feature, or freeze a certain state of a code base for a release, and so on.

Sourcetree Compare Two Branches List

In PyCharm, all operations with branches are performed in the Git Branches popup: To invoke it, click the Git widget in the Status bar (it shows the name of the branch that is currently checked out):

You can also manage branches and perform batch operations with multiple branches in the Branches pane of the Git tool window.

Mark branches as favorites

If you have many branches, you may want to see only your favorite ones. The master branch is marked as a favorite by default. Favorite branches are always displayed at the top of the Branches popup and in the Branches pane of the Git tool window.

  • To mark a branch as a favorite, in the Branches popup, hover the mouse cursor over the branch name, and click the star outline that appears on the left:

    You can also select a branch in the Branches pane of the Git tool window and click on the toolbar.

  • To hide non-favorite branches, click Show Only Favorites at the bottom of the Branches popup.

Sourcetree Compare Two Branches Near Me

Create new branch

Create a new branch from current branch

  1. In the Branches popup, choose New Branch or right-click the current branch in the Branches pane of the Git tool window tool window and choose New Branch.

  2. In the dialog that opens, specify the branch name, and make sure the Checkout branch option is selected if you want to switch to that branch.

    The new branch will start from the current branch HEAD.

Create a new branch from selected branch

  1. In the Branches popup or in the Branches pane of the Git tool window select a local or a remote branch that you want to start a new branch from and choose New Branch from Selected.

  2. In the dialog that opens, specify the branch name, and make sure the Checkout branch option is selected if you want to switch to that branch.

Create a new branch from selected commit

  1. In the Log view, select the commit that you want to act as a starting point for the new branch and choose New Branch from the context menu.

  2. In the dialog that opens, specify the branch name, and make sure the Checkout branch option is selected if you want to switch to that branch.

Check out branches (git-checkout)

Check out a branch as a new local branch

If you want to work on a branch created by someone else, you need to check it out to create a local copy of that branch.

To make sure you have a full list of remote branches, click Fetch in the branches popup:

  1. In the Branches popup or in the Branches pane of the Git tool window, select a branch that you want to check out locally from Remote Branches, or Common Remote Branches if your project has several roots and synchronous branch control is enabled, or from Repositories Remote Branches if it is disabled.

  2. Choose Checkout from the list of actions.

  3. Enter a new name for this branch if necessary, or leave the default name that corresponds to the remote branch, and click OK.

    The new local branch will be set to track the origin remote branch.

Switch between branches

When multitasking, you often need to jump between branches to commit unrelated changes.

  1. In the Branches popup or in the Branches pane of the Git tool window, select the branch that you want to switch to under Local Branches and choose Checkout from the list of available operations.

  2. What happens next depends on whether there are conflicts between your local changes that you have not committed yet, and the branch that you are going to check out:

    • If your working tree is clean (that means you have no uncommitted changes), or your local changes do not conflict with the specified branch, this branch will be checked out (a notification will pop up in the bottom-right corner of the PyCharm window).

    • If your local changes are going to be overwritten by checkout, PyCharm displays a list of files that prevent you from checking out the selected branch, and suggests choosing between Force Checkout and Smart Checkout.

      If you click Force Checkout, your local uncommitted changes will be overwritten, and you will lose them.

      If you click Smart Checkout, PyCharm will shelve uncommitted changes, check out the selected branch, and then unshelve the changes. If a conflict occurs during the unshelve operation, you will be prompted to merge the changes. For details, see Resolve conflicts.

      If you want to clean your working copy using stash instead of shelve, in the Settings/Preferences dialog Ctrl+Alt+S go to Version Control Git and choose Shelve under the Clean working tree using setting.

PyCharm saves your context (a set of opened files, the current run configuration, and breakpoints) provided that the Restore workspace on branch switching option is enabled in the Settings/Preferences dialog Ctrl+Alt+S under Version Control Confirmation. When you switch to a branch, PyCharm automatically restores your context associated with that branch.

Compare branches

Compare a branch with the current branch

If you want to check how a branch has diverged from the current branch, you can compare them.

  1. From the Branches popup or from the Branches pane of the Git tool window, select the branch that you want to compare with the current branch, and choose Compare with Current.

    True image 2021 full movie. A new tab will be added to the Git tool window listing all commits that exist in the selected branch and do not exist in the current branch.

    You can click the Swap Branches link to change which branch is considered as a base against which you are comparing the other branch.

  2. To see a list of all files that are different in the two branches, click Ctrl+A: the Changed Files pane will list all files that contain differences.

Compare a branch with the working tree

Apart from comparing a branch with the current branch, you can compare it against the local state of the current branch. This is useful if you have local uncommited changes.

  • From the Branches popup or from the Branches pane of the Git tool window, select the branch that you want to compare with the local working tree, and choose Show Diff with Working Tree.

    The dialog that opens shows a list of all files that are different in the selected branch compared with the branch that is currently checked out:

    • Files that exist in the selected branch and are missing in the current branch are marked with grey.

    • Files that exist in the current branch but are missing in the selected branch are marked with green.

    • Files that contain differences between the selected and the current branch are marked with blue.

    You can click the Swap Branches link to change which branch is considered as a base against which you are comparing the other branch.

    • To review the differences within a specific file, select it and click on the toolbar, or press Ctrl+D.

    • To apply the entire file contents to the current branch, click (see Apply separate files for details).

Delete branches

After you have integrated the changes from a feature branch into the main line of development, you can delete the branch you do not need anymore.

  1. Check out a branch that you are going to use for further work.

  2. In the Branches popup or from the Branches pane of the Git tool window, select the branch you want to delete and choose Delete.

After you have deleted a branch, a notification will be displayed in the bottom-right corner from which you can restore the deleted branch:

If the branch contained commits that have not yet been merged to its upstream branch or to the current branch, it will still be deleted immediately (equivalent to the git branch --D or git branch --delete --force command), but the notification will also contain a link allowing you to view the unmerged commits.

If the deleted branch was tracking a remote branch, there will also be a link in this notification to remove the remote branch.

If you've closed the notification and later decide you want to restore a deleted branch, the link will be available in the Event Log until you restart PyCharm.

Configure synchronous branch control

If you have a multi-rooted repository, you can configure PyCharm to perform all branch operations (such as checkout, merge, delete, and so on) simultaneously on all roots as if it were a single repository. If an operation fails at least in one of the repositories, PyCharm prevents branches from diverging by suggesting you to roll back this operation in the repositories where it was successful.

  1. In the Settings/Preferences dialog Ctrl+Alt+S select Version Control Git.

  2. Select the Execute branch operations on all roots option (note that this option is only available if your project has multiple roots).