See changes to a specific file using git

GitGit Svn

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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.

Solution 3 - Git

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

Solution 4 - Git

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

git blame <filename>

Solution 5 - Git

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>

Solution 6 - Git

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

Solution 7 - Git

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.

Solution 8 - Git

git diff filename

will give you added lines as + removed lines as -

Solution 9 - Git

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'

Solution 10 - Git

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

Solution 11 - Git

Or if you prefer to use your own gui tool:

git difftool ./filepath

You can set your gui tool guided by this post: https://stackoverflow.com/questions/255202/how-do-i-view-git-diff-output-with-a-visual-diff-program

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMellonView Question on Stackoverflow
Solution 1 - GitGreg HewgillView Answer on Stackoverflow
Solution 2 - GitAJ ZaneView Answer on Stackoverflow
Solution 3 - Gitda Rocha PiresView Answer on Stackoverflow
Solution 4 - GitVineetView Answer on Stackoverflow
Solution 5 - GitTejas PawarView Answer on Stackoverflow
Solution 6 - GitMohideen bin MohammedView Answer on Stackoverflow
Solution 7 - GitLuisBentoView Answer on Stackoverflow
Solution 8 - GitVineela ThonupunuriView Answer on Stackoverflow
Solution 9 - GitBenjamin RDView Answer on Stackoverflow
Solution 10 - GitAouidane Med AmineView Answer on Stackoverflow
Solution 11 - GitElLocoCocoLocoView Answer on Stackoverflow