Imagine this scenario:
# edit two files
git add -p // add hunks from one file
Now when you run git stash -p
, it will again ask you whether you want to stash the hunks that you just selected via git add -p
. Is there some way to configure git to ignore these already-added hunks by default? Most of the time, I don't want to stash stuff that I added already.
By default, running git stash will stash: changes that have been added to your index (staged changes) changes made to files that are currently tracked by Git (unstaged changes)
You have to add the untracked files of the repository by using the “git add” command and run the “git stash” command to save the untracked file and clean the current directory for working by removing the untracked file from the repository folder.
A safer option is to run git stash --all to remove everything but save it in a stash. Assuming you do want to remove cruft files or clean your working directory, you can do so with git clean .
There is a similar example in the manpage:
man git stash:
"Testing partial commits
You can use git stash save --keep-index when you want
to make two or more commits out of the changes in the
work tree, and you want to test each change before
committing:
# ... hack hack hack ...
$ git add --patch foo # add just first part to the index
$ git stash save --keep-index # save all other changes to the stash"*
I can confirm:
If you use git stash -p
(which implies --keep-index
), you still get asked if the changes that are already in the index, should be stashed (as you have described).
So, it seems the manpage is confusing, which is also mentioned elsewhere: https://github.com/progit/progit2/issues/822
To sum it up:
--keep-index
(or -p
which implies --keep-index
) just leaves the index intact. The changes already staged still get inserted into the stash. And AFAIK, there is no way to do what you described.
Or, more precicely (again from the manpage):
With --patch, you can interactively select hunks from
the diff between HEAD and the working tree to be stashed.
The stash entry is constructed such that its index state
is the same as the index state of your repository, and its
worktree contains only the changes you selected interactively.
Alternatives:
There are at least 3 ways you could achieve what you want (more or less):
-p
with git stash. Stash everything (with --keep-index
and possibly --all
, to make sure you've stowed away everything safely). --amend
to change existing commit.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With