git.org 13.2 KB
Newer Older
to1ne's avatar
to1ne committed
1
#+TITLE:      git configuration
to1ne's avatar
to1ne committed
2
3
4
5
6
7
8
#+AUTHOR:     Toon Claes
#+KEYWORDS:   toon claes git config
#+STARTUP:    showall
-----

* git config
:PROPERTIES:
to1ne's avatar
to1ne committed
9
10
:header-args: :tangle ~/.config/git/config
:header-args+: :comments both :mkdirp yes
to1ne's avatar
to1ne committed
11
12
:END:

13
14
15
16
17
18
19
This file generates my git global config.

** User config

Only define my name, and not my email. And instruct git to avoid
trying to guess a default for the email. This will enforce me to set
the email for each git repo.
to1ne's avatar
to1ne committed
20
21
22

#+BEGIN_SRC conf
[user]
to1ne's avatar
to1ne committed
23
24
        useConfigOnly = true
        name = Toon Claes
25
#+END_SRC
to1ne's avatar
to1ne committed
26

to1ne's avatar
to1ne committed
27
** Core config
28
29

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
30
[core]
31
32
#+END_SRC

to1ne's avatar
to1ne committed
33
*** Global ignore file
34

to1ne's avatar
to1ne committed
35
Use a global ignore file.
36

37
38
39
I'm using the default =~/.config/git/ignore= so no need to set
this explicitly.

to1ne's avatar
to1ne committed
40
#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
41
        excludesfile = ~/.config/git/ignore
42
43
#+END_SRC

to1ne's avatar
to1ne committed
44
*** Commit comments
45

to1ne's avatar
to1ne committed
46
47
48
I tend to use markdown in my commit messages, and markdown uses the
=#= symbol to define headings. So instead of the default symbol =#=
used to define comments in commit messages, use =;=.
49
50

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
51
        commentchar = ";"
52
53
#+END_SRC

to1ne's avatar
to1ne committed
54
*** CR/LF
55

to1ne's avatar
to1ne committed
56
Don't do output conversion on end-of-line.
57
58

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
59
        autocrlf = input
60
61
#+END_SRC

to1ne's avatar
to1ne committed
62

to1ne's avatar
to1ne committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
** Help

#+begin_src
[help]
#+end_src

*** Auto-correct

Auto-correct mistyped commands after 0.1 seconds.

#+begin_src conf
        autoCorrect = 1
#+end_src

to1ne's avatar
to1ne committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
** Init

#+begin_src conf
[init]
#+end_src

*** Default branch

When creating a new repo, set the default branch to =main=.

#+begin_src conf
        defaultBranch = main
#+end_src

to1ne's avatar
to1ne committed
91
92
93
94
95
96
97
98
99
100
** Commit

#+begin_src conf
[commit]
#+end_src

*** Template

Specify a git commit template that will help me to write good commit messages.

to1ne's avatar
to1ne committed
101
#+begin_src conf-windows :tangle ~/.config/git/commit_msg.txt :comments no
to1ne's avatar
to1ne committed
102
103
104
105
106
107
108
109
110
111
112
; If applied, this commit will...


; Why is this change needed?
Prior to this change,

; How does it address the issue?
This change

; Provide links to any relevant tickets, articles or other resources

to1ne's avatar
to1ne committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
; Changelog trailer
; New feature
;Changelog: added
; Bug fix
;Changelog: fixed
; Feature change
;Changelog: changed
; New deprecation
;Changelog: deprecated
; Feature removal
;Changelog: removed
; Security fix
;Changelog: security
; Performance improvement
;Changelog: performance
; Other
;Changelog: other

;EE: true
to1ne's avatar
to1ne committed
132

to1ne's avatar
to1ne committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
; ## Help ##
;
; Subject line imperative uppercase verbs:
;
;   Add = Create a capability e.g. feature, test, dependency.
;   Drop = Delete a capability e.g. feature, test, dependency.
;   Fix = Fix an issue e.g. bug, typo, accident, misstatement.
;   Bump = Increase the version of something e.g. a dependency.
;   Make = Change the build process, or tools, or infrastructure.
;   Start = Begin doing something; e.g. enable a toggle, feature flag, etc.
;   Stop = End doing something; e.g. disable a toggle, feature flag, etc.
;   Optimize = A change that MUST be just about performance, e.g. speed up code.
;   Document = A change that MUST be only in the documentation, e.g. help files.
;   Refactor = A change that MUST be just refactoring.
;   Reformat = A change that MUST be just format, e.g. indent line, trim space, etc.
;   Rephrase = A change that MUST be just textual, e.g. edit a comment, doc, etc.
;
; ## About ##
;
; This is our team's starting point for our git commit messages.
; You can edit this template as you like, to customize it.
;
; For more information about git commit ideas and help:
; https://github.com/joelparkerhenderson/git_commit_message
to1ne's avatar
to1ne committed
157
158
159
160
161
#+end_src

And configure git to use this template.

#+begin_src conf
to1ne's avatar
to1ne committed
162
        template = ~/.config/git/commit_msg.txt
to1ne's avatar
to1ne committed
163
164
#+end_src

to1ne's avatar
to1ne committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
** Status

#+begin_src conf
[status]
#+end_src

*** Untracked files

Show also individual files in untracked directories.

#+begin_src conf
        showUntrackedFiles = all
#+end_src

*** Submodule summary

Enable submodule summary showing the summary of commits for modified
submodules.

#+begin_src conf
        submoduleSummary = true
#+end_src

188
189
190
191
192
193
194
195
196
197
198
199
200
201
** Diff

#+begin_src conf
[diff]
#+end_src

*** Moved code

Show moved lines in a different color.

#+begin_src conf
        colorMoved = zebra
#+end_src

to1ne's avatar
to1ne committed
202
203
204
205
206
207
208
209
** Fetching

#+begin_src conf
[fetch]
#+end_src

*** Prune

to1ne's avatar
to1ne committed
210
Prune the local tracking branches and tags when fetching from remote.
to1ne's avatar
to1ne committed
211
212

#+begin_src conf
to1ne's avatar
to1ne committed
213
214
        prune = true
        pruneTags = true
to1ne's avatar
to1ne committed
215
216
#+end_src

217
218
219
220
221
222
223
224
225
226
227
228
229
230
** Pull

#+begin_src conf
[pull]
#+end_src

*** Fast-forward only

When pulling only update the current branch by fast-forwarding.

#+begin_src conf
        ff = only
#+end_src

to1ne's avatar
to1ne committed
231
232
233
** Aliases

Define a set of aliases.
234
235

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
236
[alias]
237
238
#+END_SRC

to1ne's avatar
to1ne committed
239
*** Scrub
240

to1ne's avatar
to1ne committed
241
This alias scrubs away all local branches that are merged.
242
243

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
244
        scrub = !git branch --merged | egrep -v '(^\\*|master)' | xargs git branch -d
245
246
#+END_SRC

to1ne's avatar
to1ne committed
247
*** Work-in-progress
248

to1ne's avatar
to1ne committed
249
250
251
252
253
I don't like git stashes. [[https://twitter.com/to1ne/status/993495155234738177][A git stash is where code goes to die]]. So
instead of stashes, [[https://haacked.com/archive/2014/07/28/github-flow-aliases/][create a WIP commit]] on the current branch. This is
much better to keep context of the work-in-progress code, allows me to
push the code to a remote and have backup, and maybe also give me some
insights on the test results of CI.
254

to1ne's avatar
to1ne committed
255
256
257
I'm also providing an alias that undoes the last commit if it is a WIP
commit.

to1ne's avatar
to1ne committed
258
259
Improved =unwip= alias [[https://twitter.com/ZJvandeWeg/status/1050310776353886209][by ZJ]].

260
#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
261
        wip = !git add -A && git commit -mWIP
262
        unwip = !git log -1 --format=format:"%H" --invert-grep --grep '^WIP' | xargs git reset --soft
263
264
#+END_SRC

to1ne's avatar
to1ne committed
265
*** Rebase on remote master
266

to1ne's avatar
to1ne committed
267
268
269
270
I commonly rebase my working branch on =master= on the remote. That
remote can be =origin= or =upstream=. So this alias allows me to fetch
=master= from the =<remote>= and rebase current branch on
=<remote>/master=.
271

to1ne's avatar
to1ne committed
272
273
274
275
It expects one argument:
 + the name of the remote

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
276
        remaster = !sh -c 'git fetch $1 master && git rebase $1/master' -
to1ne's avatar
to1ne committed
277
278
279
280
281
282
283
284
285
286
#+END_SRC

*** Resync with remote

When the remote branch was rebased and you want to resynchronize your
local branch to the remote state, use this =reremote= alias.

It expects one argument:
 + the name of the remote

287
#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
288
        reremote = !sh -c 'git rev-parse --abbrev-ref HEAD | xargs git fetch $1 && git reset --hard FETCH_HEAD' -
289
290
#+END_SRC

291
292
293
294
295
296
297
298
299
300
301
302
303
*** Fetch and checkout

Fetch the branch from the remote, create a tracking branch for it and
check it out now 🎶the funk soul brother🎶.

It expects two arguments:
 + the name of the remote
 + the name of the branch

#+begin_src conf
	cofetch = !sh -c 'git fetch $1 $2:remotes/$1/$2 && git checkout -b $2 remotes/$1/$2' -
#+end_src

304
305
306
307
308
309
310
311
312
*** Politely force push current branch

When force pushing it's safer to use Git's =--force-with-lease= as this ensures
the latest changes were fetched before overwriting changes on the remote.

#+begin_src conf
	plush = !sh -c 'git push --force-with-lease $1 HEAD' -
#+end_src

313
314
** Rerere

to1ne's avatar
to1ne committed
315
316
317
git-rerere stands for: Reuse recorded resolution of conflicted merges.
It is an awesome feature that helps you to resolve the same conflicts
over and over again.
318
319

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
320
[rerere]
to1ne's avatar
to1ne committed
321
        enabled = true
322
323
#+END_SRC

to1ne's avatar
to1ne committed
324
** Markdown diffing
to1ne's avatar
to1ne committed
325

to1ne's avatar
to1ne committed
326
327
328
329
Below is an attempt to improve diffing markdown files. But I never got
it working and I just left it here.

#+BEGIN_SRC conf :tangle no
to1ne's avatar
to1ne committed
330
[diff "markdown"]
to1ne's avatar
to1ne committed
331
        tool = lowdown
to1ne's avatar
to1ne committed
332
333

[difftool "lowdown"]
to1ne's avatar
to1ne committed
334
        cmd = "lowdown-diff -s -Tman $LOCAL $REMOTE | groff -Tascii -man | less"
to1ne's avatar
to1ne committed
335
336
#+END_SRC

to1ne's avatar
to1ne committed
337
** Emacs diff tool
338

to1ne's avatar
to1ne committed
339
340
341
342
343
344
Use emacs(client) as a difftool.

#+begin_src conf
[difftool "ediffclient"]
        cmd = emacsclient --eval \"(ediff-files \\\"$LOCAL\\\" \\\"$REMOTE\\\")\"
#+end_src
345

to1ne's avatar
to1ne committed
346
347
348
349
350
** LFS

These settings involving LFS are generated by git itself.

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
351
[filter "lfs"]
to1ne's avatar
to1ne committed
352
353
354
355
        smudge = git-lfs smudge -- %f
        process = git-lfs filter-process
        required = true
        clean = git-lfs clean -- %f
to1ne's avatar
to1ne committed
356
357
#+END_SRC

to1ne's avatar
to1ne committed
358
359
360
361
362
363
364
365
366
367
368
** Protocol

Enable git protocol version 2.

Read [[https://about.gitlab.com/2018/12/10/git-protocol-v2-enabled-for-ssh-on-gitlab-dot-com/][about it]].

#+BEGIN_SRC conf
[protocol]
        version = 2
#+END_SRC

to1ne's avatar
to1ne committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
** Sendmail

Configure git to send mails directly from git.

Instructions came from [[https://git-send-email.io/][git-send-email.io]].

I have =msmtp= configured to send mails through Emacs, so I can just
use that tool here too.

#+begin_src conf
[sendemail]
    smtpserver = /usr/bin/msmtp
#+end_src

to1ne's avatar
to1ne committed
383
384
* Global Gitignore
:PROPERTIES:
to1ne's avatar
to1ne committed
385
386
:header-args: :tangle ~/.config/git/ignore
:header-args+: :comments both :mkdirp yes
to1ne's avatar
to1ne committed
387
388
:END:

to1ne's avatar
to1ne committed
389
390
391
392
** Ruby vendoring

Ignore gems installed in the local =vendor= directory.

to1ne's avatar
to1ne committed
393
#+BEGIN_SRC conf
394
/vendor/bundle
to1ne's avatar
to1ne committed
395
#+END_SRC
to1ne's avatar
to1ne committed
396

to1ne's avatar
to1ne committed
397
398
399
400
401
402
** GNU Global

For a while I used GNU Global for tagging code, so ignore the =TAGS=
files from being committed.

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
403
404
405
GPATH
GRTAGS
GTAGS
to1ne's avatar
to1ne committed
406
TAGS
to1ne's avatar
to1ne committed
407
#+END_SRC
to1ne's avatar
to1ne committed
408

to1ne's avatar
to1ne committed
409
410
411
412
413
414
415
416
417
** CCLS LSP

For C code I'm using CCLS with LSP in emacs. Ignore the files this
creates.

#+begin_src conf
.ccls-cache/
#+end_src

to1ne's avatar
to1ne committed
418
419
420
421
422
** Dir locals

Most project don't like Emacs =.dir-locals.el=.

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
423
.dir-locals.el
to1ne's avatar
to1ne committed
424
425
#+END_SRC

to1ne's avatar
to1ne committed
426
** Compile flags
to1ne's avatar
to1ne committed
427

to1ne's avatar
to1ne committed
428
429
For LSP in Emacs you might need to specify extra compiler flags, but
not all projects have it, so ignore files setting them.
to1ne's avatar
to1ne committed
430
431
432

#+begin_src conf
compile_flags.txt
to1ne's avatar
to1ne committed
433
.ccls
to1ne's avatar
to1ne committed
434
435
#+end_src

to1ne's avatar
to1ne committed
436
** Byebug
to1ne's avatar
to1ne committed
437

to1ne's avatar
to1ne committed
438
439
440
441
Byebug creates a history file in the current working directory, and
that's not something you want to check in.

#+BEGIN_SRC conf
to1ne's avatar
to1ne committed
442
443
444
.byebug_history
#+END_SRC

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482

* Git attributes
:PROPERTIES:
:header-args: :tangle ~/.config/git/attributes
:header-args+: :comments both :mkdirp yes
:END:

Improve diff output for various file types.

From: https://tekin.co.uk/2020/10/better-git-diff-output-for-ruby-python-elixir-and-more

#+BEGIN_SRC conf
*.c     diff=cpp
*.h     diff=cpp
*.c++   diff=cpp
*.h++   diff=cpp
*.cpp   diff=cpp
*.hpp   diff=cpp
*.cc    diff=cpp
*.hh    diff=cpp
*.cs    diff=csharp
*.css   diff=css
*.html  diff=html
*.xhtml diff=html
*.ex    diff=elixir
*.exs   diff=elixir
*.go    diff=golang
*.php   diff=php
*.pl    diff=perl
*.py    diff=python
*.md    diff=markdown
*.rb    diff=ruby
*.rake  diff=ruby
*.rs    diff=rust
*.lisp  diff=lisp
*.el    diff=lisp
#+END_SRC

to1ne's avatar
to1ne committed
483
484
485
486
487
488
489
* Hooks

User-wide hooks.

** Pre-push
:PROPERTIES:
:header-args: :tangle ~/.config/git/hooks/pre-push
to1ne's avatar
to1ne committed
490
:header-args+: :comments both :mkdirp yes :shebang "#!/bin/bash -e"
to1ne's avatar
to1ne committed
491
492
493
:END:

When a =Gemfile= is found, and =rubocop= is enabled, run Rubocop on
494
495
496
497
498
499
the files modified compared to the last merge commit. And since in our
codebase everything is applied to master with a merge commit, this can
be considered the upstream commit.

We also could have used =@{upstream}=, but that requires each branch
to set it's =upstream= branch, and that is not always the case.
to1ne's avatar
to1ne committed
500

501
#+BEGIN_SRC sh :tangle no
to1ne's avatar
to1ne committed
502
503
if [[ -f Gemfile.lock && -x $(bundle exec which rubocop) ]]; then
  echo "> Running Rubocop.."
504
  git diff --name-only --diff-filter=d $(git log --merges -1 --pretty=format:%H) | xargs bundle exec rubocop
to1ne's avatar
to1ne committed
505
506
507
508
else
  exit 0
fi
#+END_SRC
to1ne's avatar
to1ne committed
509

to1ne's avatar
to1ne committed
510
* Standalone commands
to1ne's avatar
to1ne committed
511
512
513
514
515

A section of small, standalone git command scripts

** Delouse
:PROPERTIES:
to1ne's avatar
to1ne committed
516
:header-args: :tangle ~/.local/bin/git-delouse
to1ne's avatar
to1ne committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
:header-args+: :comments both :mkdirp yes :shebang "#!/bin/bash -e"
:END:

I borrowed this command from [[https://twitter.com/nvie][@nvie]]'s [[https://github.com/nvie/git-toolbelt][git-toolbelt]].

Say you want to rebuild your last commit, but want to keep the commit
message. =git delouse= empties the last commit on the current branch and
places all changes back into the working tree.

#+BEGIN_SRC sh
#!/bin/sh
set -e
curr_sha=$(git rev-parse HEAD)

git reset HEAD~1
git commit --allow-empty --no-verify -C "$curr_sha"
#+END_SRC
to1ne's avatar
to1ne committed
534
535
536

** Abort
:PROPERTIES:
to1ne's avatar
to1ne committed
537
:header-args: :tangle ~/.local/bin/git-abort
to1ne's avatar
to1ne committed
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
:header-args+: :comments both :mkdirp yes :shebang "#!/bin/bash -e"
:END:

A short command to abort any action that is ongoing. That could be:
 + cherry-pick
 + rebase
 + merge

#+BEGIN_SRC sh
git_status=$(git status)

if [[ $git_status =~ 'currently cherry-picking' ]]; then
  git cherry-pick --abort
  echo "Ongoing cherry-pick aborted."
  exit 0
fi

if [[ $git_status =~ $(echo 'currently (editing a commit while )?rebasing') ]]; then
  git rebase --abort
  echo "Ongoing rebase aborted."
  exit 0
fi

if [[ $git_status =~ 'you are still merging' ]]; then
  git merge --abort
  echo "Ongoing merge aborted."
  exit 0
fi

echo >&2 "Nothing found to abort."
exit 1
#+END_SRC

** Continue
:PROPERTIES:
to1ne's avatar
to1ne committed
573
:header-args: :tangle ~/.local/bin/git-continue
to1ne's avatar
to1ne committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
:header-args+: :comments both :mkdirp yes :shebang "#!/bin/bash -e"
:END:

A short command to continue any action that is ongoing. That could be:
 + cherry-pick
 + rebase
 + merge

#+BEGIN_SRC sh
git_status=$(git status)

if [[ $git_status =~ 'currently cherry-picking' ]]; then
  echo "Continuing ongoing cherry-pick."
  git cherry-pick --continue
  exit 0
fi

if [[ $git_status =~ $(echo 'currently (editing a commit while )?rebasing') ]]; then
  git rebase --continue
  echo "Continuing ongoing rebase."
  exit 0
fi

if [[ $git_status =~ 'you are still merging' ]]; then
  echo "Continuing ongoing merge."
  git commit
  exit 0
fi

echo >&2 "Nothing found to continue."
exit 1
#+END_SRC
to1ne's avatar
to1ne committed
606
607
608

** Modified
:PROPERTIES:
to1ne's avatar
to1ne committed
609
:header-args: :tangle ~/.local/bin/git-modified
to1ne's avatar
to1ne committed
610
611
612
613
614
615
616
617
618
619
:header-args+: :comments both :mkdirp yes :shebang "#!/bin/bash -e"
:END:

Get all files that are modified.

There was some discussion on this on [[https://gitlab.slack.com/archives/C02PF508L/p1537285816000100?thread_ts=1537250844.000100&cid=C02PF508L][team chat]].

#+begin_src sh
git diff --name-only --diff-filter=d
#+end_src