DEV Community

loading...
Cover image for Git and GitHub Essentials - #6 Git Diff

Git and GitHub Essentials - #6 Git Diff

envoy_ profile image Envoy-VC ・2 min read

📌Section 1.1: Show dierences in working branch

git diff

This will show the unstaged changes on the current branch from the commit before it. It will only show changes
relative to the index, meaning it shows what you could add to the next commit, but haven't. To add (stage) these
changes, you can use git add.
If a file is staged, but was modified after it was staged, git diff will show the differences between the current file
and the staged version.

📌Section 1.2: Show changes between two commits

git diff 1234abc..6789def # old new
Enter fullscreen mode Exit fullscreen mode

E.g.: Show the changes made in the last 3 commits:

git diff @~3..@ # HEAD -3 HEAD
Enter fullscreen mode Exit fullscreen mode

Note: the two dots (..) is optional, but adds clarity.
This will show the textual difference between the commits, regardless of where they are in the tree

📌Section 1.3: Show dierences for staged files

git diff --staged
Enter fullscreen mode Exit fullscreen mode

This will show the changes between the previous commit and the currently staged files.
NOTE: You can also use the following commands to accomplish the same thing:

git diff --cached
Enter fullscreen mode Exit fullscreen mode

Which is just a synonym for --staged or

git status -v
Enter fullscreen mode Exit fullscreen mode

Which will trigger the verbose settings of the status command.

📌Section 1.4: Comparing branches

Show the changes between the tip of new and the tip of original:

git diff original new # equivalent to original..new
Enter fullscreen mode Exit fullscreen mode

Show all changes on new since it branched from original:

git diff original...new # equivalent to $(git merge-base original new)...new
Enter fullscreen mode Exit fullscreen mode

📌Section 1.5: Show both staged and unstaged changes

To show all staged and unstaged changes, use:

git diff HEAD
Enter fullscreen mode Exit fullscreen mode

NOTE: You can also use the following command:

git status -vv
Enter fullscreen mode Exit fullscreen mode

📌Section 1.6: Show differences for a specific file or directory

git diff myfile.txt
Enter fullscreen mode Exit fullscreen mode

Shows the changes between the previous commit of the specified file (myfile.txt) and the locally-modified version
that has not yet been staged.

This also works for directories:

git diff documentation
Enter fullscreen mode Exit fullscreen mode

The above shows the changes between the previous commit of all files in the specified directory (documentation/)
and the locally-modified versions of these files, that have not yet been staged.

To show the difference between some version of a file in a given commit and the local HEAD version you can specify
the commit you want to compare against:

git diff 27fa75e myfile.txt
Enter fullscreen mode Exit fullscreen mode

Or if you want to see the version between two separate commits:

git diff 27fa75e ada9b57 myfile.txt
Enter fullscreen mode Exit fullscreen mode

📌Section 1.7: Using meld to see all modifications in the working directory

git difftool -t meld --dir-diff
Enter fullscreen mode Exit fullscreen mode

will show the working directory changes. Alternatively,

git difftool -t meld --dir-diff [COMMIT_A] [COMMIT_B]
Enter fullscreen mode Exit fullscreen mode

will show the differences between 2 specific commits

Discussion (0)

pic
Editor guide