-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-sync
executable file
·30 lines (29 loc) · 1.13 KB
/
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
#!/usr/bin/env bash
#
# Sync your current branch with it's remote counterpart.
#
# $1 - Optional remote name, defaults to origin. If you pass "all", all remotes will be synced to
# the current branch (or the branch passed as argument $2)
# $2 - Optional branch name, defaults to current branch
#
# Examples
#
# git sync
# git sync origin
# git sync origin master
#
function sync_branch() {
local current_branch_name remote branch_name all_remotes
all_remotes=("$(git remote)")
current_branch_name=$(git rev-parse --abbrev-ref HEAD)
remotes=("${1:-origin}")
if [[ $1 == "all" ]]; then
remotes=("${all_remotes[@]}")
fi
branch_name=${2:-${current_branch_name}}
for remote in "${remotes[@]}"; do
echo "=== Syncing ${branch_name} -> ${remote}/${branch_name}"
git pull --rebase "${remote}" "${branch_name}" && (git push "${remote}" "${branch_name}" && echo "=== Synced ${branch_name} -> ${remote}/${branch_name} sucessfully" || echo "=== Error when pushing changes to ${remote}/${branch_name}") || echo "=== Error when fetching changes from ${remote}/${branch_name}"
done
}
sync_branch "$@"