├── README.md └── gitup /README.md: -------------------------------------------------------------------------------- 1 | # gitup 2 | Git tool to update all local branches to their remote tracking branches 3 | 4 | What it does 5 | --------- 6 | 1. Fetch 7 | 2. Updates each of your branches to their remote tracking branch if they can be fast-forwarded 8 | 3. Only updates your current branch if you have a clean working directory 9 | 4. Does not touch any branches that are ahead or have diverged from their remote branch 10 | 5. Does all this without touching your working copy! (i.e. NO checkouts, never switches your branch) 11 | 12 | Usage 13 | -------- 14 | This is a bash function. When you are in a directory which is a git repo (has a .git dir) then just run gitup.sh 15 | Just add this to your bash profile so you can run "gitup" in any directory. 16 | -------------------------------------------------------------------------------- /gitup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | gitup(){ 4 | RED='\033[33;31m' 5 | YELLO='\033[33;33m' 6 | GREEN='\033[33;32m' 7 | NC='\033[0m' # No Color 8 | 9 | if ! [ -d .git ]; then 10 | echo "${RED}This is not a git repo" 11 | return 1 12 | fi 13 | 14 | HEAD=$(git rev-parse HEAD) 15 | CHANGED=$(git status --porcelain | wc -l) 16 | CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) 17 | 18 | echo "Fetching..." 19 | git fetch --all --prune &>/dev/null 20 | if [ $? -ne 0 ]; then 21 | echo "Fetch Failed!" 22 | return 1 23 | fi 24 | 25 | for branch in `git for-each-ref --format='%(refname:short)' refs/heads`; do 26 | 27 | LOCAL=$(git rev-parse --quiet --verify $branch) 28 | if [ "$branch" = "$CUR_BRANCH" ] && [ $CHANGED -gt 0 ]; then 29 | echo -e "${YELLO}WORKING${NC}\t\t$branch" 30 | elif git rev-parse --verify --quiet $branch@{u}&>/dev/null; then 31 | REMOTE=$(git rev-parse --quiet --verify $branch@{u}) 32 | BASE=$(git merge-base $branch $branch@{u}) 33 | 34 | if [ "$LOCAL" = "$REMOTE" ]; then 35 | echo -e "${GREEN}OK${NC}\t\t$branch" 36 | elif [ "$LOCAL" = "$BASE" ]; then 37 | if [ "$branch" = "$CUR_BRANCH" ]; then 38 | git merge $REMOTE&>/dev/null 39 | else 40 | git branch -f $branch $REMOTE 41 | fi 42 | echo -e "${GREEN}UPDATED${NC}\t\t$branch" 43 | elif [ "$REMOTE" = "$BASE" ]; then 44 | echo -e "${RED}AHEAD${NC}\t\t$branch" 45 | else 46 | echo -e "${RED}DIVERGED${NC}\t\t$branch" 47 | fi 48 | else 49 | echo -e "${RED}NO REMOTE${NC}\t$branch" 50 | fi 51 | done 52 | } 53 | --------------------------------------------------------------------------------