从存储中弹出而不进行索引更改

问题描述

上下文:

我想在git pre-commit钩子中运行rubocop,但是我假设我有一个隐藏的更改会引起抱怨,而一个未更改的更改却以某种方式解决了这个问题,有问题的更改会被rubocop忽略。 >

到目前为止,我的解决方案是:

在运行rubocop之前,运行git stash --keep-index -u以存储所有未分段的更改,然后再运行rubocop

问题:

运行rubocop之后,git stash pop将工作树恢复到其先前的状态,但是暂存所有这些更改(先前未暂存)

我的问题:

是否有一个选项可以在不自动暂存任何已弹出的更改的情况下弹出存储?

是否还有其他方法可以在项目上运行将要提交的程序,而不是在工作树中进行可能的暂存更改?


要重现这种情况:

# Set up a dummy repo
  git init
  echo foo > file
  git add foo && git commit -m "Initial"
# Make a change and stage it
  echo bar >> file
  git add file
# Make another change and *don't* stage it
  echo baz >> file
# This is what I later want to restore:
  git status && git diff && git diff --cached
# Stash only unstaged changes (and ignored / untracked files)
  git stash --keep-index -u
# So far everything is as it should be
  git status && git diff && git diff --cached
# Run whatever script on the code
  rubocop .
# This is where things go wrong though:
  git stash pop
# Both changes are Now staged to be committed ?
  git status && git diff && git diff --cached

解决方法

是否有一个选项可以在不自动暂存任何已弹出的更改的情况下弹出存储?

不是一种选择,不是,但是您可以自己获得效果

index=`git write-tree`
git stash pop
git read-tree $index