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

  • 本文关键字:文件 提交 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中,可以在提交任何内容之前查看更改。所以回到我的问题,在你真正承诺之前,必须有一种方法来观察与原始状态的差异。也许有一个不同的命令。

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

git diff

git diff path/to/a/given/file

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

git diff path/to/a/dir/

如果你已经用git add进行了更改,你可以看到你用进行了什么补丁

git diff --staged

也可以使用--staged指定路径。

确保您已经进行了一些更改。否则,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查看工作树中暂存和未暂存的更改。

UPDATE:根据您的编辑,您真正想要的是上面的git diff衍生物。我不知道Aptana工作室是怎么运作的。它可能不遵循典型的命令行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

最新更新