Git poop - stash, checkout, pull, and apply
Here is my immaturely named alias that I get a lot of use out of. It's purpose is simple: stash working changes, check out a branch, pull the latest of that branch (from origin), and then re-apply the stash. I seem to use it daily since I always have slight changes/notes on my working tree.
Run this command to setup a global git alias:
git config --global alias.poop '!sh -c '"'git diff-index HEAD --quiet --exit-code; NEEDS_STASH=\$?; if [ \"\$NEEDS_STASH\" -eq \"1\" ] ; then git stash; else echo \"Nothing to stash.\"; fi; git checkout \"\$0\" && git pull origin \"\$0\"; if [ \"\$NEEDS_STASH\" -eq \"1\" ] ; then git stash pop; fi;'"
Usage:
git poop <branch-name>
eg:
# equates to: git stash; git checkout develop; git pull origin develop; git stash pop
git poop develop
It is smart enough to only re-apply the stashed changes if changes were stashed. I feel like it's not perfect, but it's become very useful to me so I wanted to share. Improvements / tweaks welcome.
Written by Matt Feury
Related protips
6 Responses
"-bash: !sh: event not found" on my mac
ahh true! very sorry about that as I use fish (http://fishshell.com/). i just posted an edited version that should work on bash / fish.
Fish shell is great.
You don't need that condition:
if [ "$NEEDS_STASH" -eq "1" ] ; then
git stash;
else
echo "Nothing to stash.";
fi;
If there's nothing to stash, git-stash will tell you.
Wouldn't you be better off committing your 'slight changes/notes' into a separate branch and rebasing that every time?
git poop
looks like it will almost do what I am want. I am looking for git stash && git pull --rebase && git stash pop
but I think I can cobble that together from your example. Thank you for sharing.