git add 后文件未显示在 git diff 中。我怎么知道它会被提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19167901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File not shown in git diff after a git add. How do I know it will be committed?
提问by Kitty1911
I had an untracked file which was not appearing in a git diff and when I added it to the 'changes to be committed' area, it still doesn't show up in the git diff. I shows up with a git status -v
when I do a diff against HEAD.
我有一个未跟踪的文件,它没有出现在 git diff 中,当我将它添加到“要提交的更改”区域时,它仍然没有出现在 git diff 中。我展示了一个git status -v
,当我做对头上的差异。
I'm still very new to git, so could anyone please tell me if the file will be committed even if it doesn't show up in a regular diff, as it has been added to the staging area?
我对 git 还是很陌生,所以有人能告诉我文件是否会被提交,即使它没有出现在常规差异中,因为它已被添加到暂存区?
回答by David Cain
If you'd like to see the stagedchanges in a diff, you can still use git diff
, you just need to pass the --staged
flag:
如果您想查看差异中的分阶段更改,您仍然可以使用git diff
,您只需要传递--staged
标志:
david@pav:~/dummy_repo$ echo "Hello, world" > hello.txt
david@pav:~/dummy_repo$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# hello.txt
nothing added to commit but untracked files present
david@pav:~/dummy_repo$ git add hello.txt
david@pav:~/dummy_repo$ git diff
david@pav:~/dummy_repo$ git diff --staged
diff --git a/hello.txt b/hello.txt
new file mode 100644
index 0000000..76d5293
--- /dev/null
+++ b/hello.txt
@@ -0,0 +1 @@
+Hello, world
If you only care about which files are staged, you can of course do a git status
, but git diff --staged --name-only
will give each staged filename on its own line.
如果您只关心暂存哪些文件,您当然可以执行git status
, 但git diff --staged --name-only
会在自己的行中给出每个暂存的文件名。
回答by milkovsky
回答by Michael Durrant
As you've seen. it shows in git status
, so yes it will be committed when you git commit
it.
正如你所见。它显示在 中git status
,所以是的,它会在您提交时提交git commit
。
You may find it helpful to use a visual git tool such as gitx (Mac) or gitg (Linux),
您可能会发现使用 gitx (Mac) 或 gitg (Linux) 等可视化 git 工具很有帮助,
It has nice panels to show you staged, not staged, etc.
它有漂亮的面板,可以向您展示已上演、未上演等。
My screenshot seems spare as I currently have no changes in that status.
我的屏幕截图似乎很空闲,因为我目前该状态没有任何变化。
回答by tom
If you typed in "git add __", the file will be included in your next commit. This commit will be pushed to the server when you run "git push".
如果您输入“git add __”,该文件将包含在您的下一次提交中。当您运行“git push”时,此提交将被推送到服务器。
回答by Xavi
I'd like to add to David Cain's top answer the following nuance: the command...
我想在 David Cain 的最佳答案中添加以下细微差别:命令...
git diff --staged
...shows only the staged changes (as @rogerdpack pointed out).
...仅显示分阶段更改(如@rogerdpack 指出的那样)。
Now, If you're only after generating the diff of a single file then I have nothing to add. But if what you're after is to generate a diff file with "all changes since the latest HEAD", then you probably should (1.) stage all files with changes, (2.) run the diff, (3.) unstage all files, as follows:
现在,如果您只是在生成单个文件的差异之后,那么我没有什么可添加的。但是,如果您想要生成一个带有“自最新 HEAD 以来的所有更改”的差异文件,那么您可能应该 (1.) 暂存所有更改的文件,(2.) 运行差异,(3.) 取消暂存所有文件,如下:
git add -A
git diff --staged HEAD
git reset
Needless to say, bear in mind that these commands will mess around with allyour previously staged and unstaged files, so you should exercise caution if you have lots of temporary files lying around. See the git diff'smanual page for more info.
不用说,请记住,这些命令会弄乱您之前暂存和未暂存的所有文件,因此如果您有大量临时文件,则应谨慎行事。有关更多信息,请参阅git diff 的手册页。