mmk
mmk

Reputation: 505

How to take backup of multiple stash?

I understand that we can use git stash show -p > stash.diff to take backup of a stash.

Now I want to take backup of at least 20 stashes. What's a good way to take backup of all the stashes?

Upvotes: 6

Views: 1229

Answers (2)

Tim Kuipers
Tim Kuipers

Reputation: 1764

The following snippet will store all stashes under a filename similar to the stash commit message. Disallowed file name characters are replaced by '_'.

for sha in $(git rev-list -g stash)
do
  git show -p $sha > "\`git show -s --format=%B --max-count=1 $sha  | sed 's/[/:\\?*+%]/_/g'\`.patch"
done

In the following version I only use the first line of the stash message for the file name:

for sha in $(git rev-list -g stash)
do
  git show -p $sha > "\`git show -s --pretty=format:'%s' $sha  | sed 's/[/:\\?*+%]/_/g'\`.patch";
done

Upvotes: 0

Edmund Dipple
Edmund Dipple

Reputation: 2434

This snippet will list the IDs of all your existing stashes, and then create separate diff files for each.

for stash in `git stash list | awk -F':' '{print $1}'`
do
    git stash show $stash -p > $stash.diff
done

Upvotes: 7

Related Questions