Rose Perrone
Rose Perrone

Reputation: 63506

How can I apply only some of a git stash?

I want to apply some of the changes in a git stash to one commit, and the rest of the changes to another.

Upvotes: 48

Views: 20669

Answers (4)

Leo
Leo

Reputation: 962

git checkout stash@{0} -- <filename>

You can also do this with a list or a glob of filenames.

Upvotes: 67

Mort
Mort

Reputation: 3549

Apply the patch for only the files you want to change.

git show --first-parent stash@{0} -- <file(s) in question> | git apply

Upvotes: 12

Dmitry Minkovsky
Dmitry Minkovsky

Reputation: 38113

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.

Upvotes: 30

alex
alex

Reputation: 490143

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.

Upvotes: 6

Related Questions