-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgit-track-remote
More file actions
executable file
·54 lines (45 loc) · 1.21 KB
/
git-track-remote
File metadata and controls
executable file
·54 lines (45 loc) · 1.21 KB
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
53
54
#!/bin/bash
# Put this file in your $PATH
#
# Usage:
#
# git track-remote origin/branch
# git track-remote branch origin/branch
#
# Sets up an existing branch to track a remote branch. Runs the
# following git-config commands:
#
# git config branch.$local_branch.remote $remote_repo
# git config branch.$local_branch.remote refs/heads/$remote_branch
echo_err() {
echo "$(basename $0): $@" >&2
}
if [[ -z "$1" ]]; then
echo_err "Didn't specify remote."
exit 1
fi
parse_git_branch() {
git-branch --no-color 2>/dev/null | grep '^\*' | sed 's/^\* //'
}
if [[ -z "$2" ]]; then
Remote="$1"
Local="$(parse_git_branch)"
else
Remote="$2"
Local="$1"
fi
RemoteRepo=$(echo "$Remote" | sed 's/\/.*$//')
RemoteBranch=$(echo "$Remote" | sed 's/^.*\///')
if [[ ! $(echo "$Remote" | grep '/') || -z "$RemoteRepo" || -z "$RemoteBranch" ]]; then
echo_err "Looks like remote was not specified correctly."
exit 1
fi
if [[ -z "$Local" ]]; then
echo_err "Not in a git repository."
exit 2
elif [[ "$Local" = "(no branch)" ]]; then
echo_err "Not on any branch, specify branch."
exit 2
fi
git config branch."$Local".remote "$RemoteRepo"
git config branch."$Local".merge "refs/heads/$RemoteBranch"