<#.SYNOPSISRestores (applies) a previously saved stash based on full or partial stash name.
.DESCRIPTIONRestores (applies) a previously saved stash based on full or partial stash name and then optionally drops the stash. Can be used regardless of whether "git stash save" was done or just "git stash". If no stash matches a message is given. If multiple stashes match a message is given along with matching stash info.
.PARAMETER messageA full or partial stash message name (see right side output of "git stash list"). Can also be "@stash{N}" where N is 0 based stash index.
.PARAMETER dropIf -drop is specified, the matching stash is dropped after being applied.
.EXAMPLERestore-Stash "Readme change"Apply-Stash MyStashNameApply-Stash MyStashName -dropApply-Stash "stash@{0}"#>function Restore-Stash {[CmdletBinding()][Alias("Apply-Stash")]PARAM ([Parameter(Mandatory=$true)] $message,[switch]$drop)
$stashId = $null
if ($message -match "stash@{") {$stashId = $message}
if (!$stashId) {$matches = git stash list | Where-Object { $_ -match $message }
if (!$matches) {Write-Warning "No stashes found with message matching '$message' - check git stash list"return}
if ($matches.Count -gt 1) {Write-Warning "Found $($matches.Count) matches for '$message'. Refine message or pass 'stash{@N}' to this function or git stash apply"return $matches}
$parts = $matches -split ':'$stashId = $parts[0]}
git stash apply ''$stashId''
if ($drop) {git stash drop ''$stashId''}}
#!/bin/bash
function gstashpop {IFS=""[ -z "$1" ] && { echo "provide a stash name"; return; }index=$(git stash list | grep -e ': '"$1"'$' | cut -f1 -d:)[ "" == "$index" ] && { echo "stash name $1 not found"; return; }git stash apply "$index"}
使用示例:
[~/code/site] on master*$ git stash push -m"here the stash name"Saved working directory and index state On master: here the stash name
[~/code/site] on master$ git stash liststash@{0}: On master: here the stash name
[~/code/site] on master$ gstashpop "here the stash name"
# Apply stash "tagged" $(X) where X is substring of "git stash list" output filtered by output that contains "dev".# I didn't use git stash apply because "dev" tag isn't unique, so it's a need to pop the stash and ensure to create a new one alias set on first stepalias gitsh="git stash pop $(git stash list | grep 'dev' | cut -d ':' -f 1) || echo 'nope'"
#!/usr/bin/env bash
if [ $# -eq 1 ] ; thenNAME=$1elseecho "Please pass exactly one argument, which is the name of the patch file"exit 1fi
git add .
# if previous patch file with the same name exists untrack itif [ -f "$NAME.patch" ] ; thengit rm --cached $NAME.patchfi
# warning: this will diff all changes into a file called NAME.patch and do a hard reset of the current branch
git diff --staged > $NAME.patchgit reset --hard $HEAD