-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.bash
325 lines (225 loc) · 10.1 KB
/
git.bash
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#### Constants ####
FORMAT="%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))"
#### General ####
# Git log graph
alias lg="git log --graph --all --decorate"
# Regular git log
alias log="git log --name-status"
# Shortcut + verbose
alias fe="git fetch -v"
# Shortcut + enable Perl regex grep
alias gg="git grep --perl-regexp --line-number "
alias gg-w="git grep --perl-regexp --line-number --word-regexp "
gg_uniq () { git grep -P $1 | awk -F: '{print $1}' | uniq; }
# Shortcut. gl = Git List
alias gl="git ls-files"
# Shortcut for stash
alias gpush="git stash"
# Shortcut for stash
alias gpop="git stash apply"
# Show the content of the top change in stash. gss = Git Stash Show
alias gss="git stash show -p"
# Shortcut for git stash drop = Git Stash ReMove
alias gsrm="git stash drop"
# Git stash list files changed = Git Stash List
alias gsl="git stash show"
# List the dates the stashes were created = Git Stash Date
alias gsd="git stash list --date=local"
# Shortcut for rebase continue
alias cont_re="git rebase --continue"
# Shortcut for rebase abort
alias abort_re="git rebase --abort"
# Shortcut for rebase skip
alias skip_re="git rebase --skip"
# Shortcut for cherry-pick continue
alias cont_cp="git cherry-pick --continue"
# Shortcut for cherry-pick abort
alias abort_cp="git cherry-pick --abort"
# Shortcut for cherry-pick skip
alias skip_cp="git cherry-pick --skip"
# Shortcut for status
alias st="git status"
# Shortcut for cherry pick
alias gcp="git cherry-pick -x"
# Shortcut for review
alias review="git review"
# Updates submodules Note: Equivelant to "git submodule sync && git submodule update"?
alias updatesub="git submodule update --recursive --init"
# Rename a file, normally case differences in git name to local name
alias gmv="git mv -f "
# Delete all non-commited files (- etags file) TODO: follow up with a `cp_secrets`
gnuke ()
{
echo "Comfirm deletion of the following files? (Remember all non-added files will be removed)"
git clean -fdxn &&
while true; do
read -s -n 1 C
case $C in
[y]* ) git clean -fdx -e ".tags" -e \".tags_sorted_by_file\"; break;;
[n]* ) break;;
* ) echo "Please answer y or n.";;
esac
done
}
# Push to origin HEAD with force
# TODO: Make this rebor, check to see if you still want to push afterwards
submit () { git push origin +HEAD; }
#### Commitments ####
# Adds all untracked files
add_untracked() { echo -e "a\n*\nq\n" | git add -i; }
# Commit modified and deleted file changes. Brings up set text editor for message
co() { git add -u && git commit; }
# Commit modified and deleted file changes with the given commit message
co_m() { git add -u && git commit -m $1; }
# Commit changes and ammend to last commit
alias co_am="git add -u && git commit --amend --no-edit"
# Commit changes and ammend to last commit + edit the commit message
alias co_am_ed="git add -u && git commit --amend"
# Undo the last commit, making them uncommited changes
alias reset="git reset HEAD~"
# Discard all non-commited changes
alias discard="git checkout -- ."
# Reset back to the state before the last executed git command. Use as an "undo" command
alias gundo="git reset HEAD@{1}"
# Undo last commit by reseting and discard the changes
undo_last() { reset && discard; }
# Ignore local content changes to tracked file
alias ignore_changes="git update-index --assume-unchanged"
# Undo `ignore_changes`
alias track_changes="git update-index --no-assume-unchanged"
# View files marked with --assume-unchanged
alias ignored_files="git ls-files -v | grep '^[[:lower:]]'"
# See files checked into git
alias cf="git ls-files -v"
#### Conflicts ####
# list all conflicted files
conf_ls() { git diff --name-only --diff-filter=U; }
# open conflicted files in sublime
conf_subl() { conf_ls && conf_ls | xargs "$SUBL_FUNC"; }
# add the conflicted files after you fix them
conf_add() { git diff --name-only --diff-filter=U | xargs git add; }
# Resolve conflict by discarding local changes
conf_ours() { git diff --name-only --diff-filter=U | xargs git checkout --ours; }
#### Branches ####
# View all local branches
alias br="git branch"
# View all remote and local branches
alias br_remote="git branch -a"
# Checkout to another branch
alias switch="git checkout "
# Rename a branch
alias mvbr="git branch -m "
# Forces a deletion on the branch
rmbr() { git branch -D $1; }
# Delete a remote branch
alias rmbr_remote="git push origin --delete "
# Create new branch based off origin/master and checkout into the given name
cdbranch () { git checkout -b $1 remotes/origin/master; }
# Print the name of the current branch
currbr() { git rev-parse --abbrev-ref HEAD; }
# Fetches origin and rebases for this branch
rebbr() { git fetch origin && git rebase origin/$(currbr) --stat; }
# Fetches origin and rebases ontop on master
rebor() { git fetch origin && git rebase origin/master --stat; }
# This will now checkout to the middle commit between now and the last known good commit. If the build succeeds, use `git bisect good`. If it fails, use ``git bisect bad`
bisect() { git bisect start && git bisect bad && git bisect good $1; }
# Note: Reset, rebase and merge all save your original HEAD pointer to ORIG_HEAD. Thus these commands have been run since the rebase you're trying to undo then you'll have to use the reflog.
undo_rebase() { git reset --hard ORIG_HEAD; }
# Fetches origin and rebases ontop on master and switches to origin/master
rebmaster() { git fetch origin && git rebase origin/master --stat && git checkout origin/master; }
# Resets the current branch to the latest branch whille ignoring local changes
pull_ignore_local() { git fetch origin && git reset --hard origin/$(currbr); }
# View last 10 local branches sorted by last commit in descending order
alias brst="git for-each-ref --sort=-committerdate --count=10 refs/heads/ --format='$FORMAT'"
# View all local branches sorted by last commit in ascending order
alias brsort="git for-each-ref --sort=committerdate refs/heads/ --format='$FORMAT'"
# View all remote branches sorted by last commit in ascending order
alias remotebrsort="git for-each-ref --sort=committerdate refs/remotes/ --format='$FORMAT' | tail -10"
# Check if the given branch has not been altered in over 4 weeks
_br_la() { git log -1 --after='4 weeks ago' -s $1; }
# TODO: add comment stating if branch has been merged with master
# TODO: echo "Deleted <branch>" correctly
rm_od ()
{
for k in $(git branch | sed /\*/d); do
# NOTE: change '-z' to '-n' to filter by BEFORE date
if [ -z "$(_br_la $k)" ]; then
# git branch -D $k
echo "Comfirm deletion of branch $(git log -1 --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'$k)?"
while true; do
read -s -n 1 C
case $C in
[y]* ) $(rmbr $k) && echo "Deleted $k"; break;;
[n]* ) break;;
[q]* ) break 2;;
* ) echo "Please answer y or n.";;
esac
done
fi
done
}
alias reorder="git rebase -i HEAD~10"
clear_merged_br () { git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d; }
# View what commits have not been checked into master
cl_br() { git log master.."$1"; }
# Tag the branch then delete it, effectively keeping the branch around without it cluttering your list
archive_br () { git tag archive/$1 $1 && git branch -D $1; }
# Restore the archived branch
restore_br () { git checkout -b $1 archive/$1; }
count_commits () { git shortlog -s -n; }
#### Given File Change ####
# Shortcut and improvement for log on a file (beyond file renames)
lgf () { git log --follow $1; }
# view commit log with changes of given file
logf () { git log --follow -p $1; }
# view commit log with changes of given file in sublime
logfsubl () { git log --follow -p $1 > $1.log && "$SUBL_FUNC" $1.log && rm $1.log; }
# view reflog log of given file
alias reflogf="git rev-list --all "
#### All File Changes ####
# Shortcut for `git diff`
alias df="git diff"
# View the changes made in the last commit - df = diff last
alias dl="git diff HEAD^ HEAD"
# view the file changed list in the last commit - cl = see last
alias cl="git diff-tree --no-commit-id --name-status -r HEAD^ HEAD"
# view reflog with time info
alias reflog="git reflog --date=iso"
# view the file changed list in the given commit ID
alias cinfo="git diff-tree --no-commit-id --name-status -r "
# View a list of all deleted files
alias gdeleted="git log --diff-filter=D --summary | grep delete"
# List all existing files being tracked by the current branch
tracked() { git ls-tree -r $(currbr) --name-only; }
# Open all tracked files
otracked() { tracked | xargs "$SUBL_FUNC"; }
# view the file changes in the given commit ID
cdiff () { git diff $1^ $1; }
# view all files given author has touched
gtouch () { git log --no-merges --stat --author="$1" --name-only --pretty=format:"" | sort -u; }
# View all the commits I have merged
my_commits () { git log --author="anpea"; }
# Perform a "git grep" including the history of the files
greph () { git rev-list --all | xargs git grep "$1"; }
get_unstaged_files() { git diff --name-only; }
get_staged_files() { git diff --staged --name-only --diff-filter=ACMRT; }
untrack() { git rm -r --cached $1; }
rm_untracked ()
{
echo "Comfirm deletion of the following files? (Remember all non-added files will be removed)"
git clean -n &&
while true; do
read -s -n 1 C
case $C in
[y]* ) git clean -f; break;;
[n]* ) break;;
* ) echo "Please answer y or n.";;
esac
done
}
# Adds the worktree with the given directory name. Run this in the main repo
add_worktree() { git worktree prune && git worktree add ../$1 master; }
# Optimized `replace` for use in a git repository
greplace () { a=$1; b=$2; git grep --files-with-matches "$a" | xargs sed -i -- s/$a/$b/g; }
# Rename files in git directory
grename () { a=$1; b=$2; for old_file in $(git ls-files "*$a*"); do new_file=$(echo $old_file | sed -En "s/$a/$b/p"); git mv -f $old_file $new_file; done; }