Switch branch and pull preserving local changes

script

Originally posted as GitHub Gist on 26 Jul 2018 , updated at 16 Nov 2020 (details)


Source: https://gist.github.com/paulera/da573453b73050bdbd11aa1693d7c9f7

Description: Switch branch and pull changes (fetch + checkout + pull) preserving local changes (using stash)


gimme.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash

git1() {
    STASH="gimme-cmd-stash-"$(date +"%s")i
    git stash save $STASH
    STASH_REF="$(git stash list | grep "$STASH" | awk -F: '{ print $1 }')"
    git fetch && git checkout $1 && git pull
    if [ "$STASH_REF" != "" ]; then
        git stash pop "$STASH_REF"
    else
        echo "No local changes, nothing was stashed"
    fi
}

git2() {
    if [[ "$OSTYPE" =~ "darwin" ]]; then
        # MAC
        ESC="\x1B"
    else
        ESC="\e"
    fi

    for i in *; do
        if [ -d "$i/.git" ]; then
            echo -e "$ESC[01;36m-----> "$i"$ESC[0m"
            echo
            cd $i
            git1 $1
            cd ..
            echo
        fi
    done
}

if [ -z $1 ] || [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
    echo "This script changes the branch of git projects preserving changes (by using stash)"
    echo "Usage:"
    echo "gimme <branch> - switch the current folder to <branch>"
    echo "gimme all <branch> - switch all subfolders to <branch>"
    echo
    exit 1
elif [ "$1" == "all" ]; then
    git2 "$2"
elif [ ! -z $1 ]; then
    git1 "$1"
fi