-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-update
executable file
·48 lines (40 loc) · 971 Bytes
/
git-update
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
47
48
#!/bin/bash
set -e # bail on errors
function current_branch() {
git rev-parse --abbrev-ref HEAD | sed -e 's/^heads\///'
}
ORIG_BRANCH=$(current_branch)
DEFAULT_REMOTE=$(git config --get branch.${ORIG_BRANCH}.remote)
REMOTE="${1:-$DEFAULT_REMOTE}"
if [[ "$ORIG_BRANCH" = "HEAD" ]]; then
echo "Cannot update in a detached HEAD state"
exit 1
fi
if [[ -z "$2" ]]; then
BRANCHES="$ORIG_BRANCH"
else
if [[ "$1" = "$REMOTE" ]]; then
shift
fi
BRANCHES="$@"
fi
STASH_OUTPUT=$(git stash)
if [[ "$STASH_OUTPUT" = "No local changes to save" ]]; then
POP_STASH=0
else
POP_STASH=1
fi
git fetch --prune --tags "$REMOTE"
for BRANCH in $BRANCHES; do
echo "* Updating $BRANCH from $REMOTE/$BRANCH"
if [[ "$BRANCH" != "$(current_branch)" ]]; then
git checkout "$BRANCH"
fi
git rebase "$REMOTE/$BRANCH"
done
if [[ "$ORIG_BRANCH" != "$(current_branch)" ]]; then
git checkout "$ORIG_BRANCH"
fi
if [[ $POP_STASH -eq 1 ]]; then
git stash pop
fi