'How to backup git stash content?

  • I have pending uncommitted works
  • I need to switch to another branch for some urgent work
  • As my current Work in Progress is not finished, I don't want to commit it. I use git stash to put it aside.

Additionally, I would like to backup that stash content on a shared drive. Just for safety. What is an elegant solution to archive the stash content on file?

git


Solution 1:[1]

You can get the diff by running show command:

git stash show -p > stash.diff

This is the diff file which you can use as a backup.

Later you can apply the diff to the existing repository:

git apply stash.diff
# and potentially stash again:
git stash

Solution 2:[2]

I don't want to commit it

Actually, git stash operates by making two commits (sometimes) three, so if you stashed your work, you committed perhaps without even knowing it. But there is nothing wrong most of the time with making a temporary commit. Just add your current work, and then make a temporary commit via:

git commit -m 'WIP'

Then, you can push your branch out to the repository, and that should serve as a backup. When you return to finish the work, you can amend that temporary commit via:

git commit --amend

Feel free to change the message to something more meaningful, but in any case the WIP message can serve as a reminder.

If the branch you are working on could be shared by others, then you could push your branch with the temporary commit to a different location as a backup, e.g.

git push origin local_branch:some_other_location

Solution 3:[3]

As Tim Biegeleisen said, git stash actually creates a commit - it just doesn't move any of your branches around.

You can access your last stashed content through stash@{0} (the second to last through stash@{1}, etc ... git stash list to view a full list of what you can access).

The most straightforward way I see to make a stash more persistent is : create a new branch :

$ git branch wip/adding/new/feature stash@{0}

Now, with a branch pointing at it, this stashed content will not expire, will be transmitted when you clone or fetch from this repo, etc ...

The simplest way to backup a repo is to clone it :

$ cd path/to/external/drive
$ git clone my/repo

# you can then update your backup by running :
$ git fetch         # from the clone

this will save all branches and tags defined in your original repo.

Solution 4:[4]

extending Zybnek's answer.

If you want to backup all of your stashes without making a commit or creating a temp branch:

  1. To count no of stashes git stash list | wc -l

  2. execute for i in {0..n}; do $(git stash show -p stash@{$i} > stash_$i.diff); done

Solution 5:[5]

I managed to backup a stash by doing this (files and paths used by Git Extensions):

  • copy the "stash" file located at git\refs
  • copy the "stash" file located at git\logs\refs
  • copy all the folders from git\objects (except folders "info" and "pack") To recover the files, just copy the files you once have backed up back into the folders mentioned above. After that, the stash will be available for you to use through Git Extensions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Tim Biegeleisen
Solution 3 Community
Solution 4 Bharat Pahalwani
Solution 5