4

Is there a git porcelain command to revert the working dir state of a staged file to its HEAD state and at the same time keep the staged state of the file? Later I would like to be able to revert the working dir state to the staged state of the file. My initial thought was to use git checkout HEAD -- targetfile, but it resets the staged state of the file too. I see that this is doable with git stash, but it affects other files while I would like to focus on a single file and preserve the state of the others untouched as otherwise it could lead to merge conflicts:

git add targetfile
git stash
... // modify tagetfile in seeking of different task solution, possibly modifying other files too
git checkout HEAD -- targetfile //drop the different solution attempt, keep other modified files if any
git stash apply --index // revert to the stashed staged solution, but produces merge conflict for other modified files

I found a nice article with summary table at the end which lacks the scenario described in this question. In my opinion this should be easily feasible, but I am surprised that staged and working dir states are so tightly coupled - you can reset the staged state for a file while preserving its working dir state, but I have hard time to achieve the opposite.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Dimitar Bonev
  • 3,326
  • 1
  • 15
  • 16

1 Answers1

3

It's not very nice, since it won't get the executable state of the file right, but at a push you could do:

git show HEAD:targetfile > targetfile

Ideally one would want a --working-tree-only option for git checkout HEAD -- targetfile, but I don't know of anything like that.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • It seems `diff` is messed up after following your suggestion: `Binary files a/mydir/index.html and b/mydir/index.html differ` These are html text files and not binary ones, git does not show me what is the difference line by line. – Dimitar Bonev May 02 '13 at 19:43
  • It turned out to be an issue with PowerShell console output encoding. So this is somewhat console dependent, but it will do the job most of the time. http://stackoverflow.com/questions/13675782/git-shell-in-windows-patchs-default-character-encoding-is-ucs-2-little-endian – Dimitar Bonev May 03 '13 at 21:30