-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-sync
executable file
·52 lines (40 loc) · 964 Bytes
/
git-sync
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/bash
set -e
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
echo "Not a git repository."
exit 1
fi
if [ "$1" = "-h" ]; then
cat <<EOF
usage: git-sync [-n] [BRANCH [UPSTREAM [TARGET]]]
switch to BRANCH, fetch from UPSTREAM, merge it to UPSTREAM/BRANCH,
push it to TARGET, switch back to the current branch (unless -n is given).
BRANCH defaults to "master", UPSTREAM to "upstream", and TARGET to "origin".
EOF
exit 0
fi
n=""
if [ "$1" = "-n" ]; then
n=1
shift
fi
b="${1:-master}"
r="${2:-upstream}"
p="${3:-origin}"
cb="$(git rev-parse --abbrev-ref HEAD)"
if [ "$cb" != "$b" ]; then
git checkout "$b"
fi
git fetch "$r" && git merge "$r"/"$b" && git push "$p"
if [ -z "$n" -a "$cb" != "$b" ]; then
git checkout "$cb"
read -e -n 1 -p "Rebase or Merge? [r/m/N] "
case "$REPLY" in
r|R)
git rebase "$b"
;;
m|M)
git merge "$b"
;;
esac
fi