-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_functions
executable file
·87 lines (85 loc) · 2.54 KB
/
git_functions
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
git_current_branch() {
git branch | grep '\*' | tr -d '* '
}
git_diff_inclusive() {
if [[ "${#}" -eq 0 ]]; then
git diff HEAD~1..HEAD
elif [[ "${#}" -ne 1 ]]; then
echo "Usage: gdi [hash] or gdi [hash1..hash2]"
elif [[ "${1}" =~ \.\. ]]; then
git diff "${1/../~1..}"
else
git diff "${1}~1..${1}"
fi
}
git_delete_branch() {
git branch -d "${1}"
git push origin :refs/heads/"${1}"
}
git_delete_tag() {
git tag -d "${1}"
git push origin :refs/tags/"${1}"
}
git_log_since() {
git log --decorate --oneline "${1:+${1}~1..}"
}
git_upstream() {
declare -r local="${1:-$(git_current_branch)}"
declare -r remote="${2:-origin/${local}}"
git branch | grep --quiet " ${local}$" \
&& git branch --set-upstream-to="${remote}" "${local}" \
&& return $?
git branch --track "${local}" "${remote}"
}
git_synchronize() {
# sync tags
echo '=== sync tags ==='
git fetch --tags --all
# sync remote branches
echo '=== get branches ==='
git remote update origin --prune
# set local branch & upstream for each remote branch
echo '=== upstream and update ==='
git branch --remotes | grep --invert-match '\->' | while read -r remote; do
declare local="${remote#origin/}"
git_upstream "${local}" "${remote}"
[[ "${local}" == "$(git_current_branch)" ]] \
&& git pull \
&& continue
git fetch origin "${local}:${local}"
done
# remove local merged branches without a corresponding remote
echo '=== remove merged ==='
git branch --merged | while read -r local; do
git branch --remotes | grep --quiet " origin/${local#* }$" \
&& continue
git branch --delete --force "${local#* }"
done
# update submodules
echo '=== update submodules ==='
git submodule update --init --recursive
# git push
echo '=== push all branches and tags ==='
git push --all
git push --tags
}
alias gfi='gf --init'
alias gdi='git_diff_inclusive'
alias gdb='git_delete_branch'
alias gdt='git_delete_tag'
alias gls='git_log_since'
alias gup='git_upstream'
alias gcb='git_current_branch'
alias gb='git checkout -b'
alias gd='git diff'
alias gc='git commit'
alias gch='git checkout'
alias gl="git log --oneline --decorate --color --graph"
alias gla="gl --all"
alias gll="git log --color --abbrev-commit --pretty=format:'%C(red)%h%Creset%C(yellow)%d%Creset %s %C(green)%cr%C(white dim) %an%Creset'"
alias gpush='git push --all; git push --tags'
alias gsync='git_synchronize'
alias guc='git reset --soft HEAD~1'
alias gs='git status'
alias gaas='git add -A; gs'