-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-force-push
More file actions
executable file
·68 lines (56 loc) · 1.65 KB
/
Copy pathgit-force-push
File metadata and controls
executable file
·68 lines (56 loc) · 1.65 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
if [[ $# -ge 2 ]]; then
remote="$1"
branch="$2"
elif [[ $# -eq 1 ]]; then
if git remote | grep -q "^${1}$"; then
remote="$1"
branch=$(git rev-parse --abbrev-ref HEAD)
else
branch="$1"
fi
else
branch=$(git rev-parse --abbrev-ref HEAD)
fi
if [[ -z $branch ]]; then
echo "No branch specified"
exit 1
fi
# Use remote branch is tracking, if any
[[ -z $remote ]] && remote=$(git config --get "branch.${branch}.remote")
# Otherwise, use origin if remote exists
[[ -z $remote ]] && remote=$(git remote | grep -q "^origin$" && echo "origin")
# Fallback to first remote
[[ -z $remote ]] && remote=$(git remote | head -n 1)
if [[ -z $remote ]]; then
echo "Unable to determine remote"
exit 1
fi
# Replace arguments
set -- "$remote" "$branch" "${@:3}"
# Try pushing
git push --force-with-lease "$@" || {
remote_branch=$(git rev-parse --abbrev-ref '@{u}')
remote=$(echo "$remote_branch" | cut -d/ -f1)
current_branch=$(git rev-parse --abbrev-ref HEAD)
git fetch "${remote}" "${current_branch}" 2>/dev/null
remote_commit=$(git rev-parse --short '@{u}')
gray='\e[090m'
lightgreen='\e[92m'
orange='\e[33m'
red='\e[31m'
reset='\e[0m'
echo
echo -e "${gray}-----------------------------------------${reset}"
git context-graph -25
echo -e "${gray}-----------------------------------------${reset}"
echo
echo -e "To acknowledge current ${red}${remote_branch}${reset} revision, use:"
echo
echo -e " git push --force-with-lease=${lightgreen}${current_branch}${reset}:${orange}${remote_commit}${reset}"
echo
echo "or just:"
echo
echo " git push --force"
echo
}