769

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

3
  • 3
    possible duplicate of How to view file history in Git? Commented Nov 8, 2011 at 9:59
  • 7
    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. Commented Nov 8, 2011 at 10:02
  • 2
    In my experiencie is better to use a visual tool like GitKraken Commented Jun 17, 2016 at 14:44

13 Answers 13

985

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

Sign up to request clarification or add additional context in comments.

6 Comments

If you want to see a file's changes that you've already "git add"'ed, it's "git diff --cached"
If you want to check a file against an older commit: $ git diff [commit hash] file_2.rb
How to exit from diff view ?
Or use SourceTree.
@GurpreetsinghDhanju Try pressing Q
|
271

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.

3 Comments

Works nicely with --stat to have an overview of the lines added / deleted.
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
I used --oneline; and --shortstat for a quick summary of changes. I have long PR descriptions and templates.
114

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

2 Comments

This would show the history of the commits on the file, which sometimes might be what you need.
gitk where have you been on my life. Would love for a command-line version listing the commits/authors for the specific file. Perhaps there's a flag to the git log <file> I'm unaware of yet.
45

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

git blame <filename>

1 Comment

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 <filename> | grep <searchfor> where <searchfor> 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
38

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

Comments

26

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>

Comments

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.

Comments

16

git diff filename

will give you added lines as + removed lines as -

1 Comment

i think this only works if not committed. but maybe you can specify revisions ?
10

I prefer to use git log with -p

git log -p path/to/file

1 Comment

best one so far!
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

Comments

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'

Comments

3

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>

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.