It's easy to use git stash

February 25, 2014 mgroves 1 comments
Tags: git

I'll admit that git sometime baffles me, and I muddle my way through it. That isn't to say that I don't like using it, or it's not valuable, just that I don't have an encyclopedic knowledge of everything about git. However, an opportunity recently came up to expand my knowledge of git:

I have been trying to make some more open source contributions recently. For one of the projects, I had to make some minor changes to the web.config and csproj files in order to get it working on my local machine. No big deal, except that these are changes that I don't want to commit to my fork, and certainly not to the upstream project.

What I could do is simply copy these changes into a text file somewhere, merge with the upstream project, and then copy/paste the changes so I could go back to work. That's an okay workaround, but it turns out that using git stash might be a better solution. git stash simply takes all the changes to files and stores them "away" into a stash list.

Here's an example, starting with the two files that I modified to get the site working, but don't want to actually commit:

Now, I'll stash everything by using "git stash". After a git stash, if you type "git status", you'll see that the working directory is "clean": no pending changes. If you then type "git stash list", you'll see a list of all your stashes (which is just one, right now).

So, now that my working directory is clear, I can go ahead and, say, "git merge upstream" or something like that without any problem. Once that merge is done, I can bring back the changes with "git stash apply" or, more explicity, "git stash apply stash@{0}", bringing back my working changes.

So that's kinda convenient and way quicker than the whole text file/copy/paste deal.

P.S. If you are struggling with Git, I recommend checking out Git Immersion. It's quick and easy, and I guarantee you that you'll start to see the benefits of using git.

Comments

Avatar for Correl Roush

Posted by Correl Roush on February 25, 2014

It can also be handy to describe what's being stashed, e.g.: git stash save "local environment config". I try to make a habit of that, otherwise I end up having to dig through each stash to see which were my config changes and which were code experiments from different branches that I need to clean up.

Other good resources to learn git would be github's live git tutorial @ http://try.github.io/, git - the simple guide and the Pro Git book

Leave a Comment

*
*
Some HTML will be escaped, you can use StackOverflow-flavored Markdown
Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments

Twitter