Skip to content

Instantly share code, notes, and snippets.

@ajaegers
Last active August 28, 2018 13:58
Show Gist options
  • Save ajaegers/b4b2e16ee3a28ec4c3ba644a3b885035 to your computer and use it in GitHub Desktop.
Save ajaegers/b4b2e16ee3a28ec4c3ba644a3b885035 to your computer and use it in GitHub Desktop.
Simple way to get all your local Git branches up to date
#!/bin/bash
# Simple way to get all your local branches up to date
# Ps: Add this to your .bash_profile or other file as a global helper
function _gitup(){
branches=`git branch`;
current_branch=null;
git_status=`git status -s`;
has_stashed=false;
echo "1. Getting list of local branches...";
echo "$branches";
echo "";
if [ -n "$git_status" ]; then
echo -n "2. Stashing current changes... ";
local curbranch=`git branch | grep \* | cut -d ' ' -f2`;
local curdate="gitup_${curbranch}_$(date +'%d-%m-%Y-%H-%M-%S')";
git_stash_result=`git stash save "$curdate"`;
has_stashed=true;
echo -e "\\033[0;32m" "OK" "\\033[0;39m" "'$git_stash_result'";
else
echo "2. No changes to stash :)";
fi
echo "3. Checkout and pull latest changes of all local branches...";
while read line; do
case $line in
\*\ *)
branch=${line#\*\ };
current_branch="$branch";
;; # match the current branch
*)
branch=$line
;; # match all the other branches
esac
echo -n " UPDATING '$branch' ... ";
git checkout -q $branch && git pull -q && echo -e "\\033[0;32m" "OK" "\\033[0;39m";
done <<< "$branches"
echo -n "4. Checkout lastest working branch '$current_branch'... ";
git checkout -q $current_branch && echo -e "\\033[0;32m" "OK" "\\033[0;39m";
if [ "$has_stashed" == true ] && [ "$git_stash_result" != "No local changes to save" ]; then
echo -n "5. Restoring latest stashed changes... ";
git stash pop -q && echo -e "\\033[0;32m" "OK" "\\033[0;39m";
else
echo "5. No changes to restore :)";
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment