Skip to content

Instantly share code, notes, and snippets.

@steve-jansen
Created March 8, 2013 16:29
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save steve-jansen/5117721 to your computer and use it in GitHub Desktop.
Save steve-jansen/5117721 to your computer and use it in GitHub Desktop.
A custom script for git to stash any working changes, checkout master, pull origin master, checkout your working branch, rebase master, and unstash your working changes
#!/bin/bash
stash() {
# check if we have uncommited changes to stash
git status --porcelain | grep "^." >/dev/null;
if [ $? -eq 0 ]
then
if git stash save -u "git-update on `date`";
then
stash=1;
fi
fi
}
unstash() {
# check if we have uncommited change to restore from the stash
if [ $stash -eq 1 ]
then
git stash pop;
fi
}
stash=0;
stash;
branch=`git branch | grep "\*" | cut -d " " -f 2-9`;
if [ "$branch" == "master" ]
then
git pull origin master;
else
git checkout master;
git pull origin master;
git checkout "$branch";
git rebase master;
fi
unstash;
@calvinjuarez
Copy link

calvinjuarez commented Jun 7, 2017

Love this!

May I ask, any reason for git status --porcelain | grep "^." >/dev/null; over git diff-index --quiet HEAD --? (I'm not an expert, I'm just looking at https://stackoverflow.com/questions/3878624 and I wanna know what the best way would be.)

Anyway, thanks for sharing!

Update: Looking closer at this, it seems that you're looking for all changes, including in untracked files, whereas diff-index just looks for changes to tracked files. So n/m. Thanks again!

@coreysnyder
Copy link

Thank you for sharing this! I used most of it to achieve something a little different in one of my scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment