Backup git stash

Suppose you are working on a git repository and you have done some code changes into that which you don’t want to commit. May be there are some setup related changes you have done to run the application locally which don’t need to be committed. One option is to save all these changes into a git stash.

git stash

All your changes will be saved into a stash and your working directory will become clean. In later time if you want those changes into your working directory then you can apply them from the stash.

git apply

Your code have a backup on cloud somewhere like GitHub or Bitbucket or something else. You can clone it from there in case you accidently delete something from your local working directory. But you don’t have any backup of the stash. It is stored only in your local machine. If you accidently delete the stash then no way you can bring that back. Then the only option is to do the code changes manually and create the stash again.

I recently found one solution of this problem. You can save the stash as diff into a text file and backup the file in a cloud storage.

git stash show -p > stash.diff

Also in future you can re-apply the code changes from that diff file into your working directory.

git apply stash.diff

It saves you from a lot of manual work and time in case you loose the stash from your local machine. I always believe that we should backup all of our important things.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s