raniel

Git pull all branches

Aug 5th, 2021
755
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # define colors
  4. CYAN='\033[0;36m'
  5. GREEN='\033[0;32m'
  6. RED='\033[0;31m'
  7. # no color
  8. NC='\033[0m'
  9.  
  10. # check if this directory is a git repository
  11. if [ ! -d .git ]; then
  12.     printf "${RED}ERROR: this directory is not a git repository${NC}\n";
  13.     exit 1
  14. fi
  15.  
  16. #check if git is installed
  17. if ! command -v git &> /dev/null; then
  18.     printf "${RED}ERROR: git command not found${NC}\n";
  19.     exit 1
  20. fi
  21.  
  22. # get git command path
  23. gitCommand=$(which git)
  24.  
  25. # get current branch
  26. current=$(${gitCommand} branch | grep \* | sed 's/\* //g')
  27.  
  28. # loop over all local branches and pull them
  29. ${gitCommand} branch |sed 's/\* //g' |while read -r branch; do
  30.     printf "\n${CYAN}PULL ${branch}${NC}\n";
  31.     ${gitCommand} checkout "${branch}"
  32.     ${gitCommand} pull
  33. done
  34.  
  35. # return to initial branch
  36. printf "\n${GREEN}RETURN TO ${current}${NC}\n";
  37. ${gitCommand} checkout "${current}"
  38.  
  39. exit 0
  40.  
RAW Paste Data