Question

I am confused on the git checkout behavior. 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. 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'?

I guess my concerns are (and I couldn't gleam info directly from checkout manual pages):

1) Why is only the index getting updated? 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?

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. 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?

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? Or do I need to create a custom alias?

Thanks in advance.

Was it helpful?

Solution

git checkout updates the index and the working tree.

git diff compares the working tree against the index.

git diff --cached compares the index against HEAD.

git help checkout says about git 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.

OTHER TIPS

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 diff, when called without arguments, compares the working tree to the index. 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top