Course Tonight

Course Tonight

Did You Know?

We design Docy for the readers, optimizing not for page views or engagement

Chapter 6: Git Diff

Estimated reading: 6 minutes 7 views
Parameter Details
-p, -u, --patch Generate patch. Suppress diff output. Useful for commands like git show that show the patch by default, or to cancel the effect of --patch.
-s, --no-patch Generate the diff in raw format.
--raw Choose a diff algorithm. The variants are as follows: myers, minimal, patience, histogram.
--diff-algorithm= Output a condensed summary of extended header information such as creations, renames, and mode changes.
--summary Show only names of changed files.
--name-only Show names and statuses of changed files. The most common statuses are M (Modified), A (Added), and D (Deleted).
--name-status Warn if changes introduce conflict markers or whitespace errors. What are considered whitespace errors is controlled by core.whitespace configuration. By default, trailing whitespaces (including lines that solely consist of whitespaces) and a space character that is immediately followed by a tab character inside the initial indent of the line are considered whitespace errors. Exits with non-zero status if problems are found. Not compatible with --exit-code.
--check Instead of the first handful of characters, show the full pre- and post-image blob object names on the “index” line when generating patch format output.
--full-index In addition to --full-index, output a binary diff that can be applied with git apply.
--binary Treat all files as text.
-a, --text Set the color mode; i.e. use --color=always if you would like to pipe a diff to less and keep Git’s coloring.

Section 6.1: Show differences 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 6.2: Show changes between two commits
git diff 1234

abc..6789def

For example, to show the changes made in the last 3 commits:

git diff @~3..@

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

Section 6.3: Show differences for staged files

git diff --st

aged

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

Which is just a synonym for --staged, or

git status -v

Which will trigger the verbose settings of the status command.

Section 6.4: Comparing branches

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

git diff original new # equivalent to original..new

Show all changes on new since it branched from original:

git diff original...new # equivalent to $(git merge-base original new)..new

Using only one parameter such as git diff original is equivalent to git diff original..HEAD.

Section 6.5: Show both staged and unstaged changes

To show all staged and unstaged changes, use:

git diff HEAD

NOTE: You can also use the following command:

git status -vv

The difference being that the output of the latter will actually tell you which changes are staged for commit and which are not.

Section 6.6: Show differences for a specific file or directory

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

git diff myfile.txt

This also works for directories. For example, to show 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, use:

git diff documentation

To show the difference between a specific version of a file in a given commit and the local HEAD version, you can specify the commit you want to compare against. For example:

git diff 27fa75e myfile.txt

If you want to see the version between two separate commits, you can use:

git diff 27fa75e ada9b57 myfile.txt

To show the difference between the version specified by the hash ada9b57 and the latest commit on the branch my_branchname for only the relative directory called my_changed_directory/, you can use:

git diff ada9b57 my_branchname my_changed_directory/

Section 6.7: Viewing a word-diff for long lines

To display differences within lines instead of displaying whole lines changed, you can use the --word-diff option:

git diff [HEAD|--staged...] --word-diff

This will alter the output to mark the differences within lines using markers like [-, -] and {+, +}. You can omit these markers and use color coding instead by specifying --word-diff=color or --color-words.

Section 6.8: Show differences between current version and last version

To show the changes between the previous commit and the current commit, use:

git diff HEAD^ HEAD

Section 6.9: Produce a patch-compatible diff

If you need a diff to apply using patch, you can use the following command to generate a patch-compatible diff:

git diff --no-prefix > some_file.patch

You can then apply the diff elsewhere using patch -p0 < some_file.patch.

Section 6.10: Difference between two commits or branches

To view the difference between two branches:

git diff <branch1>..<branch2>

To view the difference between two commits:

git diff <commitId1>..<commitId2>

To view the difference with the current branch:

git diff <branch/commitId>

To view files that changed after a certain commit:

git diff --name-only <commitId>

To view files that are different from a branch:

git diff --name-only <branchName>

To view files that changed in a folder after a certain commit:

git diff --name-only <commitId> <folder_path>

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

To see all modifications in the working directory using meld as the difftool:

git difftool -t meld --dir-diff

Alternatively, to see the differences between two specific commits using meld:

git difftool -t meld --dir-diff [COMMIT_A] [COMMIT_B]

Section 6.12: Diff UTF-16 encoded text and binary plist files

To diff UTF-16 encoded files (e.g., localization strings files in iOS and macOS), you can specify how Git should handle these files. Add the following to your ~/.gitconfig file:

[diff "utf16"]
textconv = "iconv -f utf-16 -t utf-8"

Then, edit or create a .gitattributes file in the root of the repository or ~/.gitattributes. For example, to convert all files ending in .strings before Git diffs:

*.strings diff=utf16

You can do similar things for other files that can be converted to text.

For binary plist files, you can edit .gitconfig:

[diff "plist"]
textconv = plutil -convert xml1 -o -

And .gitattributes:

*.plist diff=plist

Leave a Comment

Share this Doc

Chapter 6: Git Diff

Or copy link

CONTENTS