Managing local changes using “git stash” tool

(Disclaimer: I am quite new to git so I may end-up making a mistake here!)


You are using a third-party software codes via it’s git repo. For some reason, say that software’s developer does not have support for plugin or customization or the support for changes is limited. Somehow you need to make changes to codes but since changes are useful to only you, they reject your pull requests.

You still want to keep using this software and want to preserve your changes across future updates! Here comes git’s stash tool to your rescue! 😉


Our workflow will have mainly 3 steps:

  1. Save your local changes (git stash save)
  2. Upgrade local git repo (git fetch & git checkout [...])
  3. Re-apply (merge) local changes to new codebase (git stash apply)

#1. Saving local changes

If you have local-changes and if you decide to update your codes (git pull/git checkout to latest tag), its quite likely that git will give you a hint to either discard or stash your changes.

Since you do not want to discard them, lets stash them using simply

git stash


git stash save "some useful name/comment"

When you run a command like above, all your local changes gets saves to a stack. You don’t need to worry about where this stack resides, or other internal details. You can imagine that your local modifications are stored in a blackhole! 😉

#2. Upgrade local codebase

This involve mostly fetching remote changes and switching to latest tag (or branch if they don’t like tagging).

Since we are dealing with versions, I feel using git pull is not necessary. git pull does git fetch which is followed by git merger. As we are on a tag (which is not supposed to change by convention) there must be nothing to merge on it from remote.

I generally use following set of commands for upgradation. YMMV.

git fetch
git tag -l
git checkout <latest-tag>

#3. Reapplying local changes to latest codes

Since that developer did not incorporated your changes, you will find latest codes do not have your customizations.

You can reapply them using

git stash apply

Or specifying stash index (in case you did stash save many times)

git stash apply [email protected]{2}

To get exact index number, in case of multiple stash-saves, please use following command

git stash list

My changes got reapplied successfully without any issue. 😉

I guess depending on changes, we may need to do some adjustments as we do when doing merge.

Bonus: Undoing Stash Apply!

Just in case you applies wrong stash, or apply it on a wrong tag/branch, you will need a way to undo it.

If you are maintaing only one stash, then simply run…

git stash show -p | git apply -R

OR specify stash index

git stash show -p [email protected]{2} | git apply -R

If you know any better way, feel free to share with us! 🙂

(image credit: created in-house by our design Yogesh Daphane. Idea was mine!)