Android Studio (resp. IntelliJ IDEA) provides a very good diff and merge tool. Android Studio can also be integrated into the terminal.
This makes it ideal for integration into other tools like Git clients. I prefer SourceTree, but that’s just a matter of taste and probably just a matter of getting used to it over the years.
So integrating Android Studio with SourceTree as a diff and merge tool only requires the
studio launcher script.
Install command line launcher
In Android Studio open menu
Create Command Line Launcher… and confirm the installation to the default location with
This creates a script named
/usr/local/bin which should by default be in the
PATH variable. So you can use it from any path in your terminal.
Note: If you have multiple Android Studio versions installed, keep in mind that the script will reference the instance you created the script from.
So, if you want to change Android Studio to a different location, you may need to recreate the script, which replaces the existing script with the new one.
The Android Studio command line tool
studio brings the parameters
So for example you can compare two files by
studio diff first.txt second.txt
Integration with SauceTree is now simply setting the External Visual Diff Tool to
Custom… and Diff Command to
The Arguments must be set to
diff $LOCAL $REMOTE
The same applies to the Merge Tool, but with the Arguments:
merge $LOCAL $REMOTE $BASE $MERGED
The Result should look like this
Now select a modified file in SauceTree and apply
External Diff to it. Android Studio opens a extra diff window like this:
Also in case of a merge conflict, you can use Resolve Conflicts > Start External Merge Tool to bring up a merge window where you can systematically resolve the conflictl.
Configure Git command line
Have a look at your gitconfig - you may want to use your new studio command line launcher to edit the file 😉
If you configured SourceTree beforehand you will see, the SourceTree configuration is embedded directly into your
.gitconfig among other configurations.
... [difftool "sourcetree"] cmd = /usr/local/bin/studio diff $LOCAL $REMOTE path = [mergetool "sourcetree"] cmd = /usr/local/bin/studio merge $LOCAL $REMOTE $BASE $MERGED trustExitCode = true ...
Info: If you have configured SauceTree you should not modify the name behind
mergetool - SourceTree will lose it’s connection and will create new entries in the file.
If you have not and do not want to, you can copy the snippet and replace the name “saucetree” with any other.
Add the following line to use the
difftool configuration from above also for the command line.
[diff] tool = sourcetree
Now you can compare the changes of the modified file
git difftool foo.txt
If you want to skip the
Launch 'sourcetree' [Y/n]? every time you use
git difftool add this lines to your
[difftool] prompt = false
For the configuration as merge tool add the following lines
[merge] tool = sourcetree
You can find more configurations in the Git community documentation.
Info: You might run into the same question I did,
what is the difference between
git diff is different to
This answer to that question on Stack Overflow brings it to the point:
git difftoolwill use an external tool, while
git diffwill show the diff directly in the terminal.
There are also ways to set a GUI diff tool for
git diff. But you might need a terminal diff at some point, so I would keep that for this purpose.