How can I see what has changed in a file before committing to git?
GitGit Problem Overview
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?
Git Solutions
Solution 1 - Git
You're looking for
git diff --staged
Depending on your exact situation, there are three useful ways to use git diff
:
- Show differences between index and working tree; that is, changes you haven't staged to commit:
git diff [filename]
- 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]
- 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.
Solution 2 - Git
Use git-diff
:
git diff -- yourfile
Solution 3 - Git
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.
Solution 4 - Git
git diff filename
Solution 5 - Git
> 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.
Solution 6 - Git
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.
Solution 7 - Git
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
Solution 8 - Git
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)'
Solution 9 - Git
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
Solution 10 - Git
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.
Solution 11 - Git
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