47

我试过了git commit -v

ubuntu@ip:~/agile$ git commit -v
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   .htaccess
#
no changes added to commit (use "git add" and/or "git commit -a")
ubuntu@ip:~/agile$ 

但这只是告诉我.htaccess发生了变化,而不是发生了什么变化。我如何实现这一目标?

更新:在 Aptana Studio 中,您可以在提交任何内容之前 查看更改。所以回到我的问题,在你实际提交之前,必须有一种方法可以看到与原始状态的差异。也许有一个不同的命令。

4

2 回答 2

55

要查看跟踪文件中的所有差异但未暂存:

git diff

或者

git diff path/to/a/given/file

仅查看文件的差异。您还可以在项目的给定子目录中查看差异:

git diff path/to/a/dir/

如果您已使用 暂存更改git add,则可以查看已暂存的补丁

git diff --staged

您还可以使用 指定路径--staged

于 2012-12-09T13:40:56.887 回答
35

确保您已经进行了一些更改。否则,git commit -v将向您显示与您发布的内容类似的块,但不执行任何操作。您可以使用 手动暂存更改git add,或者如果文件已经版本化,您可以使用git commit -a -v暂存和提交更改。

例如:

$ echo "more foo" >> foo.txt
$ git commit -v
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   foo.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

暂存更改显示差异git commit -v

:: git add foo.txt
:: GIT_EDITOR=cat git commit -v

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   foo.txt
#
diff --git a/foo.txt b/foo.txt
index 257cc56..a521556 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1,2 @@
 foo
+more foo
Aborting commit due to empty commit message.

如果您只想查看差异而不提交,请使用git diff查看未暂存的更改,git diff --cached查看暂存的提交更改,或git diff HEAD查看工作树中暂存的和未暂存的更改。

更新:根据您的编辑,您真正想要的是git diff上面的衍生物。我不确定 Aptana Studio 是如何工作的。它可能不遵循典型的命令行 git 流。在命令行上,您将暂存更改,然后提交。上面的git diff命令是你用来检查这些变化的。我通常将它们别名为git unstaged, git staged, 并将git both其添加到我的~/.gitconfig:

[alias]
    # show difference between working tree and the index
    unstaged = diff

    # show difference between the HEAD and the index
    staged = diff --cached

    # show staged and unstaged changes (what would be committed with "git commit -a")
    both = diff HEAD
于 2012-12-09T11:57:10.027 回答