Skip to content

Instantly share code, notes, and snippets.

@rca
Created Sep 30, 2012
Embed
What would you like to do?
Git stash helper functions.
#!/bin/bash
#
# Helper functions around "git stash"
#
# Use this file by sourcing it into your shell environment. For example:
#
# source /path/to/gstash.sh
#
# Once sourced in you can run the commands:
#
# - gstash: shorthand for "git stash"
# - gstash_dirtypop: pop a stash into a dirty working directory
# - gstash_keep: stash all but the files specified
# - gstash_some: stash the files specified
#
# Copyright (C) 2012 Roberto Aguilar <roberto.c.aguilar@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# function to specify stashes shorthand.
#
# instead of running:
#
# git stash pop stash@{2}
#
# you can run the less finger controntion version:
#
# gstash pop 2
#
# the standard command "git stash" is also supported by simply calling the function:
#
# gstash
function gstash
{
# funky parameter manipulation from (thanks, tim!):
# http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2007-11/msg00017.html
index=${!#}
#echo "index ${index}"
test ${index} -gt -1 2> /dev/null
status=$?
if [ $status -ne 0 ]; then
echo git stash $@
git stash $@
else
#echo git stash ${@:1:$#-1} stash@{${index}}
git stash ${@:1:$#-1} stash@{${index}}
fi;
}
# apply a stash even with uncommitted changes.
#
# this will commit all dirty files, will pop the stash, and then reset the
# commit back to the working copy. for some reason "git reset" does not have
# the same limitation / safety check as "git stash"
function gstash_dirtypop
{
git commit -a -m'gstash_dirtypop temp commit' && \
git stash pop "$@" && \
git reset HEAD^
}
# stash all the files not given
function gstash_keep
{
# just in case things go awry, commit everything to make a reflog checkpoint
git commit -a -m'gstash_keep safety net'
git reset HEAD^
# now commit the requested files, stash the rest, and bring the commit back
git commit -m'gstash_keep' "$@" && git stash && git reset HEAD^
}
# stash only the files given, uses gstash_keep
function gstash_some
{
keep=""
all_files=$(git status -uno -s | awk '{print $2}')
for file in $all_files; do
keep_file=1
for stash_file in $@; do
#echo "compare '$file' with '$stash_file'"
if [ "$file" == "$stash_file" ]; then
#echo " don't keep file"
keep_file=0;
break;
fi;
done;
[ "$keep_file" -eq "1" ] && keep="$keep $file" # && echo " keeping"
done;
if [ "$keep" != "" ]; then
gstash_keep $keep;
fi;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment