Created
July 18, 2018 01:42
-
-
Save J-Swift/76a976249127300b2f844543a045ec81 to your computer and use it in GitHub Desktop.
Custom git command to stash only the staged changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# https://stackoverflow.com/a/39644782/1273175 | |
((!$#)) && echo 'Must provide a stash name!' && exit 1 | |
if ! $( git status --porcelain | grep -v '??' | grep -E '^[^ ]' --silent ); then | |
echo 'No staged changes detected!' | |
exit 1 | |
fi | |
#Stash everything temporarily. Keep staged files, discard everything else after stashing. | |
git stash --keep-index | |
#Stash everything that remains (only the staged files should remain) This is the stash we want to keep, so give it a name. | |
git stash save "$1" | |
#Apply the original stash to get us back to where we started. | |
git stash apply stash@{1} | |
#Create a temporary patch to reverse the originally staged changes and apply it | |
git stash show -p | git apply -R | |
#Delete the temporary stash | |
git stash drop stash@{1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment