Git stash, diff and patch

September 21, 2011

I recently found myself in a situation where I need to comment out certain lines in my code in order to test a particular set of features.

These commented out lines are only for my local development environment but will be useful in the future if this set of features needs work again.
In this situation, checking them into Git or making a personal .gitignore file are not really suitable solutions.

I have found two good ways of accomplishing this.

Git stash

Starting from a clean checkout, I put my temporary changes in place, then run this command:

1
git stash

Then I continue on with my work. I selectively leave these changes out of all my commits using git add -i then once everything is checked in except my temporary changes, I revert these temporary changes with git checkout -- . so I’m left with a clean repository.

When I need to work on a related feature again, I can list my stashes with:

1
git stash list

Then, apply the most recent stash by running this command:

1
git stash apply

If you have more than one stash, you can specify which stash you want to apply on the command line. See git stash --help to see more information.

Git diff and patch:

diff and patch are very useful tools to be familiar with if you ever deal with text. Git’s diff output is much the same, and so the patch tool is still very handy!

Starting from a clean checkout, I put my temporary changes in place, then run this command:

1
git diff > ~/patches/temp_workaround.patch

Then I continue on with my work. I selectively leave these changes out of all my commits using git add -i then once everything is checked in except my temporary changes, I revert these temporary changes with git checkout -- . so I’m left with a clean repository.

When I need to work on a related feature again, I can apply my patch by running this command from the project root:

1
patch -p1 < ~/patches/temp_workaround.patch

Both of these methods will put my temporary work arounds in place again which saves me trying to work out which lines I needed to comment out everytime. Saving the stashes and/or patch files with descriptive names means you can find and re-use these temporary changes quite easily in the future.

Search and replace, vim and git

Search and replace, vim and git Continue reading

Using netrw instead of NERDTree for Vim

Published on December 28, 2016