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

后端 未结 2 732
予麋鹿
予麋鹿 2020-11-30 13:19

I implemented a git-hook to scan my commits through pyflakes and jshint, and after reading Yipit\'s commentary on why most pre-commit hooks are broken, I chose to implement

相关标签:
2条回答
  • 2020-11-30 13:49

    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.

    0 讨论(0)
  • 2020-11-30 14:02

    Are you sure the changes are non-existant? If the file is showing as modified, something has changed, even if it's whitespace or the permission (executable state) of a file. You can use git diff to see the changes. You can also review the changes by using the git commit -av command (the -v flag will display the diff of the commit).

    Your IDE is going to keep a current "buffer" state of a file. If that file changes on the disk (which git stash pop|apply will do), the IDE will recognize this and unless you have it set to automatically refresh the file (usually in your IDE settings/preferences), it may prompt you if you want to save the current buffer as a new file -- which is not typically necessary when using git -- or to re-load the buffer with the changed state of the file.

    0 讨论(0)
提交回复
热议问题