Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore added hunks in `git stash -p`

Tags:

git

git-stash

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.

like image 714
milianw Avatar asked May 08 '18 21:05

milianw


People also ask

Does git stash remove added files?

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)

Do you have to git add before stash?

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.

Does git clean remove stash?

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 .


1 Answers

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):

  • Don't use -p with git stash. Stash everything (with --keep-index and possibly --all, to make sure you've stowed away everything safely).
  • Commit your staged changes before stashing. That way you won't have a diff between HEAD and working tree for these changes you want to omit from stash. But, what if you're not sure you want to commit this yet? You can always make changes later and use --amend to change existing commit.
  • Unstage your changes (remove from index) and then stash.
like image 99
Sybille Peters Avatar answered Sep 30 '22 18:09

Sybille Peters