Technotes

Technotes for future me

How to compare two git branches

Compare two branches using git diff

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. In short, it will show you all the commits that “branch2” has that are not in “branch1”.

Let’s say for example that you are looking to see the differences between a feature branch (being one commit ahead of master) and the master branch. In order to see what has been modified between master and feature, you would run the following command.

git diff master..feature

diff --git a/file-feature b/file-feature
new file mode 100644
index 0000000..add9a1c
--- /dev/null
+++ b/file-feature
@@ -0,0 +1 @@
+this is a feature file

Compare specific file between two branches

In some cases, you may want to see all changes done to a specific file on the current branch you are working on. In order to see the differences done to a file between two branches, use the “git diff” command, specify the two branches and the filename.

git diff master..feature -- <file>

Let’s say for example that the file that you modified between those two branches is called “README”.

In order to see the differences done to this file, you would run the following command

$ git diff master..feature -- README
# git diff master..origin/fix/feature -- README

diff --git a/README b/README
new file mode 100644
index 0000000..add9a1c
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+this is the README file

https://devconnected.com/how-to-compare-two-git-branches/

git diff of encrypted files

vi ~/.gitconfig

[diff "gpg"]
     textconv = gpg --no-tty -q --decrypt
vi /<repo>/.gitattributes

*.gpg filter=gpg diff=gpg
*.asc filter=gpg diff=gpg

Ansible vault diff in Git

Normally, when you diff an Ansible vault, all you see is gibberish.

With Git, there’s an easy way to associate a textconv with files, so you can run the vaults through ansible-vault view prior to diffing.

Setup your textconv for vault files in either ~/.gitconfig (globally) or ./.git/config (per-project).

vi ~/.gitconfig

[diff "ansible-vault"]
     textconv = ansible-vault view
     cachetextconv = true

Then, either in ~/.config/git/attributes (globally) or in ./.gitattributes (per-project), configure your vault files to use the ansible-vault type.

# or *.vault.yml, or *-vault.yml, or whatever convention you use for vaults
vault.yml diff=ansible-vault

Now, git diff has a lot less gibberish.

Last updated on 31 Jan 2021
Published on 2 Nov 2020
Edit on GitHub