Reputation: 7046
Context:
I want to run rubocop in a git pre-commit hook, but I assuming I have a stashed change that would cause it to complain, and an unstashed change that somehow fixes this, the offending change would slip by rubocop.
My solution so far:
Before running rubocop, run git stash --keep-index -u
to stash all unstaged changes before running rubocop
The problem:
After running rubocop, git stash pop
restores the working tree to its previous state, but stages all those changes (which were previously unstaged)
My question:
Is there an option to pop the stash without automatically staging any of the popped changes?
or
Is there some other way to run a program on the project as it would be committed, instead of the working tree with possible unstaged changes?
To reproduce the situation:
# 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
Upvotes: 3
Views: 113
Reputation: 60255
Is there an option to pop the stash without automatically staging any of the popped changes?
Not an option, no, but you can get that effect yourself with
index=`git write-tree`
git stash pop
git read-tree $index
Upvotes: 2