-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-config
executable file
·389 lines (341 loc) · 16.9 KB
/
git-config
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/bin/bash
# Execute this script, or run individual lines you want. Don't symlink this to ~/.gitconfig.
set -x
# NeoBundle makes vim bork out when run as vi (which is git default)
#git config --global core.editor $(command -v vim)
# Find the branching point http://stackoverflow.com/a/4991675
git config --global alias.oldest-ancestor "!bash -c '"$'git log -1 "$(diff --old-line-format= --new-line-format= <(git rev-list --first-parent "${1:-master}") <(git rev-list --first-parent "${2:-HEAD}") | head -1)"'"' -"
# Sort branch by date
# Usage: git bd [-a] [-<line_limit>] [--contains <ref>]
# -a include remote branches and tags
# -<line_limit> number of lines to tail
# --contains <ref> Only show branches containing ref
# Example: git bd
# Example: git bd -3
# Example: git bd -a
# Example: git bd --contains release/1.0
# Example: git bd -a -20
# Example: git bd -a20
# Example: git bd -a --contains release/1.0
git config --global alias.bd '!f() {
refs="refs/heads/";
tail_n="+0";
contains= ;
while [ "$#" -gt 0 ]; do
case "$1" in
-a) refs="--"; shift;;
-a*) refs="--"; one="${1/-a/-}"; shift; set -- "$one" "$@";;
--contains) contains="$2"; shift 2;;
--contains=*) contains="${1#*=}"; shift;;
-*[!0123456789]*) shift;;
-*) tail_n="${1#-}"; shift;;
*) shift;;
esac;
done;
format="%(refname) %00%(committerdate:format:%s)%(taggerdate:format:%s) %(color:red)%(committerdate:relative)%(taggerdate:relative)%(color:reset)%09%00%(color:yellow)%(refname:short)%(color:reset) %00%(subject)%00 %(color:reset)%(color:dim cyan)<%(color:reset)%(color:cyan)%(authorname)%(taggername)%(color:reset)%(color:dim cyan)>%(color:reset)";
{
if [ "${#contains}" -eq 0 ]; then
git -c color.ui=always for-each-ref --color --format="$format" "$refs";
else
git -c color.ui=always for-each-ref --color --format="$format" --contains="$contains" "$refs";
fi |
perl -ne "print unless /^refs\/stash /";
[ "${#contains}" -eq 0 ] && [ "$refs" = "--" ] && git show-ref -q --verify refs/stash &&
git log --color --walk-reflogs --format="%gd %x00%ct %C(red)%cr%C(reset)%x09%x00%C(yellow)%gd%C(reset) %x00%s%x00 %C(reset)%C(dim cyan)<%C(reset)%C(cyan)%an%C(reset)%C(dim cyan)>%C(reset)" refs/stash;
} |
perl -pe "s/^refs\/tags\/[^\x00]*\x00([^\x00]*)\x00([^\x00]*)/\$1(tag) \$2/ || s/^[^\x00]*\x00([^\x00]*)\x00/\$1/; s/\x00([^\x00]{0,50})([^\x00]*)\x00/\$1\x1b[1;30m\$2\x1b[0m/" |
sort -n -k1,1 |
cut -d" " -f2- |
tail -n "$tail_n";
}; f'
# Short log
# Usage: git l
# Takes the same arguments as `git log`
git config --global alias.l '!f() { : git log ;
cd "${GIT_PREFIX:-.}"
commit_count="$(git rev-list --count HEAD@{upstream}..HEAD 2>/dev/null || echo 2)";
commit_count=$(( commit_count + 3 ))
[ "$commit_count" -lt 5 ] && commit_count=5;
[ "$commit_count" -gt 20 ] && commit_count=20;
git --no-pager log \
--format="%C(auto)%h %C(reset)%C(dim red)[%C(reset)%C(red)%cr%C(reset)%C(dim red)]%C(reset)%C(auto) %x02%s%x03 %C(reset)%C(dim cyan)<%C(reset)%C(cyan)%an%C(reset)%C(dim cyan)>%C(reset)%C(auto)%d%C(reset)" \
--color --graph "-$commit_count" "$@" |
if command -v perl >/dev/null; then
perl -pe "
s/ seconds? ago/s/ ||
s/ minutes? ago/m/ ||
s/ hours? ago/h/ ||
s/ days? ago/d/ ||
s/ weeks? ago/w/ ||
s/(\d+) years?, (\d+) months? ago/\$1y\$2m/ ||
s/ months? ago/mo/ ||
s/ years? ago/y/;
s/([^\x1b]\[)(.*?)]/sprintf(\"%s%21s]\",\$1,\$2)/e;
s/\x02([^\x03]{0,50})([^\x03]*)\x03/length \$2?\$1.\"\x1b[1;30m\".\$2:\$1/e
";
else
awk "{
replaced=( \
sub(/ seconds? ago/,\"s\") ||
sub(/ minutes? ago/,\"m\") ||
sub(/ hours? ago/,\"h\") ||
sub(/ days? ago/,\"d\") ||
sub(/ weeks? ago/,\"w\") \
);
if (!replaced && \$0 ~ /[0-9]+ years?, [0-9]+ months? ago/) {
sub(/ years?, /,\"y\");
sub(/ months? ago/,\"m\");
} else if (!replaced) {
sub(/ months? ago/,\"mo\") ||
sub(/ years? ago/,\"y\");
}
while (\$0 !~ /[^\\x1b]\\[[^]]{21}/) {
sub(/[^\\x1b]\\[/,\"& \");
}
if (\$0 ~ /\\x02[^\\x03]{51}.*\\x03/) {
sub(/\\x02.{50}/,\"&\\x1b[1;30m\");
}
sub(/\\x02/,\"\");
sub(/\\x03/,\"\");
}1";
fi |
less -RFX;
}; f'
# Log oneline with date
git config --global alias.lo "log --format='%C(auto)%h %cs %s %an %d%C(reset)'"
# Full log graph
git config --global alias.lg "log --all --graph --format='%C(auto)%h %C(dim red)[%C(reset)%C(red)%cr%C(dim red)]%C(reset)%C(auto) %s %C(dim cyan)<%C(reset)%C(cyan)%an%C(dim cyan)>%C(reset)%C(auto)%d'%C(reset)"
# Short status
git config --global alias.s "status --short --branch"
# More readable diff
git config --global alias.d "diff --patience"
# Word diff
git config --global alias.dw "diff --word-diff=color"
# Diff staged
git config --global alias.ds "diff --patience --staged"
# Word diff staged
git config --global alias.dsw "diff --word-diff=color --staged"
# Show branches
git config --global alias.b "branch"
# Update both branches and tags for either all remotes or specified remotes
# Examples:
# git f
# git f origin upstream
git config --global alias.f '!f() { if [ "$#" -eq 0 ]; then git fetch --all --prune 2>&1; else git fetch --multiple --prune "$@" 2>&1; fi | sed "s/^/branches: /"; if [ "$#" -eq 0 ]; then git fetch --all --tags 2>&1; else git fetch --multiple --tags "$@" 2>&1; fi | sed "s/^/tags: /"; }; f'
# Update both branches and tags, then pull.
git config --global alias.fp '!f() { git fetch --all --prune 2>&1 | sed "s/^/branches: /"; git fetch --all --prune --tags 2>&1 | sed "s/^/tags: /"; [ "$1" = "--rebase" ] && action=rebase || action=merge; git "$action" FETCH_HEAD; }; f'
# Fetch github-style pull requests
# git fetch origin +refs/pull/*/head:refs/remotes/origin/pr/*
git config --global alias.fetch-pr '!f() { git remote get-url "$1" >/dev/null 2>&1 || { printf >&2 "Usage: git fetch-pr <remote> [<pr-number>]\n"; exit 1; }; pr="$2"; [ -z "$pr" ] && pr="*"; git fetch "$1" "+refs/pull/$pr/head:refs/remotes/$1/pr/$pr"; }; f'
# Fetch gitlab-style merge requests
git config --global alias.fetch-mr '!f() { git remote get-url "$1" >/dev/null 2>&1 || { printf >&2 "Usage: git fetch-mr <remote> [<mr-number>]\n"; exit 1; }; mr="$2"; [ -z "$mr" ] && mr="*"; git fetch "$1" "+refs/merge-requests/$mr/head:refs/remotes/$1/mr/$mr"; }; f'
# Pull
git config --global alias.p "pull"
# Merge
git config --global alias.m "merge"
# Rebase
git config --global alias.r "rebase"
# Grep with blame
git config --global alias.grep-blame "$(cat <<'EOF' | sed 's/^ *//' | tr '\n' ' '
! : git grep ; perl -e '
my $truncate = 500;
my ($git_maj, $git_min) = `git --version` =~ /version (\d+)\.(\d+)/;
my $git_grep_supports_column = $git_maj == 2 && $git_min >= 19 || $git_maj > 2;
my $full_name = (`git config --get --bool grep.fullName` eq "true\n" && $? == 0);
my (@args, @invalid_args);
my $last_arg_was_e = 0;
my $index = -1;
foreach my $arg (@ARGV) {
$index++;
if ($last_arg_was_e) {
push @args, "-e", $arg;
$last_arg_was_e = 0; next;
}
if ($arg eq "-e") {
push @args, $arg if $index == $#ARGV;
$last_arg_was_e = 1; next;
}
if ($arg eq "--") {
push @args, @ARGV[$index .. $#ARGV];
last;
}
if ($arg =~ /^-O|^--open-files-in-pager(?:=|$)/ ||
$arg =~ /^(?:--no-null|--no-line-number|-h|--column|-c|--count|--heading|
-l|--files-with-matches|--name-only|-L|--files-without-match)$/x) {
push @invalid_args, $arg; next;
}
$full_name = 1 if ($arg eq "--full-name");
$full_name = 0 if ($arg eq "--no-full-name");
push @args, $arg;
}
print STDERR "Warning: Ignored invalid grep-blame flags: @invalid_args\n" if (@invalid_args);
my $color_flag = -t STDOUT ? "--color" : "--no-color";
unshift @args, "--no-column" if $git_grep_supports_column;
chdir $ENV{"GIT_PREFIX"} if $ENV{"GIT_PREFIX"};
open grep_fh, "-|", "git", "grep", "--line-number", "--null", $color_flag, @args;
chdir $ENV{"PWD"} if $full_name;
my $last_file; my @lines = (); my @texts = ();
while (<grep_fh>) {
if (/^Binary file .* matches$/) { print; next; }
my ($file, $line, $text, $context_separator) = /^(.*?)\0(.*?)\0(.*)$|^((?:\e\[[^m]*m)?--(?:\e\[[^m]*m)?)$/;
if (defined($context_separator)) {
do_blame() if (@lines);
@lines = (); @texts = ();
print $context_separator, "\n";
next;
}
if ($file eq "") { print; next; }
if (defined($last_file) && $file ne $last_file) {
do_blame() if (@lines);
@lines = (); @texts = ();
}
$last_file = $file; push @lines, $line; push @texts, $text;
}
do_blame() if (@lines);
close grep_fh;
exit $? >> 8;
sub do_blame {
open blame_fh, "-|", "git", "--no-pager", "blame", (map {"-L$_,$_"} @lines), "--", $last_file;
while (<blame_fh>) {
/^([^\)]*\))/;
shift @lines;
my $text = shift @texts;
my $out = "$1 $last_file $text";
(my $blank_out = $out) =~ s/\e\[[^m]*m//g;
if (length $blank_out > $truncate) {
$out =~ s/^((?:(?:\e\[[^m]*m)+(?:.|$)|.(?:\e\[[^m]*m)*|$(*SKIP)(*FAIL)){$truncate})(?=(?:(?:\e\[[^m]*m)+(?:.|$)|.(?:\e\[[^m]*m)*|$(*SKIP)(*FAIL)){15}).*/$1\e\[m...(truncated)/
}
print "$out\n";
}
close blame_fh;
}
' --
EOF
)"
# Subject length
git config --global alias.subject-length '!f() { git log -1 --format="%s" | tr -d "\n" | wc -m; }; f'
# Stash only staged changes
git config --global alias.stash-staged '!f() { : git stash ;
staged="$(git diff --staged --unified=0)";
unstaged="$(git diff --unified=0)";
[ "$staged" = "" ] && return;
[ "$unstaged" = "" ] && { git stash "$@"; return "$?"; };
printf "This is a potentially destructive command.\nBe sure you understand it before running it.\nContinue? [y/N]: ";
IFS= read -r cont; echo "$cont" | grep -iq "^y" || { echo "Not continuing."; return 1; };
git reset --hard &&
echo -E "$staged" | git apply --unidiff-zero - &&
git stash "$@" &&
echo -E "$unstaged" | git apply --unidiff-zero - || {
top="$(git rev-parse --git-dir)";
echo -E "$staged" >"$top/LAST_STAGED.diff";
echo -E "$unstaged" >"$top/LAST_UNSTAGED.diff";
printf "\x1b[0;31mERROR:\x1b[0m Could not stash staged.\nDiffs saved: try git apply --unidiff-zero .git/LAST_STAGED.diff .git/LAST_UNSTAGED.diff\n";
};
}; f'
# Stash only unstaged changes
git config --global alias.stash-unstaged '!f() { : git stash ;
staged="$(git diff --staged --unified=0)";
unstaged="$(git diff --unified=0)";
[ "$staged" = "" ] && { git stash "$@"; return "$?"; };
[ "$unstaged" = "" ] && return;
printf "This is a potentially destructive command.\nBe sure you understand it before running it.\nContinue? [y/N]: ";
IFS= read -r cont; echo "$cont" | grep -iq "^y" || { echo "Not continuing."; return 1; };
git reset --hard && echo -E "$unstaged" |
git apply --unidiff-zero - &&
git stash "$@" &&
echo -E "$staged" | git apply --unidiff-zero - || {
top="$(git rev-parse --git-dir)";
echo -E "$staged" >"$top/LAST_STAGED.diff";
echo -E "$unstaged" >"$top/LAST_UNSTAGED.diff";
printf "\x1b[0;31mERROR:\x1b[0m Could not stash unstaged.\nDiffs saved: try git apply --unidiff-zero .git/LAST_STAGED.diff .git/LAST_UNSTAGED.diff\n";
};
}; f'
# Press enter to view a stash's diff, q to exit the diff pager, and esc to exit fzf
git config --global alias.stash-browse '!f() {
git rev-parse || return
fzf="$(command -v fzf)"
[ -z "$fzf" ] && [ -x ~/.fzf/bin/fzf ] && fzf=~/.fzf/bin/fzf
[ -z "$fzf" ] && { printf >&2 "\e[0;31mgit stash-browse requires fzf to be installed\e[0m\n"; return 1; }
git stash list --color=always \
--format="%C(auto)%h %gd %C(dim red)[%C(reset)%C(red)%cr%C(dim red)]%C(reset) %C(auto)%<(70,trunc)%s %C(dim cyan)<%C(reset)%C(cyan)%an%C(dim cyan)>%C(reset)" |
"$fzf" --ansi --no-sort --reverse --tiebreak=index \
--bind "enter:execute: echo {} | sed \"s#\([a-f0-9]*\).*#\1#\" | head -1 |
xargs -I % sh -c \"git show --patch --stat --color=always % | less -R\""
return 0
}; f'
# Press enter to view a commit's diff, q to exit the diff pager, and esc to exit fzf
git config --global alias.browse '!f() { : git log ;
git rev-parse || return
fzf="$(command -v fzf)"
[ -z "$fzf" ] && [ -x ~/.fzf/bin/fzf ] && fzf=~/.fzf/bin/fzf
[ -z "$fzf" ] && { printf >&2 "\e[0;31mgit browse requires fzf to be installed\e[0m\n"; return 1; }
git log --graph --color=always \
--format="%C(auto)%h %C(dim red)[%C(reset)%C(red)%cr%C(dim red)]%C(reset)%C(auto)%d %s %C(dim cyan)<%C(reset)%C(cyan)%an%C(dim cyan)>%C(reset)" "$@" |
"$fzf" --ansi --no-sort --reverse --tiebreak=index \
--bind "enter:execute: echo {} |
sed \"s#^[* /|\-]*\([a-f0-9]*\).*#\1#\" | head -1 |
xargs -I % sh -c \"git show -m --patch --stat --color=always % | less -R\""
return 0
}; f'
# Highlight word differences in lines like GitHub
diff_highlight_found=''
if command -v diff-highlight &>/dev/null; then
diff_highlight_found='diff-highlight'
elif [[ -x /usr/share/git/diff-highlight/diff-highlight ]]; then
diff_highlight_found='/usr/share/git/diff-highlight/diff-highlight'
elif [[ -x /usr/share/doc/git/diff-highlight/diff-highlight ]]; then
diff_highlight_found='/usr/share/doc/git/diff-highlight/diff-highlight'
elif [[ -x /usr/share/git-core/contrib/diff-highlight ]]; then
diff_highlight_found='/usr/share/git-core/contrib/diff-highlight'
fi
if [[ -n "$diff_highlight_found" ]]; then
git config --global pager.log "$diff_highlight_found | less -RFX"
git config --global pager.show "$diff_highlight_found | less -RFX"
git config --global pager.diff "$diff_highlight_found | less -RFX"
fi
# Highlight whitespace differences in removed lines too
git config --global diff.wsErrorHighlight all
# Set a global gitignore file
git config --global core.excludesfile ~/.gitignore_global
# Add color to git output
git config --global color.ui auto
# Push only the current branch
git config --global push.default current
# Show the original code in merge conflicts
git config --global merge.conflictstyle diff3
# Show other changes when doing git pull --rebase
git config --global rebase.stat true
# Automatically set fixup and squash for commits made with --fixup and --squash
git config --global rebase.autoSquash true
# Enable git rerere
git config --global rerere.enabled true
# Fetch pull requests too
#git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
# Show the patch by default with git stash show
git config --global stash.showPatch true
# Automatically stash and unstash before and after a rebase
git config --global rebase.autoStash true
# Do not try to guess user and email when committing
git config --global user.useConfigOnly true
# Show ref names in git log
git config --global log.decorate full
# Show all untracked files in git status, not just root directories
git config --global status.showUntrackedFiles all
# Verify transferred objects in exchange for slower transfer
# If you have errors cloning or fetching, use --config transfer.fsckObjects=false
#git config --global transfer.fsckObjects true
# (disable for now)
git config --global transfer.fsckObjects false
# easy git instaweb
git config --global instaweb.local true
git config --global instaweb.httpd webrick
# Fix git rebases on mac
# https://www.git-tower.com/blog/make-git-rebase-safe-on-osx/
if [[ "$(uname)" = "Darwin" ]]; then
git config --global core.trustctime false
fi
# Update sandbox branch
# Old, but maybe a useful reference for shell functions in git aliases
# * origin/master is merged into your branch
# * your branch is pushed to the sandbox
#git config --global alias.update-sandbox $'!f() ( set -euo pipefail; if [[ "$#" -ne 1 ]]; then echo "Usage: update-sandbox <sandbox-name>"; echo "Example: update-sandbox pear"; exit 1; fi; set -x; git fetch; git merge origin/master; git push origin "HEAD:$1" || { set +x; echo "Force push required. Press any key to continue or ctrl+c to abort: "; read; set -x; git push --force origin "HEAD:$1"; } ); f'