I want to apply some of the changes in a git stash to one commit, and the rest of the changes to another.
- 1What do you mean with "half"? Half of what? Half of each stash, or half of stash list?Fatih Arslan– Fatih Arslan2013-03-04 23:09:50 +00:00Commented Mar 4, 2013 at 23:09
- 1Could it be by half you mean a portion?bitmask– bitmask2013-03-04 23:10:32 +00:00Commented Mar 4, 2013 at 23:10
4 Answers
git checkout stash@{0} -- <filename> You can also do this with a list or a glob of filenames.
2 Comments
Looks like you can use git checkout -p with a stash reference, like stash@{0}. This will let you choose piece by piece what you want to apply to your work tree from the stash.
After you're done using git checkout -p, the changes you accepted will have been applied to your work tree and added to the cache/index, ready to commit. You can use git checkout -p stash@{0} multiple times, choosing the patches you want and committing however many times you want along the way.
1 Comment
Apply the patch for only the files you want to change.
git show --first-parent stash@{0} -- <file(s) in question> | git apply 8 Comments
-m option (git show -m stash...) to output a diff that git apply understands. Without that I got error: unrecognised input from git apply.-m. Can you point me to some documentation and I'll update my answer.-m is not in the options list on the man page for git-show but it is briefly mentioned in the combined diff format section. It's not clear to me why it works sorry!-m is for git stash show (having the same options as git diff), not for git show stash--first-parent to the show command, it works again. Answer updated.Unstash the stash...
git stash pop ...use git add in patch mode...
git add -p ...and then commit that...
git commit -m "Partial stashed commit" This is what quickly came to my head without reading the docs. Leo's answer has much better way to achieve this.
3 Comments
git stash --keep-index to do this.