3

I'm looking for a way to delete a single change from a stash compiled of several changes, anyone knows of a way to do it? The best way for to describe it (due to the fact that git commit and git stash are so similar) is that I wish I knew a way to git commit --amend the stash.

It all started when I git stash a few changes before pulling from a shared repository. while I was trying to git pop the code from the stash, I had a conflict (I couldn't resolve) with a file I didn't care for duo to the fact that I had a script to generate it automatically, and was more interested in the other files located in my stash.

While I was looking for a solution I've found this Q : How would I extract a single file (or changes to a file) from a git stash? - it seemed promising, and was also given to someone else asking a similar question to mine, but it didn't help...

Community
  • 1
  • 1
Meir
  • 163
  • 1
  • 5

2 Answers2

1

No, there is no way to delete a single change from a stash - at least not with the high-level commands that git stash provides. As a stash is internally just a commit, you could use the low-level commands to manipulate it, but that is likely more trouble than it is worth.

My rule of thumb is: If things start to get complicated with git stash, it's time to convert the stash to a real commit (typically on a separate branch). Then you can use all the nice git commands for manipulating commits (checkout, reset, rebase etc.).

In your case I'd recommend steps like this:

If git stash pop produces a conflict, clean up your working copy (re-checkout the files with conflict markers, etc.).

Then run:

git stash branch wip-branch

This will create and check out a new branch named "wip-branch", starting from the commit at which the stash was originally created. Then it will apply the changes from the stash. That way there cannot be a conflict, because the stashed changes are re-applied to the commit they are based on.

Now you have a real branch "wip-branch" with your stashed changes, which you can rebase, merge, cherry-pick etc. to your heart's content :-).

Note: While I used to use git stash regularly, I have almost stopped, because creating a real (temporary) commit on a real branch is not much more work, and gives me more options (not to mention that I can use proper commit comments, so I don't forget what the changes mean).

sleske
  • 81,358
  • 34
  • 189
  • 227
  • More detailed than my answer. I like the branch approach. +1 – VonC May 15 '14 at 07:53
  • @VonC: Thanks. Actually (as you can see from edit history), I used to manually create the branch and apply the stash. Answering this question motivated me to read the git-stash manpage, and I found that git-stash actually has a command to convert a stash to a branch. "To teach is to learn", as the proverb goes. – sleske May 15 '14 at 07:56
  • Good catch! `git stash branch` FTW! – VonC May 15 '14 at 07:58
  • @sleske, thanks! i hope i won't have to use your solution, i've already cleared the stash (by accident while trying some "voodoo command" combined with what was written in the Q i linked) cause my code was needed by other people (shared repository). anyway, you can add a message/comment to your stash... – Meir May 15 '14 at 08:27
0

Maybe it is easier to:

  • apply your stash
  • git checkout the file with conflict (to reset it, ignoring any conflict in it)
  • git stash again
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250