如何在提交之前查看文件的更改?

时间:2012-12-09 11:49:55

标签: git

我试过了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中,您可以在之前查看之前的更改。回到我的问题,在实际提交之前,必须有一种方法可以看到原始状态的差异。也许对此有不同的命令。

2 个答案:

答案 0 :(得分:12)

确保您已经进行了一些更改。否则,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 unstagedgit stagedgit 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

答案 1 :(得分:7)

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

git diff

git diff path/to/a/given/file

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

git diff path/to/a/dir/

如果您已使用git add暂停了更改,则可以看到您上传的补丁

git diff --staged

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