git diff [options] [<commit>] [--] [<path>…]
git diff [options] --cached [<commit>] [--] [<path>…]
git diff [options] <commit> <commit> [--] [<path>…]
git diff [options] <blob> <blob>
git diff [options] [--no-index] [--] <path> <path>
|-p, -u, --patch||Generate patch|
|-s, --no-patch||Suppress diff output. Useful for commands like |
|--raw||Generate the diff in raw format|
|--diff-algorithm=||Choose a diff algorithm. The variants are as follows: |
|--summary||Output a condensed summary of extended header information such as creations, renames and mode changes|
|--name-only||Show only names of changed files|
|--name-status||Show names and statuses of changed files The most common statuses are M (Modified), A (Added), and D (Deleted)|
|--check||Warn if changes introduce conflict markers or whitespace errors. What are considered whitespace errors is controlled by |
|--full-index||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|
|--binary||In addition to |
|-a, --text||Treat all files as text.|
|--color||Set the color mode; i.e. use |
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
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.
git diff --staged
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
git status -v
Which will trigger the verbose settings of the
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.
git diff 1234abc..6789def # old new
E.g.: Show the changes made in the last 3 commits:
git diff @~3..@ # HEAD -3 HEAD
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.
git difftool -t meld --dir-diff
will show the working directory changes. Alternatively,
git difftool -t meld --dir-diff [COMMIT_A] [COMMIT_B]
will show the differences between 2 specific commits.
git diff myfile.txt
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
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
Or if you want to see the version between two separate commits:
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 do this:
git diff ada9b57 my_branchname my_changed_directory/
git diff [HEAD|--staged...] --word-diff
Rather than displaying lines changed, this will display differences within lines. For example, rather than:
-Hello world +Hello world!
Where the whole line is marked as changed,
word-diff alters the output to:
You can omit the markers
+} by specifying
--color-words. This will only use color coding to mark the difference:
git config --global merge.conflictstyle diff3
diff3 style as default: instead of the usual format in conflicted sections, showing the two files:
<<<<<<< HEAD left ======= right >>>>>>> master
it will include an additional section containing the original text (coming form the common ancestor):
<<<<<<< HEAD first second ||||||| first ======= last >>>>>>> master
This format makes it easier to understand merge-conflict, ie. in this case locally
second has been added, while remote changed
last, resolving to:
The same resolution would have been much harder using the default:
<<<<<<< HEAD first second ======= last >>>>>>> master
git diff HEAD^ HEAD
This will show the changes between the previous commit and the current commit.
You can diff UTF-16 encoded files (localization strings file os iOS and macOS are examples) by specifying how git should diff these files.
Add the following to your
[diff "utf16"] textconv = "iconv -f utf-16 -t utf-8"
iconv is a program to convert different encodings.
Then edit or create a
.gitattributes file in the root of the repository where you want to use it. Or just edit
This will convert all files ending in
.strings before git diffs.
You can do similar things for other files, that can be converted to text.
For binary plist files you edit
[diff "plist"] textconv = plutil -convert xml1 -o -
Show the changes between the tip of
new and the tip of
git diff original new # equivalent to original..new
Show all changes on
new since it branched from
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
git diff branch1..branch2
Sometimes you just need a diff to apply using patch. The regular
git --diff does not work. Try this instead:
git diff --no-prefix > some_file.patch
Then somewhere else you can reverse it:
patch -p0 < some_file.patch
To view difference between two branch
git diff <branch1>..<branch2>
To view difference between two branch
git diff <commitId1>..<commitId2>
To view diff with current branch
git diff <branch/commitId>
To view summary of changes
git diff --stat <branch/commitId>
To view files that changed after a certain commit
git diff --name-only <commitId>
To view files that are different than 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>