git compare code example

Example 1: git diff of a file between two commits

git diff <branch_hash> <other_branch_hash> -- <file_path>

Example 2: git diff two commits one file

$ git diff HEAD^^ HEAD main.c
$ # show diff between HEAD(current commit) and HEAD^^(two commits back) for main.c

Example 3: make diff git as commit

A simple way to do it is:

create and checkout branch tmp at branch_a (git branch tmp branch_a && git checkout tmp)
git reset --soft branch_b
git commit
that commit must have all the diff

Example 4: git check diff between two repositories

Just specify your local branch1 or remote origin/branch2
git diff branch1 origin/branch2	#show diff referenced from second branch
git diff branch1 branch2 file.txt #show diff only between file.txt