How can I see what has changed in a file before committing to git?

Question

I've noticed that while working on one or two tickets, if I step away, I'm not sure what I worked on, what changed, etcetera.

Is there a way to see the changes made for a given file before git add and then git commit?

Answer

You're looking for

git diff --staged

Depending on your exact situation, there are three useful ways to use git diff:

  1. Show differences between index and working tree; that is, changes you haven't staged to commit:
git diff [filename]
  1. Show differences between current commit and index; that is, what you're about to commit (--staged does exactly the same thing, use what you like):
git diff --cached [filename]
  1. Show differences between current commit and working tree:
git diff HEAD [filename]

git diff works recursively on directories, and if no paths are given, it shows all changes.

Why is Git better than Subversion?

GitHub Error Message - Permission denied (publickey)