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

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?

This question is related to git

The answer is


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)'

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.


git diff filename


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.


Use git-diff:

git diff -- yourfile

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


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


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.


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.


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.