Comparing the differences in the whole project since last commit (or any other):
git diff HEAD~
you can also compare between current state and some older commits:
git diff HEAD~2
- Differences between two commits ago and three comits ago
git diff HEAD~2 HEAD~3
All of the above can be of course applied to a single unique file. For example:
git diff HEAD~2 HEAD~3 main.cpp
Instead of reference to head you can use a sha1 sum assigned to every commit or blob number or tags:
- Single File or whole repo with a blob:
git diff d0403e0 9c20bb9 main.cpp
git diff d0403e0 9c20bb9
the same as can be done with sha1 hashes or tags.
Comparison can of course be done also in between branches.
git diff -b dev []
or even without -b parameter, comparing between state at current
branch and the last commit of given one:
git diff []
In every situation instead of data change you can see what files
really changed. This can be achieved with --name-only parameter passed
to diff. For example, to see files changes between current state and
last commit:
git diff HEAD~ --name-only
Sometimes you only have to check in which commits a single file has
been changed, in that kind of situations "whatchanged" command can be
very handy. It display in which commits a given file has been changed:
git whatchanged
Graphical diff/merge tools
Every of the above command will print the text diff acceptable by
patch tool, but in case of very big diffs not very readable for humans.
There is of course no problem to use any other diff tool of own kind.
The command syntax remains the same except now we use difftool instead
of diff, i.e.:
Launch graphical difftool
meld and compare the contents of main.cpp between current state and last commit:
git difftool -t meld HEAD~ main.cpp
The same applies to merging, in those cases we use mergetool. The
syntax is the same as above. For example, to merge data from file
main.cpp on
branch "dev" to current
branch we use:
git mergetool -t meld dev main.cpp
This is not of course most convenient way. You can configure your
favourite merge/diff tool (from the set which is currently supported by
git, you can also add your own merge tool to
git, but I wont cover this part) to be used as default by setting the
merge.tool variable.:
git config --global merge.tool=meld
git config --global diff.tool=meld
From now on "
meld" will be used as default diff/merge tool and theres no need to pass it with -t parameter to difftool/mergetool command.