-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.sh
185 lines (130 loc) · 3.42 KB
/
git.sh
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/zsh
PROJECT_PREFIX="<todo>"
MAIN_BRANCH=master
## -- Commits --
function most_recent_commit_message {
git --no-pager log -1 --pretty=%s
}
function show_last_10_commits {
git --no-pager log --oneline --decorate -10
}
function commit {
git add --all
git commit --message="$(current_branch) $1"
}
function wip {
local wip_message="wip: undo with git reset --soft HEAD~"
git add --all
git commit --message=$wip_message
}
function melt {
git add --all
git commit --amend --all --no-edit
}
function undo {
local commit_message=$(most_recent_commit_message)
echo "Undoing commit: $commit_message"
git reset --soft HEAD~
}
function fixup {
local commit=$1
git add --all
git commit --fixup=$commit
git -c sequence.editor=: rebase --interactive "$commit"^ --autosquash
}
compdef _git fixup_all=git-commit
## -- Branches --
function current_branch {
git rev-parse --abbrev-ref HEAD
}
function list_most_recent_branches {
git for-each-ref \
--sort=committerdate refs/heads/ \
--format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'
}
function switch_branch {
local target_branch=$1
if [[ $target_branch == "$PROJECT_PREFIX"* ]]
then
git switch $target_branch
else
git switch $PROJECT_PREFIX-$target_branch
fi
}
function create_branch {
local branch_name=$1
local start_point=$2
if [[ -z "$start_point" ]]
then
git switch --create $PROJECT_PREFIX-$branch_name
else
git switch --create $PROJECT_PREFIX-$branch_name $start_point
fi
}
function backup_branch {
local branch_to_backup=$(current_branch)
local suffix="-backup"
git switch --create $branch_to_backup$suffix $branch_to_backup
git switch -
}
## -- Push --
function push {
git push origin $(current_branch)
}
function push_no_ci {
git push origin $(current_branch) -o ci.skip
}
function putsch {
git push --force-with-lease origin $(current_branch)
}
function putsch_no_ci {
git push --force-with-lease origin $(current_branch) -o ci.skip
}
## -- Rebase & Reset --
function rebase_on_master {
git fetch --all --prune > /dev/null
git rebase origin/$MAIN_BRANCH
}
function rebase_on_remote {
local remote_branch=$(current_branch)
git fetch --all --prune > /dev/null
git rebase origin/$remote_branch
}
function reset_to_master {
git fetch --all --prune > /dev/null
git reset --hard origin/MAIN_BRANCH
}
function reset_to_remote {
local remote_branch=$(current_branch)
git fetch --all --prune > /dev/null
git reset --hard origin/$remote_branch
}
function nuke {
go_to_root
git reset --hard
git clean --force -d
}
## -- Misc --
function go_to_root {
cd $(git rev-parse --show-toplevel)
}
function master {
git switch $MAIN_BRANCH
}
## -- Aliases --
alias root=go_to_root
### One-letter aliases, should never be destructive
alias m=master
alias b=list_most_recent_branches
### Multiple-letters aliases
alias co=commit
alias st='git status'
alias br=list_most_recent_branches
alias bb=backup_branch
alias cb=create_branch
alias sw=switch_branch
alias lo=show_last_10_commits
alias rom=rebase_on_master
alias ror=rebase_on_remote
alias rtm=reset_to_master
alias rtr=reset_to_remote