Git stash to store frequently used edits

Posted by on Dec 20, 2011 in code | One Comment

Git Stash We work on a couple of projects that require specific changes to test a particular configuration, for example to test against a local server. We generally put this kind of configuration into a plist file and create a target that builds using that plist. Sometimes, however, that’s not convenient–for example, there may be several different potential servers to test against, and it makes more sense to manually edit the server configuration.

In that situation, I found myself frequently using git stash to set aside the server config while I switch branches or pull. It struck me that a stash is a good place to store that kind of configuration permanently. Since a stash, like any commit, stores just the diff, it can generally be re-applied even as the underlying file continues to change over time.

So I’ve started storing these quick configuration changes in stashes that are easy to locate and re-apply.

Here’s how you create such a stash:

  1. start with clean checkout
  2. make the changes you use for dev
  3. git stash save dev-config

Then, when you need to use the stash:

> git stash list
stash@{0}: On master: dev-config
> git stash apply stash@{0}

If you use this frequently, you can create this alias in your .gitconfig to save the step of finding the appropriate stash:

Then you can run:

git apply-stash dev-config

1 Comment

  1. jianhua
    December 24, 2011

    For GIT what’s its major advantage compared with svn? Thanks.

    Reply

Leave a Reply