forked from andersjanmyr/git-tips-and-tricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
804 lines (636 loc) · 28.1 KB
/
index.html
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
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
573
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Git - Practical Tips</title>
<meta name="author" content="Anders Janmyr">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="reveal.js/css/reveal.min.css">
<link rel="stylesheet" href="reveal.js/css/theme/beige.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<link rel="stylesheet" href="local.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="reveal.js/css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section data-markdown>
# Git
## Practical Tips
### http://anders.janmyr.com
### @andersjanmyr
### anders.janmyr@jayway.com
</section>
<section>
<section data-markdown>
![introduction](introduction.jpg)
</section>
<section data-markdown>
## CV (Legacy)
![cv-legacy](cv.png)
</section>
<section data-markdown>
## CV (Modern)
![cv-modern](github-tapirs.png)
</section>
<section data-markdown>
## http://commitlogsfromlastnight.com/
![bad-commit](commit-logs-from-last-night.png)
</section>
<section data-markdown>
## A Good Commit Message
![good-commit](awesome-commit-message.png)
</section>
</section>
<section>
<section data-markdown>
# Basics
![basics](basics.jpg)
</section>
<section data-markdown>
# Nothing Committed is *Ever* Lost
</section>
<section data-markdown>
## Git Storage
![git-storage](git-storage.png)
</section>
<section data-markdown>
## DAG
### Directed Acyclic Graph
![git-dag](git-dag.jpg)
</section>
<section data-markdown>
# Branch
## Pointer to a commit
### A sticky label
</section>
<section data-markdown>
# Git Has Three Parts
* HEAD
* Index
* Working Dir
</section>
<section data-markdown>
## `git add`
### Adds files to the index
</section>
<section data-markdown>
## `git commit`
### Adds index files to the HEAD
</section>
<section data-markdown>
## `git reset`
###Moves the head pointer
#### And optionally moves changes to and from the index
</section>
<section data-markdown>
# Nothing Committed is *Ever* Lost
</section>
</section>
<section>
<section data-markdown>
# History
![history](history.jpg)
</section>
<section data-markdown>
## `git rebase`
![git-rebase.png](git-rebase.png)
`git rebase` does NOT change the committed files
</section>
<section data-markdown>
## `git commit --amend`
# Amend latest commit with files in index
$ git commit --amend
# And resuse the commit message
$ git commit --amend -C HEAD
`git commit --amend` does NOT change the committed files
</section>
<section data-markdown>
## `git rebase -i`
![git-rebase-interactive.png](git-rebase-interactive.png)
`git rebase` does NOT change the committed files
</section>
<section data-markdown>
## `git cherry-pick`
![cherrypick.png](cherrypick.png)
`git cherry-pick` does NOT change the committed files
</section>
<section data-markdown>
## `git add -p`
![git-add-p](git-add-p.png)
</section>
<section data-markdown>
## `git add -p` ?
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any other
a - stage this hunk and all later in the file
d - do not stage this hunk nor any later in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided
s - split the current hunk into smaller hunks
e - manually edit the current hunk
</section>
<section data-markdown>
## `git reset`
# Moves the HEAD pointer back 4 steps
$ git reset HEAD~4
# Does NOT change working dir
$ git reset --mixed HEAD~4
# And adds the files to the index
# Does NOT change working dir
$ git reset --soft HEAD~4
# Also CHANGES working dir
$ git reset --hard HEAD~4
`git reset` does NOT change the committed files
</section>
<section data-markdown>
## `git reflog`
Contains references to where `HEAD` has been.
$ git reflog
eb06230 HEAD@{0}: reset: moving to master
ccfaa12 HEAD@{1}: reset: moving to head~
7b8a6cb HEAD@{2}: reset: moving to head~
eb06230 HEAD@{3}: checkout: moving from master to an
eb06230 HEAD@{4}: commit: Added reset
7b8a6cb HEAD@{5}: reset: moving to 7b8a6cb59edbfde
69c6f00 HEAD@{6}: reset: moving to 69c6f00
1910819 HEAD@{7}: reset: moving to 1910819
</section>
<section data-markdown>
## `git log -g`
Contains the log for the reflog
$ git log -g --oneline
23d1698 HEAD@{0}: commit: Updated abstract
ee3677f HEAD@{1}: cherry-pick: Fixed the find file by partial name
cf56e88 HEAD@{2}: cherry-pick: Removed summary
2289c5f HEAD@{3}: checkout: moving from finding to master
26bb1a6 HEAD@{4}: commit: Fixed the find file by partial name
b97bf68 HEAD@{5}: commit: Removed summary
bd6af0a HEAD@{6}: commit (amend): Removed everything but finding
2289c5f HEAD@{7}: merge finding: Fast-forward
### `--oneline` - Oneline output format
</section>
</section>
<section>
<section data-markdown>
# Finding in Git
![finding](finding.jpg)
</section>
<section data-markdown>
## Where is my file?
$ git ls-files | grep security
lib/dynamo-db/security.js
test/security-test.js
</section>
<section data-markdown>
## What message contains "fix"?
$ git log --oneline --grep=fix -i
7eeb362 Whitespace fix
e72c673 Load images as background images to fix scaling
ec5576b Fixed broken integration test, added test case
22e0a54 Fixed broken unit tests
99b18c3 Clean up qr resource leak, and fixed unit test
f8d9537 Pass qrstore in socket.io init, fixed broken test
### `-i` - Ignore case
### `--oneline` - Oneline output format
</section>
<section data-markdown>
## In what file can I find "crypt"?
$ git grep -n crypt
lib/dynamo-db/security.js:2:var crypto = require('crypto');
lib/dynamo-db/security.js:15: var hmac = crypto.createHmac('sha256', key);
### `-n` - show line numbers
</section>
<section data-markdown>
## Find "type" in revisions `master` and `8f0fb7f`.
git grep -l type master 8f0fb7f
master:lib/dynamo-db/index.js
master:lib/dynamo-db/security.js
master:package.json
8f0fb7f:lib/dynamo-db/index.js
8f0fb7f:package.json
### `-l` - show filenames only
</section>
<section data-markdown>
## What commits touched file 'Makefile'?
$ git log --oneline Makefile
c176cc0 Added deploy silent option to avoid tagging
86edd1e Removed client side scripts from coverage run
3a199d2 adds cdn caching for images, if env variable cdn
20a66ed pass file arguments to mocha to run single test
b46c077 Notify with growl when application is deployed
044a954 Removed copying of old file
7111636 Almost working with minification
### `--oneline` - Oneline output format
</section>
<section data-markdown>
## Who deleted my file?
$ git log --diff-filter=D -- test/create-table-test.js
commit ba6c4d8bc165b8fb8208979c3e5513bd53477d51
Author: Anders Janmyr anders@janmyr.com
Date: Wed Jan 25 09:46:52 2012 +0100
Removed the stupid failing test.
### --diff-filter - supports D, A, C, M, R
</section>
<section data-markdown>
## When was the file added?
### Part of name
$ git log --diff-filter=A --name-status |grep -C 6 int
commit 09420cfea8c7b569cd47f690104750fec358a10a
Author: Anders Janmyr anders@janmyr.com
Date: Tue Jan 24 16:23:52 2012 +0100
Extracted integration test
A integration-test/sts-test.js
commit 205db3965dec6c2c4c7b2bb75387a591d49e1951
Author: Anders Janmyr anders@janmyr.com
### --name-status - Short info about affected files
</section>
<section data-markdown>
## Who changed that line?
$ git blame test/security-test.js
205db396 (Anders Janmyr 2012-01-21 10:03:59 +0100 1) var vows = require('vows'),
205db396 (Anders Janmyr 2012-01-21 10:03:59 +0100 2) assert = require('assert');
09420cfe (Anders Janmyr 2012-01-24 16:23:52 +0100 3) var access = 'access';
09420cfe (Anders Janmyr 2012-01-24 16:23:52 +0100 4) var secret = 'secret';
</section>
<section data-markdown>
## Who deleted that line?
$ git blame --reverse head~6..head security-test.js
558b8e7f (Anders Janmyr 2012-01-25 16:53:52 +0100 1) var vows = require('vows'),
558b8e7f (Anders Janmyr 2012-01-25 16:53:52 +0100 2) assert = require('assert');
093c13e9 (Anders Janmyr 2012-01-24 17:26:09 +0100 3) var Security = require('dynamo-db').Security;
ba6c4d8b (Anders Janmyr 2012-01-25 09:46:52 +0100 4)
^b96c68b (Anders Janmyr 2012-01-21 12:33:50 +0100 5) var access = process.env['S3_KEY'];
### --reverse - The last revision in which a line has existed
### `git log 093c13e9..`
</section>
<section data-markdown>
## What commits contain the string 'aws'?
$ git log -Saws --diff-filter=M --patch
commit b96c68b839f204b310b79570bc3d27dc93cff588
diff --git a/lib/dynamo-db/security.js b/lib/dyna...
@@ -57,9 +58,10 @@ mod.url = function(host, path,...
var extParams = _.extend({}, this...
- var signedParams = 'iam.amazonaws.com'
- return request({ method: method,...
+ var signedParams = this.signedPa...
...
### -S string - Search for string
### -G regex - Search for regex
</section>
</section>
<section>
<section data-markdown>
# Config
![config](config.png)
</section>
<section data-markdown>
## Global `~/.gitconfig`
$ git config --global -l
user.name=Anders Janmyr
user.email=anders@janmyr.com
...
## Local `./.git/config`
$ git config --local -l
remote.origin.url=git@github.com:andersjanmyr/git-...
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
...
</section>
<section data-markdown>
## Automatic rebase pull
$ git config branch.master.rebase true
Sets
[branch "master"]
rebase = true
</section>
<section data-markdown>
## git-bash-completion
if [ -f /usr/local/etc/bash_completion.d/git-completion.bash ]
then
. /usr/local/etc/bash_completion.d/git-completion.bash
elif [ -f /etc/bash_completion.d/git ]; then
. /etc/bash_completion.d/git
fi
</section>
<section data-markdown>
## Git Prompt
function prompt {
local sha=$(git rev-parse --short HEAD)
export PS1="[\!:\W ${sha}$(__git_ps1 '@%s')]\n$ "
}
PROMPT_COMMAND='prompt'
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export GIT_PS1_SHOWUPSTREAM="auto"
[511:git-tips-and-tricks 50e6e0c@master *%=]
$
</section>
<section data-markdown>
## Git Prompt
## `__git_ps1`
if [ -f /usr/local/etc/bash_completion.d/git-prompt.sh ]
then
. /usr/local/etc/bash_completion.d/git-prompt.sh
fi
</section>
<section data-markdown>
## git config alias
$ git config --global alias.au 'add -u'
Sets
[alias]
au = add -u
## Explanation
# Adds all tracked files to the index
$ git add -u
*Use `--global` for aliases*
</section>
<section data-markdown>
## alias git amend
$ git config alias.amend 'commit --amend -C HEAD'
## Explanation
commit = The commit command
--amend = Amend the changes to previous commit
-C HEAD = Reuse the commit message
# Alternative
-c HEAD = Lets you edit the message
</section>
<section data-markdown>
## alias git ld
git config alias.ld 'log --diff-filter=D --summary'
Show commits with deleted files
## Explanation
log = The log command
--diff-filter=D = Only commits with deleted files
--summary = Show changes such as deletion, etc
</section>
<section data-markdown>
## alias git hist
![git-hist](git-hist.png)
</section>
<section data-markdown>
## git hist explained
$ git config alias.hist 'log --pretty="format:%C(blue)%h%C(red)%d%C(yellow) %s %C(green)%an%Creset, %ar" --graph'
## Explanation
<pre><code>log = The log command
--graph = Text-based graphical representation
--pretty="format" = Format according to spec
%C(color) = Change color
%h = Abbreviated commit hash (6b266c2)
%d = Ref names (HEAD, origin/master)
%s = Subject (first line of comment)
%an = Author name
%ar = Author date, relative</code></pre>
</section>
<section data-markdown>
# Shell functions
## When an alias can't cut it
</section>
<section data-markdown>
## `g()`
# `g` is short for git, defaults to `git s` (status)
function g() {
local cmd=${1-s}
shift
git $cmd $@
}
### Usage
$ g
## master
M README.md
$ g log
</section>
<section data-markdown>
## `gg()`
# Commit changes and quote all args as message
function gg() {
git commit -m "$*"
}
Usage (no quotes :)
$ gg Commit this awesome shell function
</section>
</section>
<section>
<section data-markdown>
# Other Useful Stuff
![other-stuff](other-useful-stuff.jpg)
</section>
<section data-markdown>
## List Tagged Commits
### `git log --simplify-by-decoration`
![simplify-log](simplify-by-decoration.png)
</section>
<section data-markdown>
## `stash save [-u] [message]`
$ git stash save -u Extracted new methods
Saved working directory and index state On master: Extracted new methods
HEAD is now at a9aeba6 Merge branch 'deprecate-calculations-with-block'
-u - Also stash untracked
</section>
<section data-markdown>
## `git stash pop` or `apply`
$ stash pop
# On branch master
# Your branch is behind 'origin/master' by 6 commits...
#
# Changes not staged for commit:
#
# modified: activerecord/test/cases/has...
#
Dropped refs/stash@{0} (c6b45e5b6fa0de7c...)
</section>
<section data-markdown>
## --assume-unchanged
$ git status --short
M config/database.yml
$ git update-index --assume-unchanged config/database.yml
$ git status --short
$
</section>
<section data-markdown>
## --no-assume-unchanged
$ git update-index --no-assume-unchanged config/database.yml
$ git status --short
M config/database.yml
</section>
<section data-markdown>
## `git stripspace`
![stripspace](git-stripspace.png)
* Strips trailing whitespace
* Add a newline at file end
* Merges multiple newlines into one
</section>
<section data-markdown>
# Submodules
### Resusing common code
</section>
<section data-markdown>
## `git submodule add`
$ git submodule add git://github.com/hakimel/reveal.js.git
# Clones and adds a repo as a submodule.
Added in .git/config
[submodule "reveal.js"]
url = git://github.com/hakimel/reveal.js.git
Added in .gitmodules
[submodule "reveal.js"]
path = reveal.js
url = git://github.com/hakimel/reveal.js.git
</section>
<section data-markdown>
## `git submodule add -b master`
$ git submodule add -b master git://github.com/hakimel/reveal.js.git
# Clones and adds a repo as a tracked submodule
Added in .gitmodules
[submodule "reveal.js"]
path = reveal.js
url = git://github.com/hakimel/reveal.js.git
branch = master
</section>
<section data-markdown>
## `git submodule update`
$ git submodule update
# Updates the submodules
## `git submodule update --remote`
$ git submodule update --remote
# Updates the submodules if the branch has changed
# Updates the files, but the change has to be committed
</section>
<section data-markdown>
## `git submodule sync`
$ git submodule sync
# Updates `.git/config` from `.gitmodules`
# useful when submodule URLs change upstream
</section>
<section data-markdown>
## `git submodule foreach`
$ git submodule foreach git pull origin master
# Updates each version to the latest version
</section>
<section data-markdown>
## `git submodule rm`
*DOES NOT EXIST*
* Delete the relevant section from the .gitmodules file.
* Delete the relevant section from .git/config.
* Run git rm --cached path-to-submodule (no trailing slash).
* Commit and delete the now untracked submodule files.
### Or use
script `git-submodule-rm` -- Google it!
</section>
<section data-markdown>
## `git bisect`
$ git bisect start failing-test first-good-test
Bisecting: 5 revisions left to test (roughly 3 steps)
[e29cc1e] Added two element sort function
$ git bisect run npm test
✔ 3 tests complete
[850743] Set the version to 1.0.0 before publishing
✔ 3 tests complete
[042d9f] Added failing test to demonstrate bisect
✖ 1 of 4 tests failed
[31b36f] Added travis status to readme.
042d9ff is the first bad commit
</section>
</section>
<section>
<section data-markdown>
# Github
![github](github.png)
</section>
<section data-markdown>
## The `hub` command
$ brew install hub
$ alias git=hub
</section>
<section data-markdown>
## `hub create`
$ git create
[ repo created on GitHub ]
git remote add origin git@github.com:YOUR_USER/CURRENT_REPO.git
</section>
<section data-markdown>
## `hub clone`
$ git clone schacon/ticgit
git clone git://github.com/schacon/ticgit.git
</section>
<section data-markdown>
## `hub remote add`
$ git remote add origin anders
git remote add origin git://github.com/anders/CURRENT_REPO.git
</section>
<section data-markdown>
## `hub push`
$ git push origin,staging,qa master
git push origin master
git push staging master
git push qa master
</section>
<section data-markdown>
## `hub pull-request`
# while on a topic branch called "feature":
$ git pull-request
[ opens text editor to edit title and body for the request ]
[ opened pull request on GitHub for "YOUR_USER:feature" ]
</section>
<section data-markdown>
## `hub browse`
$ git browse -- issues
open https://github.com/YOUR_USER/CURRENT_REPO/issues
</section>
</section>
<section>
<section data-markdown>
<img src='stupid-questions.jpg' align='right'></img>
# Questions
### http://anders.janmyr.com
### @andersjanmyr
### anders.janmyr@jayway.com
</section>
<section data-markdown>
## Resources
### Config
http://goo.gl/sS8Kv
### Finding
http://goo.gl/qQVsP
### Presentation
http://andersjanmyr.github.io/git-tips-and-tricks
### `gitconfig`
http://goo.gl/dl3xiG
</section>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
theme: Reveal.getQueryHash().theme,
transition: Reveal.getQueryHash().transition || 'default',
history: true,
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>