forked from nvie/git-toolbelt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-update-all
executable file
·52 lines (45 loc) · 1.53 KB
/
git-update-all
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
49
50
51
52
#!/bin/sh
set -e
usage () {
echo "usage: git update-all [-h] [remote]" >&2
echo >&2
echo "Updates (pulls) upstream changes on all local branches." >&2
echo "remote defaults to origin." >&2
echo >&2
echo "Options:" >&2
echo "-h Show this help" >&2
}
while getopts h flag; do
case "$flag" in
h) usage; exit 2;;
esac
done
shift $(($OPTIND - 1))
if git is-dirty; then
echo "Cannot do this while your working copy has local changes. Stash or commit them first." >&2
exit 2
fi
remote=${1:-origin}
curr=$(git current-branch)
git fetch --quiet
for branch in $(git local-branches); do
if git remote-branch-exists "$remote" "$branch"; then
if [ "$branch" != "$(git current-branch)" ]; then
git checkout --quiet "$branch" >/dev/null 2>/dev/null
fi
orig_sha=$(git sha -s "$branch")
new_sha=$(git sha -s "$remote"/"$branch")
if [ "$orig_sha" != "$new_sha" ] && git contains "$remote"/"$branch" "$branch"; then
if git merge --quiet --ff-only "$remote/$branch"; then
echo "updated '$branch' to latest '$remote/$branch' ($orig_sha...$new_sha)"
else
echo "warning: could not fast-forward '$branch' to '$remote/$branch': branches have diverged." >&2
fi
else
echo "skipping: branch '$branch' is already up-to-date or newer than '$remote/$branch'" >&2
fi
else
echo "skipping: branch '$branch' is not on '$remote'" >&2
fi
done
git checkout --quiet "$curr"