421

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?

Satchel
  • 16,414
  • 23
  • 106
  • 192

12 Answers12

631

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.

ijoseph
  • 6,505
  • 4
  • 26
  • 26
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 1
    @sveilleux2 No, just run git diff without any arguments - as the last sentence of the answer says, if no paths are given, it shows all changes. (The brackets on `[filename]` indicate an optional argument.) With the `*` you're letting the shell list all the files, so if you're in a subdirectory you'll only get things in that subdirectory (not the whole repo), and you'll miss changes in hidden files. – Cascabel May 20 '15 at 17:14
  • # show differences between current commit and index # that is, what you're about to commit git diff --cached [filename] Don't you mean: # show differences between current commit and index # that is, what you're about to _push_? git diff --cached [filename] – ofarooq Apr 07 '17 at 12:46
  • 4
    To see differences made after adding a file(i.e, after "git add "), do "git diff --staged [filename]" – Sidtharthan Jul 28 '17 at 03:40
  • Oh, why it's so complicated?! What if we have a couple of dozen files?.. Just complaining to git interface... Yeah, seem, `git add -p` is an alternative to inspect all files. – Kirby Nov 16 '17 at 17:03
  • 1
    @Jefromi - please consider adding `git diff --staged [filename]` in your main answer as that is a situation more often needed. – Lazarus Thurston Nov 28 '17 at 11:55
  • @sanjmeh it was already there, under the primary name (`--cached`). I added a note about the synonym. – Cascabel Nov 28 '17 at 15:17
51

Use git-diff:

git diff -- yourfile
diedthreetimes
  • 4,086
  • 26
  • 38
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • I did realise that `--` is not mandatory. Is it used for presentational purposes of the command right? - I couldn't find some explicit explanation in the `git-diff` link provided – Manuel Jordan Feb 23 '22 at 22:54
12

For me git add -p is the most useful way (and intended I think by git developers?) to review all unstaged changes (it shows the diff for each file), choose a good set of changes that ought to go with a commit, then when you have staged all of those, then use git commit, and repeat for the next commit. Then you can make each commit be a useful or meaningful set of changes even if they took place in various files. I would also suggest creating a new branch for each ticket or similar activity, and switch between them using checkout (perhaps using git stash if you don't want to commit before switching), though if you are doing many quick changes this may be a pain. Don't forget to merge often.

Reed Hedges
  • 1,590
  • 2
  • 15
  • 17
  • So instead of git add file name use git add -p? – Satchel Jun 13 '15 at 06:09
  • 2
    git add -p is a combination of staging, seeing the changes you can stage and selecting them one by one in an interactive manner. See [Commit only part of a file in Git](https://stackoverflow.com/questions/1085162/commit-only-part-of-a-file-in-git) for more on git add -p. – Angelos Asonitis Apr 07 '19 at 12:50
  • There is no such command as 'git add -p' in Git now – Maksym Dudyk Jun 23 '22 at 08:56
8

git diff filename

elmt
  • 1,604
  • 14
  • 24
5

git diff

Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two files on disk.

miku
  • 181,842
  • 47
  • 306
  • 310
  • The quote's a bit too much - by default, it performs the first comparison: between working tree and index. – Cascabel Dec 16 '10 at 02:00
3

Remember, you're committing changes, not files.

For this reason, it's very rare that I don't use git add -p (or the magit equivalent) to add my changes.

Dustin
  • 89,080
  • 21
  • 111
  • 133
  • 2
    git does **NOT** deal with changes -- trying to think about it as if it does is the primary source of confusion and mistakes. git deals with snapshots. – Chris Dodd Jul 05 '17 at 06:12
3
git diff <path>/filename

path can your be complete system path till the file or
if you are in the project you paste the modified file path also
for Modified files with path use :git status

HariKishore K
  • 399
  • 3
  • 6
3

Well, my case when you don't want to care about files list. Just show them all.

When you already ran git add with your files list:

$ git diff --cached $(git diff --cached --name-only)

In more recent versions of git, you can use --staged also, which is a synonym of --cached.

The same can be used for haven't added files but without --cached option.

$ git diff $(git diff --name-only)

Git command alias for "cached" option:

$ git config --global alias.diff-cached '!git diff --cached $(git diff --cached --name-only)'
Safwan
  • 3,300
  • 1
  • 28
  • 33
Kirby
  • 2,847
  • 2
  • 32
  • 42
  • 3
    Thanks for the details git diff --cached --name-only was the command i was looking for.... – Doogle Oct 04 '18 at 11:25
1

Go to your respective git repo, then run the below command:

git diff filename

It will open the file with the changes marked, press return/enter key to scroll down the file.

P.S. filename should include the full path of the file or else you can run without the full file path by going in the respective directory/folder of the file

Yash Bansal
  • 402
  • 5
  • 10
0

You can also use a git-friendly text editor. They show colors on the lines that have been modified, another color for added lines, another color for deleted lines, etc.

A good text editor that does this is GitHub's Atom 1.0.

AskYous
  • 4,332
  • 9
  • 46
  • 82
0

For some paths, the other answers will return an error of the form fatal: ambiguous argument.

In these cases diff needs a separator to differentiate filename arguments from commit strings. For example to answer the question asked you'd need to execute:

$ git diff --cached -- <path-to-file>

This will display the changes between the modified files and the last commit.

On the other hand:

git diff --cached HEAD~3 <path-to-file>

will display the changes between the local version of and the version three commits ago.

diedthreetimes
  • 4,086
  • 26
  • 38
-1

If you are unsure about the files you made the changes and looking to know the changes before committing to your local branch, use git add -p which helps you to verify the changes before accepting to add in your local. Using this query

Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? will give you multiple options like y as to Stage the change, n to not to stage the change etc.

Other options: q - quit; do not stage this hunk nor any of the remaining ones a - stage this hunk and all later hunks in the file d - do not stage this hunk nor any of the later hunks in the file g - select a hunk to go to / - search for a hunk matching the given regex j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk k - leave this hunk undecided, see previous undecided hunk K - leave this hunk undecided, see previous hunk s - split the current hunk into smaller hunks e - manually edit the current hunk ? - print help

Hope it helps..!! Happy Gitting...

HseJaR
  • 31
  • 9