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.