Is there a way with to prevent “git stash pop” from marking files as modified?

I don’t think so (timestamp is altered, as mentioned in “GIT: Adding Local Changes to Non-Current Branch” for instance).

The easiest way would be configure your IDE to refresh automatically.

For Eclipse for instance, this would be the setting “Refresh on Access”.


Another approach would be to keep another local repo (one where there is no local modification ever, so no need to stash).
Your pre-commit hook remains in your current repo.

Your hook would use your current index (what you have added), but the other work-tree repo.
For that, you would need to commit with:

git commit --work-tree=/path/to/other/local/repo -m "my commit message"

If the hook doesn’t fail, you can have a post-commit hook (still in your current repo) go to the other repo, and pull the current branch, updating its (pristine) working tree.

cd /path/to/other/local/repo 
git --work-tree=/path/to/other/local/repo --git-dir=/path/to/other/local/repo/.git pull

(Note how your hook, which lives in your first repo, needs to specify work-tree and git-dir of the second repo, in order to work properly).
That is a simplified proposal as it doesn’t take into account the current branch you are working on (it assumes only one branch ‘master’).
But you can detect the branch and adapt from there the hook (with the proper checkout and switch to the right branch).

After the second repo update of its working tree (by the post-commit hook of the first repo), that second repo is ready to serve as a pristine working tree against which your pre-commit hook (of your first and current repo) can safely operate.

Leave a Comment