See changes to a specific file using git

2020-02-16 05:08发布

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 git-svn
9条回答
▲ chillily
2楼-- · 2020-02-16 05:47

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.

查看更多
手持菜刀,她持情操
3楼-- · 2020-02-16 05:54

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

查看更多
放荡不羁爱自由
4楼-- · 2020-02-16 05:54

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

git blame <filename>

查看更多
ゆ 、 Hurt°
5楼-- · 2020-02-16 05:55

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.

查看更多
Evening l夕情丶
6楼-- · 2020-02-16 06:00

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
查看更多
祖国的老花朵
7楼-- · 2020-02-16 06:04

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?

查看更多
登录 后发表回答