Showing difference in Git

Show difference in Git. Show changes in Git. Last updated: 2018-12-17

git diff command shows difference between commits, commit and working tree, commit and staging area, working tree and staging area, etc.

Note: If you want to show difference in a visible tool, git difftool works for you. It accepts the same options as git diff .

The default difftool used by Git is vimdiff, you can set up your own difftool like Beycond Compare, see

Setting up Beyond Compare as difftool and mergetool in Git

Difference between commits

# Difference between two commits
$ git diff <commit> <commmit>

# Examples:
# Difference between HEAD's parent and HEAD
$ git diff HEAD~ HEAD

If you just want to show difference for a file, add -- <file-path> :

# Difference between HEAD's parent and HEAD for index.php
$ git diff HEAD~ HEAD -- ./index.php

Difference between working tree and staging area

In other words, the , the below command show the untagged changes :

# Difference between working tree and staging area
$ git diff

Difference between working tree and commit

# Difference between working tree and commit
$ git diff <commit>

# Examples:

# Difference between working tree and commit specified by '323552e'
$ git diff 323552e

Difference between staging area and commit

# Difference between staging area and commit
$ git diff --cached [<commit>]

# Examples:

# Difference between staging area and HEAD. In other words, show the changes to be committed
$ git diff --cached 

# Difference between staging area and HEAD~
$ git diff --cached HEAD~

Committing only a commit message in Git

Commit only a commit message in Git. Last updated: 2018-12-16

Usually, a commit can not be empty, but you can use --allow-empty option to do this:

# Commit only a commit message.
$ git commit --allow-empty -m 'my commit message'

Undoing add in Git

If you add a file to staging area and want to undo the action, use below command :

# Undo add action for a file
# --, the following parameter will be parsed as a file path
$ git reset HEAD -- <file-path>

# Examples:

# Undo add action for index.php
$ git reset -- index.php

If you want to undo add action for all files added to staging area :

$ git reset HEAD

If fact, git status tells you how to unstage a file :

$ git add index.php
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:    index.php

Undoing a commit in Git

Undo a commit in Git. Last updated: 2018-12-14

Undo a commit that hasn’t been push yet

If you made some mistakes in a commit, you can redo it in a new commit using --amend option. Just fix your mistakes, stage(add) them and make a new commit as below:

# Redo the last commit.
# --amend, replace the last commit with a new one.
$ git commit --amend

If above solution does not suit you, reset can be used to just undo the commit (be careful to use reset):

# Discard the last commit. The commit's modifications in working tree are kept.
$ git reset HEAD~

# Discard the last commit. The commit's modifications in staging area are kept.
$ git reset --soft HEAD~

# Discard the last commit. The commit's modifications in working tree are not kept.
$ git reset --hard HEAD~

Undo a commit that has been pushed

If a commit has been pushed to the remote repository, changing the history in the remote repository is not a good idea. You can let Git to make a new commit which reverses the modifications in the last commit.

git revert command works for you :

# Reverse a commit.
$ git revert -m 1 HEAD

Afterwards, push it to the remote repository again.

Undoing modifications of a file in Git

To discard the changes of a file no matter it is staged or not:

# Undo modifications of a file from working tree and index
$ git checkout -- <file>

# Examples:
# Undo modifications of index.php
$ git checkout -- index.php

If fact, git status tells you how to undo the changes:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   index.php

Note: stash command is a safer way to discard changes, because it allows you to revert the changes again. See stashing changes for how to use it.