Jiff Slater
🤔 About
✍️ Contact
30 Jul 2021
These articles have been archived. You may find them useful but I am no longer offering support for them. Check out my latest articles on plkt.io.
Selectively applying changes with Git Stash
21 January 2019

So you just spent 2 hours fixing a bug and made a bunch of other changes along the way. You took notes on your change and have a good mental model of what you did but you’re too lazy to walk through the changes and commit them individually.

This has happened to me a few times and resulted in some awry looking commits with dangling changes.

Turns out git has your back.

First, create a temporary branch that you’ll use to continue the tree of commits.

$ git branch staging-temp

Next, interactively apply the changes

$ git add -i

You can read the documentation for more tips on interactive mode: https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging.

Sometimes, I prefer to do this the other way around. I instead begin by stashing all the irrelevant changes and then iteratively merging them back into HEAD.

$ git stash save --patch

Don’t forget to merge the temporary branch back into main.