How to use GIT to stash (backup or save) changes, compare files in stash for changes, and restore a single file from stash

So say you have software running on a server that you used “git pull” to get from a GitHub project.  You only use the software and need to make minor changes to work with your particular setup, meaning you don’t submit any updates to the GitHub project.  Well what happens when you want to update that software?  All those changes you make is going to cause issues, right?  Well maybe, but that’s where git stash will come in handy for you…In simple terms git stash will take those changes you made and save or “backup” them inside what is called a stash.  This can be used for many different scenarios but to keep it simple in this example I will be showing you how to use git stash to save the changes you made, use git pull to update the software, compare the new files you just downloaded with the files you made changes in, and then restore any of the files you want from the “backup”/stash.

First you will need to go to the directory of the software, and issue this command:

This will save all the changes you made into stash and revert all of the files you made changes on back to their original state.

Next you can update the software

Show Stashed Files

You can now compare those stashed files to the newly updated files using the exact file path given to you above from the git stash show command:

Look for the + and the – to see the changes

If you decide you want to restore a single file from the git stash use this command:

If you want to just restore all of the files you can use

I tried to keep this as simple as possible, there are many different ways to do this, and it gets even trickier when using mutliple stash but the examples above should give you a basic idea of how to use git stash. Once you understand how this works you can move forward and use it in more complex manners…good luck!

Myles

Orlando, FL

Did this post help you?

Give back and rate it for me!

Related Posts