698

I know that I can use the git diff command to check the changes, but, as far as I understood, it is directory based. This means it gives all the changes of all files on the current directory.

How can I check only the changes in one specific file? Say, I have changed files file_1.rb, file_2.rb, ..., file_N.rb, but I am only interested in the changes in the file file_2.rb. How do I check these changes then (before I commit)?

nbro
  • 15,395
  • 32
  • 113
  • 196
Mellon
  • 37,586
  • 78
  • 186
  • 264
  • 2
    possible duplicate of [How to view file history in Git?](http://stackoverflow.com/questions/1786027/how-to-view-file-history-in-git) – CharlesB Nov 08 '11 at 09:59
  • 5
    My main question is how to check the difference for a specific file before I commit all the changes. Git log is for committed changes I guess. – Mellon Nov 08 '11 at 10:02
  • 1
    In my experiencie is better to use a visual tool like [GitKraken](https://www.gitkraken.com) – Giovanni Benussi Jun 17 '16 at 14:44

13 Answers13

898

Use a command like:

git diff file_2.rb

See the git diff documentation for full information on the kinds of things you can get differences for.

Normally, git diff by itself shows all the changes in the whole repository (not just the current directory).

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
232

Another method (mentioned in this SO answer) will keep the history in the terminal and give you a very deep track record of the file itself:

git log --follow -p -- file

This will show the entire history of the file (including history beyond renames and with diffs for each change).

In other words, if the file named bar was once named foo, then git log -p bar (without the --follow option) will only show the file's history up to the point where it was renamed -- it won't show the file's history when it was known as foo. Using git log --follow -p bar will show the file's entire history, including any changes to the file when it was known as foo.

Community
  • 1
  • 1
AJ Zane
  • 3,941
  • 1
  • 21
  • 18
  • 6
    Works nicely with --stat to have an overview of the lines added / deleted. – Tom Hale May 10 '17 at 12:48
  • 4
    Yes this answer should rank at least higher, tracks the history, not just the current diff, nor do you have to keep track of all the commit hashes. Pretty simple usage yet very powerfull in changes – Ilhicas Jun 27 '18 at 17:16
  • I used --oneline; and --shortstat for a quick summary of changes. I have long PR descriptions and templates. – Rafs Jun 28 '23 at 10:30
110

You can use gitk [filename] to see the changes log

da Rocha Pires
  • 2,443
  • 1
  • 24
  • 19
  • 3
    This would show the history of the commits on the file, which sometimes might be what you need. – r1k0 Sep 03 '14 at 13:36
39

You can use below command to see who have changed what in a file.

git blame <filename>

Vineet
  • 413
  • 4
  • 6
  • 4
    This is the best answer as it tells you who made what change, and when as well as which commit the change was part of. To filter for a specific change, just do `git blame | grep ` where `` is a short value. For example, to find out who changed `foo` to `bar` in `dist/index.php`, you would use `git blame dist/index.php | grep bar` – Kraang Prime May 11 '17 at 15:38
35

to list only commits details for specific file changes,

git log --follow file_1.rb

to list difference among various commits for same file,

git log -p file_1.rb

to list only commit and its message,

git log --follow --oneline file_1.rb
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
22

You can execute

git status -s

This will show modified files name and then by copying the interested file path you can see changes using git diff

git diff <filepath + filename>
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
Tejas Pawar
  • 690
  • 8
  • 16
19

you can also try

git show <filename>

For commits, git show will show the log message and textual diff (between your file and the commited version of the file).

You can check git show Documentation for more info.

LuisBento
  • 678
  • 9
  • 16
14

git diff filename

will give you added lines as + removed lines as -

5

you can use tig :

sudo apt install tig

you just type : tig in your console

you could have something like this , you could browse the commits and the local changes for every code line

enter image description here

Aouidane Med Amine
  • 1,571
  • 16
  • 17
3

Totally agree with @Greg Hewgill

And if you path includes spaces, you might use, try adding with apostrophe ' or `

git diff 'MyProject/My Folder/My Sub Folder/file_2.rb'

Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
2

Or if you prefer to use your own gui tool:

git difftool ./filepath

You can set your gui tool guided by this post: How do I view 'git diff' output with a visual diff program?

Community
  • 1
  • 1
ElLocoCocoLoco
  • 387
  • 3
  • 11
1

I prefer to use git log with -p

git log -p path/to/file
Sanka Sanjeewa
  • 1,993
  • 14
  • 16
0

You can use git diff by passing a commit range, and a path preceded by --. For example to see the changes made to a file in the last commit of the current branch as compared to the tip of the current branch, you can use the HEAD~1 shorthand:

git diff HEAD~1 -- <path to file>
Jose Quijada
  • 558
  • 6
  • 13