简体   繁体   English

git diff在.gitignore中但已添加以进行跟踪的文件上

[英]git diff on a file that is in .gitignore but has been added for tracking

For a file 对于文件

config/config.py 配置/ config.py

That file is in .gitignore I need to make changes to add to my own clone. 该文件位于.gitignore中,我需要进行更改以添加到自己的克隆中。 However it is not desirable to change the .gitignore itself: we want most users not to see this file. 但是,不希望更改.gitignore本身:我们希望大多数用户不要看到此文件。

So I did 所以我做了

git add -f config/config.py

However 然而

git diff config/config.py

shows nothing. 什么也没显示。

Correction I had stated in original version that 我在原始版本中指出的更正

git status

shows nothing: that is not true it does show 什么都不显示:那是不正确的,它确实显示

new file:   config/config.py

So it appears the surprise were only with the git diff command? 因此,似乎只有git diff命令令人惊讶吗?

(Off topic a bit: generally the whole .gitignore / tracked vs untracked is a surprisingly tricky/painful process. I have looked at a number of SOF's on this topic and they do not lend confidence here.) (有点偏离主题:通常,整个.gitignore /跟踪与未跟踪都是一个棘手的过程/痛苦的过程。我研究了许多有关此主题的SOF,它们在这里没有信心。)

git diff shows the difference between the staging area (aka index) and the working tree. git diff显示临时区域(又名索引)和工作树之间的差异。 You have just added your file to the staging area, so it is perfectly normal that git diff shows nothing. 您刚刚将文件添加到临时区域,因此git diff显示任何内容是完全正常的。 You want to run 你要跑步

git diff HEAD

instead. 代替。 This will show the diff between the file and HEAD which means "the last commit". 这将显示文件与HEAD之间的差异,表示“最后一次提交”。 If the file was not present in HEAD , then the diff will show the whole file. 如果该文件不存在于HEAD ,那么差异将显示整个文件。

It seems that you ran git rm --cached and did a commit previously, hence the file is not there in HEAD . 看来您运行了git rm --cached并事先进行了提交,因此HEAD没有该文件。 You did a mistake previously, and trying to repair this mistake can be more dangerous if you do not understand what you are doing. 您以前曾犯过一个错误,如果您不了解自己在做什么,尝试修复此错误可能会更加危险。 If you want to try, then learn about rewriting history in Git. 如果要尝试,请了解有关在Git中重写历史记录的信息。

If you want to see the diff with a commit past in history, then run 如果您想查看具有历史记录提交的差异,请运行

git diff <commit-id>

This is completely unrelated from the fact that the file is in .gitignore . 这与文件位于.gitignore的事实完全无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM