简体   繁体   English

来自commit的git checkout文件后,git diff显示没有变化?

[英]After git checkout file from commit, git diff shows no changes?

I am confused on the git checkout behavior. 我对git checkout行为很困惑。 If I wanted to grab a previous version of the file and do something like: 如果我想获取该文件的先前版本并执行以下操作:

git checkout HEAD^^ Xml/Config-Profile.xml
git status

I get: 我明白了:

On branch master
Your branch is up-to-date with 'origin/master'

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified: Xml/Config-Profile.xml

And if I do, git diff, I get no differences. 如果我这样做,git diff,我没有区别。 I've figured out that if I do git diff --cached, it shows the changes, so I guess that means that my checkout command pulled from repository's HEAD^^ commit and updated the 'index only'? 我已经想出如果我做git diff --cached,它会显示更改,所以我想这意味着我的checkout命令从存储库的HEAD ^^提交并更新了'仅限索引'?

I guess my concerns are (and I couldn't gleam info directly from checkout manual pages): 我想我的担忧是(我无法直接从结帐手册页中查看信息):

1) Why is only the index getting updated? 1)为什么只有索引更新? How come it doesn't behave as if I just replaced the Config-Profile.xml file in my working directory with some (previous) copy of the file? 为什么它不像我刚刚用我的工作目录中的Config-Profile.xml文件替换文件的一些(上一个)副本?

2) I guess what makes this confusing for me, and maybe it is all just semantics, is that if I were editing a file in my working directory and decided I didn't like it and did a git checkout, it replacing the file in the working directory. 2)我想是什么让我感到困惑,也许只是语义,如果我在我的工作目录中编辑文件并决定我不喜欢它并做了一个git checkout,它取代了文件工作目录。 After stating that, I'm guessing that git checkout pulls latest file from the index into the working directory, instead of the file from the latest commit in the repository? 在说明之后,我猜测git checkout将索引中的最新文件拉入工作目录,而不是存储库中最新提交的文件?

3) Is there a command that pulls a file from a commit directly into the working directory as if I edited it myself manually to that point? 3)是否有一个命令将文件从提交中直接拉入工作目录,就像我自己手动编辑到那一点一样? Or do I need to create a custom alias? 或者我是否需要创建自定义别名?

Thanks in advance. 提前致谢。

git checkout updates the index and the working tree. git checkout更新索引和工作树。

git diff compares the working tree against the index. git diff将工作树与索引进行比较。

git diff --cached compares the index against HEAD. git diff --cached将索引与HEAD进行比较。

git help checkout says about git checkout <tree-ish> -- <pathspec> : git help checkoutgit checkout <tree-ish> -- <pathspec>

    The <tree-ish> argument can be used to specify a specific tree-ish 
    (i.e. commit, tag or tree) to update the index for the given paths
    before updating the working tree.

If you really only want to update you working tree you can reset the index using: 如果您真的只想更新工作树,可以使用以下命令重置索引:

git reset HEAD Xml/Config-Profile.xml

as suggested by your git status output. 正如您的git status输出所示。

When a commit and paths are given to git checkout , it updates the paths both in the working tree and the index from the commit. 当为git checkout提供提交和路径时,它会更新工作树中的路径和提交中的索引。

git diff , when called without arguments, compares the working tree to the index. git diff ,当不带参数调用时,将工作树与索引进行比较。 Since both are updated with the same file, you don't see any changes. 由于两者都使用相同的文件进行更新,因此您看不到任何更改。

Is there a command that pulls a file from a commit directly into the working directory as if I edited it myself manually to that point? 是否有一个命令将文件从提交中直接拉入工作目录,就像我自己手动编辑到那一点一样? Or do I need to create a custom alias? 或者我是否需要创建自定义别名?

You could do 你可以做到

git show HEAD:Xml/Config-Profile.xml > Xml/Config-Profile.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM