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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13787109/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:03:58  来源:igfitidea点击:

How to see changes to a file before commit?

git

提问by Houman

I have tried git commit -v

我试过了 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$ 

But it is just showing me that .htaccesshas changed, but not what has changed. How do I achieve this?

但它只是向我展示了.htaccess已经改变的东西,而不是已经改变的东西。我如何实现这一目标?

Update:In Aptana Studio it is possible to see the changes beforeyou commit anything. So back to my question, there must be a way to see the difference to the original state, before you actually commit. Maybe there is a different command for that.

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

回答by John Szakmeister

Make sure you've staged some changes. Otherwise, git commit -vwill show you a block similar to what you posted, but not do anything. You can stage changes manually with git add, or if the files are already versioned, you can use git commit -a -vto stage and commit the changes.

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

For example:

例如:

$ 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")

Staging the change shows the diff with git commit -v:

暂存更改显示差异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.

If you just want to see the diff without committing, use git diffto see unstaged changes, git diff --cachedto see changes staged for commit, or git diff HEADto see both staged and unstaged changes in your working tree.

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

UPDATE: given your edit, what you really want are the git diffderivatives above. I'm not sure how Aptana Studio works. It may not follow the typical command line git flow. On the command line, you'd stage your changes, and then commit. And the above git diffcommands are what you'd use to examine those changes. I typically alias them as git unstaged, git staged, and git bothby adding this to my ~/.gitconfig:

更新:根据您的编辑,您真正想要的是git diff上面的导数。我不确定 Aptana Studio 是如何工作的。它可能不遵循典型的命令行 git flow。在命令行上,您将暂存更改,然后提交。上面的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

回答by lrineau

To see all the diff in tracked files but not staged:

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

git diff

or

或者

git diff path/to/a/given/file

to see the diff only for a file. You can also see the diff in a given sub-directory of your project:

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

git diff path/to/a/dir/

If you have already staged the changes with git add, you can see what patch you have staged with

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

git diff --staged

You can also specify a path with --staged.

您还可以使用--staged.