-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_wf.html
More file actions
1046 lines (951 loc) · 38.9 KB
/
Copy pathgit_wf.html
File metadata and controls
1046 lines (951 loc) · 38.9 KB
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.7: http://docutils.sourceforge.net/" />
<title>My Git Workflow</title>
<meta name="author" content="bc Wong" />
<meta name="date" content="Dec 05, 2011" />
<style type="text/css">
/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 5951 2009-05-18 18:03:10Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/
/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
border: 0 }
table.borderless td, table.borderless th {
/* Override padding for "table.docutils td" with "! important".
The right padding separates the table cells. */
padding: 0 0.5em 0 0 ! important }
.first {
/* Override more specific margin styles with "! important". */
margin-top: 0 ! important }
.last, .with-subtitle {
margin-bottom: 0 ! important }
.hidden {
display: none }
a.toc-backref {
text-decoration: none ;
color: #527bbd; }
blockquote.epigraph {
margin: 2em 5em ; }
dl.docutils dd {
margin-bottom: 0.5em }
dl.docutils dt {
color: navy
}
div.abstract {
margin: 2em 5em }
div.abstract p.topic-title {
font-weight: bold ;
text-align: center }
div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
margin: 2em ;
border: medium outset ;
padding: 1em }
div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
font-weight: bold ;
font-family: sans-serif }
div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
color: red ;
font-weight: bold ;
font-family: sans-serif }
/* Uncomment (and remove this text!) to get reduced vertical space in
compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
margin-bottom: 0.5em }
div.compound .compound-last, div.compound .compound-middle {
margin-top: 0.5em }
*/
div.dedication {
margin: 2em 5em ;
text-align: center ;
font-style: italic }
div.dedication p.topic-title {
font-weight: bold ;
font-style: normal }
div.figure {
margin-left: 2em ;
margin-right: 2em }
div.footer, div.header {
clear: both;
font-size: smaller }
div.line-block {
display: block ;
margin-top: 1em ;
margin-bottom: 1em }
div.line-block div.line-block {
margin-top: 0 ;
margin-bottom: 0 ;
margin-left: 1.5em }
div.sidebar {
margin: 0 0 0.5em 1em ;
border: medium outset ;
padding: 1em ;
background-color: #ffffee ;
width: 40% ;
float: right ;
clear: right }
div.sidebar p.rubric {
font-family: sans-serif ;
font-size: medium }
div.system-messages {
margin: 5em }
div.system-messages h1 {
color: red }
div.system-message {
border: medium outset ;
padding: 1em }
div.system-message p.system-message-title {
color: red ;
font-weight: bold }
div.topic {
margin: 2em }
h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
margin-top: 0.4em }
h1.title {
text-align: center }
h2.subtitle {
text-align: center }
hr.docutils {
width: 75% }
img.align-left, .figure.align-left{
clear: left ;
float: left ;
margin-right: 1em }
img.align-right, .figure.align-right {
clear: right ;
float: right ;
margin-left: 1em }
.align-left {
text-align: left }
.align-center {
clear: both ;
text-align: center }
.align-right {
text-align: right }
/* reset inner alignment in figures */
div.align-right {
text-align: left }
/* div.align-center * { */
/* text-align: left } */
ol.simple, ul.simple {
margin-bottom: 1em }
ol.arabic {
list-style: decimal }
ol.loweralpha {
list-style: lower-alpha }
ol.upperalpha {
list-style: upper-alpha }
ol.lowerroman {
list-style: lower-roman }
ol.upperroman {
list-style: upper-roman }
p.attribution {
text-align: right ;
margin-left: 50% }
p.caption {
font-style: italic }
p.credits {
font-style: italic ;
font-size: smaller }
p.label {
white-space: nowrap }
p.rubric {
font-weight: bold ;
font-size: larger ;
color: maroon ;
text-align: center }
p.sidebar-title {
font-family: sans-serif ;
font-weight: bold ;
font-size: larger }
p.sidebar-subtitle {
font-family: sans-serif ;
font-weight: bold }
p.topic-title {
color: #527bbd;
font-weight: bold }
pre.address {
margin-bottom: 0 ;
margin-top: 0 ;
font: inherit }
pre.literal-block, pre.doctest-block {
margin-left: 2em ;
margin-right: 2em;
background-color: #f5f5f5;
padding: 5px;
}
span.classifier {
font-family: sans-serif ;
font-style: oblique }
span.classifier-delimiter {
font-family: sans-serif ;
font-weight: bold }
span.interpreted {
font-family: sans-serif }
span.option {
white-space: nowrap }
span.pre {
white-space: pre }
span.problematic {
color: red }
span.section-subtitle {
/* font-size relative to parent (h1..h6 element) */
font-size: 80% }
table.citation {
border-collapse: collapse;
border-left: solid 1px gray;
margin-left: 1px }
table.docinfo {
border-collapse: collapse;
margin: 2em 4em }
table.docutils {
border-collapse: collapse;
border: 3px solid #527bbd;
margin-top: 0.5em ;
margin-bottom: 0.5em }
table.footnote {
border-collapse: collapse;
border-left: solid 1px black;
margin-left: 1px }
table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
border: 0px solid #527bbd;
padding: 0.2em 0.5em 0.2em 0.5em;
vertical-align: top }
table.docutils th.field-name, table.docinfo th.docinfo-name {
font-weight: bold ;
text-align: left ;
white-space: nowrap ;
padding-left: 0 }
h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
font-size: 100% }
ul.auto-toc {
list-style-type: none }
/* bc addition */
body {
margin: 1em 10% 1em 10%;
}
h1, h2, h3, h4, h5, h6 {
color: #527bbd;
font-family: sans-serif;
margin-top: 1.2em;
margin-bottom: 0.5em;
line-height: 1.3;
}
h1, h2, h3 {
border-bottom: 2px solid silver;
}
h1.title {
border-top: 4px solid silver;
border-bottom: 4px solid silver;
padding: 0.5em 0 0.5em 0;
margin-bottom: 1em;
}
h2 {
padding-top: 0.5em;
}
h3 {
float: left;
}
h3 + * {
clear: left;
}
div.section {
font-family: serif
}
</style>
</head>
<body>
<div class="document" id="my-git-workflow">
<h1 class="title">My Git Workflow</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>bc Wong</td></tr>
<tr><th class="docinfo-name">Date:</th>
<td>Dec 05, 2011</td></tr>
</tbody>
</table>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="auto-toc simple">
<li><a class="reference internal" href="#preamble" id="id1">1 Preamble</a></li>
<li><a class="reference internal" href="#basic-concepts-for-beginners" id="id2">2 Basic Concepts for Beginners</a></li>
<li><a class="reference internal" href="#setup-and-introspection" id="id3">3 Setup and Introspection</a><ul class="auto-toc">
<li><a class="reference internal" href="#initialize-a-local-repository" id="id4">3.1 Initialize a Local Repository</a></li>
<li><a class="reference internal" href="#look-around" id="id5">3.2 Look Around</a></li>
<li><a class="reference internal" href="#make-more-branches" id="id6">3.3 Make More Branches</a></li>
</ul>
</li>
<li><a class="reference internal" href="#edit-and-commit" id="id7">4 Edit and Commit</a><ul class="auto-toc">
<li><a class="reference internal" href="#make-changes" id="id8">4.1 Make Changes</a></li>
<li><a class="reference internal" href="#commit" id="id9">4.2 Commit</a></li>
<li><a class="reference internal" href="#amend-a-commit" id="id10">4.3 Amend a Commit</a></li>
</ul>
</li>
<li><a class="reference internal" href="#fetch-push-and-conflicts" id="id11">5 Fetch, Push and Conflicts</a><ul class="auto-toc">
<li><a class="reference internal" href="#push-to-remote" id="id12">5.1 Push to Remote</a></li>
<li><a class="reference internal" href="#fetch-from-remote" id="id13">5.2 Fetch from Remote</a></li>
<li><a class="reference internal" href="#handle-conflicts" id="id14">5.3 Handle Conflicts</a></li>
</ul>
</li>
<li><a class="reference internal" href="#code-review" id="id15">6 Code Review</a><ul class="auto-toc">
<li><a class="reference internal" href="#review-uncommitted-code" id="id16">6.1 Review Uncommitted Code</a></li>
<li><a class="reference internal" href="#review-commits" id="id17">6.2 Review Commits</a></li>
<li><a class="reference internal" href="#prepare-a-patch" id="id18">6.3 Prepare a Patch</a></li>
</ul>
</li>
<li><a class="reference internal" href="#self-study" id="id19">7 Self-study</a></li>
<li><a class="reference internal" href="#my-git-setup" id="id20">8 My Git Setup</a></li>
</ul>
</div>
<div class="section" id="preamble">
<h1><a class="toc-backref" href="#id1">1 Preamble</a></h1>
<p>This is intended as a concise sing-along guide for task-specific git commands.
I have strong feelings about how a beginner should not be exposed to the full
spectrum of the git's power, or dangerous commands like "<tt class="docutils literal">git pull</tt>". So this
is an attempt to cover a subset of the concepts and commands in greater depth,
which hopefully help readers learn more about git on their own. I am using
git version 1.7.1 as I write this.</p>
</div>
<div class="section" id="basic-concepts-for-beginners">
<h1><a class="toc-backref" href="#id2">2 Basic Concepts for Beginners</a></h1>
<dl class="docutils">
<dt>repository</dt>
<dd><p class="first">A repository (or repo) is a stand-alone revision controlled tree. Your
repository may be connected to other repositories, and know about the
states of these other repositories. These are called "remote"s.</p>
<p class="last">Repositories are identified by human-friendly names.</p>
</dd>
<dt>commit object</dt>
<dd><p class="first">In git, commits work at the repository level, not at the individual files
level. A commit object describe a set of changes across many files. A commit
object also includes any addition or removal of files. <strong>Each commit object
points to its previous commit and represents the entire lineage</strong>, like a
linked list. So given
a single commit object, you can see the entire history of the repo up to
that commit. But there is no way to identify later commits in general.</p>
<p>Commit objects are identified by hashes (long non-sensible strings).
Commits may have "nicknames" like branch names and tag names.</p>
<p class="last">Once a commit object is created, it stays. Even if nothing is pointing to
the commit, you can still get to it if you know its hash.</p>
</dd>
<dt>branch</dt>
<dd><p class="first">You should think of a branch as a name that points to a commit. A
repository can have many branches. Most of the time, you will be working on
a branch. We call that the currently checked-out branch. As you create a
new commit on the current branch, the branch "pointer" auto advances to
point to the new commit.</p>
<p class="last">Branches are identified by human-friendly names. The <tt class="docutils literal">master</tt> branch is
typically trunk, by convention.</p>
</dd>
<dt><tt class="docutils literal">HEAD</tt></dt>
<dd><p class="first">A special name for the currently checked out branch. Since a branch is a
pointer to a commit, <tt class="docutils literal">HEAD</tt> also refers to the most recent commit in the
current branch.</p>
<p class="last">There is only one <tt class="docutils literal">HEAD</tt> in a repository, at most.</p>
</dd>
<dt>working tree</dt>
<dd><p class="first">The working tree is your view of repository tree. Git stores its commits
and branches in its own private area (the <tt class="docutils literal">.git</tt> directory under the repo
root). You can modify the working tree all you want; git can always reset
the working tree to the state described by any commit.</p>
<p class="last">If you have changed any file that git is tracking, your working tree is
<em>dirty</em>. Git will warn you when you are about to overwrite any dirty file.</p>
</dd>
</dl>
<div class="note">
<p class="first admonition-title">Note</p>
<p>This guide is written as a sing-along. You will learn git better if you
follow each step. Run this first to set things up:</p>
<pre class="literal-block">
$ git clone --bare git://github.com/bcwalrus/git-tutorial.git /tmp/git-tutorial
</pre>
<p class="last">And I want you to immediately forget what just happened. Right now. Forget.</p>
</div>
</div>
<div class="section" id="setup-and-introspection">
<h1><a class="toc-backref" href="#id3">3 Setup and Introspection</a></h1>
<div class="section" id="initialize-a-local-repository">
<h2><a class="toc-backref" href="#id4">3.1 Initialize a Local Repository</a></h2>
<p>Our project already exists elsewhere on some central server. Let's
initialize our local repository at <tt class="docutils literal"><span class="pre">~/ws/git-tutorial</span></tt>:</p>
<pre class="literal-block">
$ mkdir -p ~/ws
$ cd ~/ws
$ git clone git://github.com/bcwalrus/git-tutorial.git
Initialized empty Git repository in /home/bcwalrus/ws/git-tutorial/.git/
...
Resolving deltas: 100% (38/38), done.
$ cd git-tutorial/
$ git remote -v
origin git://github.com/bcwalrus/git-tutorial.git (fetch)
origin git://github.com/bcwalrus/git-tutorial.git (push)
</pre>
<ul>
<li><dl class="first docutils">
<dt><tt class="docutils literal">git clone <span class="pre">git://github.com/bcwalrus/git-tutorial.git</span></tt></dt>
<dd><p class="first last">This creates a local repo from the remote repo, which is specified by the
<tt class="docutils literal"><span class="pre">git://</span></tt> URL here. This remote repo is automatically linked, and is
called <tt class="docutils literal">origin</tt>. You can rename <tt class="docutils literal">origin</tt> to something else. It is
just a name.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="docutils literal">git remote <span class="pre">-v</span></tt></dt>
<dd><p class="first last">This shows you the remote repositories that we know about. This is useful
if you collaborate with other people who have their own repositories, and
need to link to their repos directly.</p>
</dd>
</dl>
</li>
</ul>
</div>
<div class="section" id="look-around">
<h2><a class="toc-backref" href="#id5">3.2 Look Around</a></h2>
<p>Let's explore a bit:</p>
<pre class="literal-block">
$ git branch
* master
$ git branch -r
origin/HEAD -> origin/master
origin/adam
origin/branch-1.0
origin/master
origin/tutorial
$ git log HEAD --graph --pretty=format:'%h -%d %s'
* 5c09211 - (HEAD, origin/master, origin/HEAD, master) Updated README.rst with usage and examples
* 406e897 - Fix python paths in scripts
* ecd00d3 - Update import statements to work with python 2.4
* 3b67880 - import * from a relative path is not allowed in python2.5
* 8563952 - Initial commit
</pre>
<p>Before we go further, run <tt class="docutils literal">gitk <span class="pre">--all</span></tt> (Linux) or <tt class="docutils literal">gitx</tt> (Mac) if you have
them installed. If you don't, install them now.</p>
<ul>
<li><dl class="first docutils">
<dt><tt class="docutils literal">git branch</tt></dt>
<dd><p class="first last">This shows you all local branches. The current branch has a
star in front of it. When you give it the <tt class="docutils literal"><span class="pre">-r</span></tt> flag, it shows the
branches in the remote repos. When you want to refer to a remote branch,
you need to prefix it with the repo name. e.g
<tt class="docutils literal"><span class="pre"><remote_repo>/<branch_name></span></tt>.
Remote branches, which you do not directly manipulate, are totally separate
from your local branches. They are different pointers.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="docutils literal">git log HEAD ...</tt></dt>
<dd><p class="first">This shows you the commit log starting from <tt class="docutils literal">HEAD</tt>. You can replace
<tt class="docutils literal">HEAD</tt> with another commit object. The flags makes the
output more concise and readable. I use a <tt class="docutils literal">gitlog</tt>
alias that is even more elaborate. See <a class="reference internal" href="#my-git-setup">My Git Setup</a>.</p>
<p class="last">The first line of the log output tells us that we at are commit 5c09211.
This part: <tt class="docutils literal">(HEAD, origin/master, origin/HEAD, master)</tt> lists the
pointers pointing to this commit.</p>
</dd>
</dl>
</li>
</ul>
</div>
<div class="section" id="make-more-branches">
<h2><a class="toc-backref" href="#id6">3.3 Make More Branches</a></h2>
<p>For a beginner of git, your branching strategy should be:</p>
<ul class="simple">
<li>Try to only work on one thing at a time, and do that work on your local
<tt class="docutils literal">master</tt> branch.</li>
<li>If you want to work on code that lives on a different remote branch, create a
local branch with the same name.</li>
</ul>
<p>Remember that our current local branch is <tt class="docutils literal">master</tt>. Suppose we want to work
on (or look at) the code in <tt class="docutils literal"><span class="pre">origin/branch-1.0</span></tt>. We do:</p>
<pre class="literal-block">
$ git branch branch-1.0 origin/branch-1.0
Branch branch-1.0 set up to track remote branch branch-1.0 from origin.
$ git branch
branch-1.0
* master
$ git checkout branch-1.0
Switched to branch 'branch-1.0'
$ git branch
* branch-1.0
master
$ git log HEAD --graph --pretty=format:'%h -%d %s'
* f3bcac9 - (HEAD, origin/branch-1.0, branch-1.0) Show command output in error message
* 406e897 - Fix python paths in scripts
* ecd00d3 - Update import statements to work with python 2.4
...
$ git checkout master
Switched to branch 'master'
</pre>
<ul>
<li><dl class="first docutils">
<dt><tt class="docutils literal">git branch <new_branch> <starting_point></tt></dt>
<dd><p class="first last">We created a new local branch called <tt class="docutils literal"><span class="pre">branch-1.0</span></tt>, which points to
whatever commit that <tt class="docutils literal"><span class="pre">origin/branch-1.0</span></tt> is currently pointing to.
The new branch shows up in the next <tt class="docutils literal">git branch</tt> command.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="docutils literal">git checkout <branch_name></tt></dt>
<dd><p class="first last">Checking out a branch means replacing your working tree with the content
from the specified branch (i.e. the commit object that the branch points
to). Git will only replace objects that it is tracking. It will not
overwrite your dirty changes. This command is always safe.</p>
</dd>
</dl>
</li>
</ul>
</div>
</div>
<div class="section" id="edit-and-commit">
<h1><a class="toc-backref" href="#id7">4 Edit and Commit</a></h1>
<div class="section" id="make-changes">
<h2><a class="toc-backref" href="#id8">4.1 Make Changes</a></h2>
<p>Let's make a change on the current branch, <tt class="docutils literal">master</tt>. Edit
<tt class="docutils literal">src/gitreview/cli/exceptions.py</tt>, and change the end of the file to:</p>
<pre class="literal-block">
class CommandArgumentsError(CLIError):
def __init__(self, msg):
msg = 'Command argument error: %s' % (msg,)
CLIError.__init__(self, message)
</pre>
<p>Next we add a TODO file:</p>
<pre class="literal-block">
$ cd ~/ws/git-tutorial
$ echo "Allow 3-way diff against common ancestor" > TODO
</pre>
<p>To look at the state of your working tree:</p>
<pre class="literal-block">
$ git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: src/gitreview/cli/exceptions.py
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# TODO
no changes added to commit (use "git add" and/or "git commit -a")
</pre>
<p>You should read the instructions in that output. Since git is already tracking
<tt class="docutils literal">exceptions.py</tt>, it automatically notices that it has changed. But we have
to tell git to track the new <tt class="docutils literal">TODO</tt> file manually:</p>
<pre class="literal-block">
$ git add TODO
</pre>
</div>
<div class="section" id="commit">
<h2><a class="toc-backref" href="#id9">4.2 Commit</a></h2>
<p>You should commit when you have made a consistent change. Commit frequently.
And always review your changes before you commit. (More on that later in
section <a class="reference internal" href="#code-review">Code Review</a>.)
Here, I will show you 2 ways to commit.</p>
<ol class="arabic">
<li><p class="first">You can <tt class="docutils literal">git add</tt> all files you would like to
commit, and then proceed to commit:</p>
<pre class="literal-block">
$ git add src/gitreview/cli/exceptions.py
$ git status
...
$ git commit
</pre>
</li>
<li><p class="first">Or, if you only have a small number of modified files, you can pass the
files as arguments:</p>
<pre class="literal-block">
$ git commit src/gitreview/cli/exceptions.py TODO
</pre>
</li>
</ol>
<p>After you entered <tt class="docutils literal">git commit ...</tt>, you will see the commit screen.
You will now enter the commit message. This is a reasonable guideline for
<a class="reference external" href="https://github.com/erlang/otp/wiki/Writing-good-commit-messages">good commit messages</a>. For this
commit message, simply enter "Test commit", save the file and quit.</p>
<p>Let's look at our history again:</p>
<pre class="literal-block">
$ git log HEAD --graph --pretty=format:'%h -%d %s'
* caca1f5 - (HEAD, master) Test commit
* 5c09211 - (origin/master, origin/HEAD) Updated README.rst with usage and examples
* 406e897 - Fix python paths in scripts
...
</pre>
<p>The <tt class="docutils literal">HEAD</tt> and <tt class="docutils literal">master</tt> branch pointers automatically advance to point to
the new commit. Note that this is a local commit. You have <strong>not</strong> pushed your
work back to the <tt class="docutils literal">origin</tt> repo yet -- <tt class="docutils literal">origin/master</tt> <strong>remains unchanged</strong>
for now.</p>
</div>
<div class="section" id="amend-a-commit">
<h2><a class="toc-backref" href="#id10">4.3 Amend a Commit</a></h2>
<p>We messed up. There is an error in our code. The last line below should
say <tt class="docutils literal">msg</tt> instead of <tt class="docutils literal">message</tt>:</p>
<pre class="literal-block">
class CommandArgumentsError(CLIError):
def __init__(self, msg):
msg = 'Command argument error: %s' % (msg,)
CLIError.__init__(self, message) ## ERROR !!!
</pre>
<p>Edit the file <tt class="docutils literal">src/gitreview/cli/exceptions.py</tt> and fix that line. If you do
<tt class="docutils literal">git status</tt>, you will see that the file is dirty again:</p>
<pre class="literal-block">
$ git status
...
$ git add src/gitreview/cli/exceptions.py
$ git commit --amend
</pre>
<p>The <tt class="docutils literal"><span class="pre">--amend</span></tt> flag amends <tt class="docutils literal">HEAD</tt>. You also have a chance to amend the
commit message. Save the commit message. Done. The new commit object will have
a different hash, and is a different object from the old commit. The old commit
object is still hanging around. If there are branches or tags pointing to the
old commit object, they do <strong>not</strong> see your amendment. But don't worry about it
if nothing is pointing to the old commit. (Look at it in <tt class="docutils literal">gitk <span class="pre">--all</span></tt>.)</p>
</div>
</div>
<div class="section" id="fetch-push-and-conflicts">
<h1><a class="toc-backref" href="#id11">5 Fetch, Push and Conflicts</a></h1>
<div class="note">
<p class="first admonition-title">Note</p>
<p>To simulate a conflict, let's have another user change <tt class="docutils literal">origin</tt> behind our
back. Run the following and immediately forget about it:</p>
<pre class="last literal-block">
$ git remote set-url origin /tmp/git-tutorial
$ pushd /tmp/git-tutorial
$ git symbolic-ref refs/heads/master refs/heads/adam ; popd
</pre>
</div>
<div class="section" id="push-to-remote">
<h2><a class="toc-backref" href="#id12">5.1 Push to Remote</a></h2>
<p>To share the work we have done, we need to push this commit back to the
<tt class="docutils literal">master</tt> branch (i.e. trunk) on <tt class="docutils literal">origin</tt>:</p>
<pre class="literal-block">
$ git push origin HEAD:master
To /tmp/git-tutorial.git
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to '/tmp/git-tutorial.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'Note about
fast-forwards' section of 'git push --help' for details.
</pre>
<p>Obviously it has failed. The <tt class="docutils literal">master</tt> branch in the remote repo has changed.
We will deal with that in the next section.</p>
<ul>
<li><dl class="first docutils">
<dt>non-fast-forward</dt>
<dd><p class="first last">A fancy word for conflict. "Fast forwarding" a branch to a commit is only
possible when that commit strictly builds on top of the branch, and
includes all commits from that branch.
Our push failed because <tt class="docutils literal">origin/master</tt> has been updated.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="docutils literal">git push origin HEAD:master</tt></dt>
<dd><p class="first">The general syntax is "<tt class="docutils literal">git push <repository> <span class="pre"><from_commit>:<to_branch></span></tt>".
The branch in <tt class="docutils literal"><to_branch></tt> is always a remote branch in that
<tt class="docutils literal"><repository></tt>. For example, if you want to push to a feature branch, do
"<tt class="docutils literal">git push origin <span class="pre">master:bug-1234</span></tt>". (Try it now.) This will create a new
branch called <tt class="docutils literal"><span class="pre">bug-1234</span></tt> in <tt class="docutils literal">origin</tt>. This new branch will point to the
same commit as your local <tt class="docutils literal">master</tt> branch.</p>
<p class="last">When you push something to a remote branch, you are asking that the remote
branch point to your commit. Git will transfer your commit object plus its
lineage to the remote repo. The remote branch will have the same hash.</p>
</dd>
</dl>
</li>
</ul>
</div>
<div class="section" id="fetch-from-remote">
<h2><a class="toc-backref" href="#id13">5.2 Fetch from Remote</a></h2>
<p>First we fetch the latest state of the <tt class="docutils literal">origin</tt> repo:</p>
<pre class="literal-block">
$ git fetch origin
From /tmp/git-tutorial
5c09211..f4aa278 master -> origin/master
$ gitk --all &
</pre>
<p><tt class="docutils literal">git fetch</tt> is one of the 3 git commands that talk to a remote repo in this
guide. The other 2 are <tt class="docutils literal">git clone</tt> and <tt class="docutils literal">git push</tt>. The rest of the time,
you are working on your local standalone repo, without affecting the rest of
the world.</p>
<p><tt class="docutils literal">git fetch</tt> does <strong>not</strong> update your working tree. It does <strong>not</strong> ask you to
resolve conflicts (unlike <tt class="docutils literal">svn update</tt>). It simply downloads the state of
<tt class="docutils literal">origin</tt> (to the private <tt class="docutils literal">.git</tt> directory) and updates the <tt class="docutils literal">origin/xyz</tt>
pointers. It is <strong>always</strong> safe to fetch, even if you are in the middle of
editing something. You can control when to merge with the remote branches and
handle conflicts.</p>
</div>
<div class="section" id="handle-conflicts">
<h2><a class="toc-backref" href="#id14">5.3 Handle Conflicts</a></h2>
<p>The diagram from <tt class="docutils literal">gitk</tt> show the divergence:</p>
<pre class="literal-block">
* [HEAD, master] Test commit
| * [origin/master] Enhance error logging
\|
* [HEAD^] Updated README.rst with usage and examples
</pre>
<p>What we want is a linear history with our "Test commit" applied directly on top
of "Enhance error logging" (<tt class="docutils literal">origin/master</tt>), to include everything that is
already in (<tt class="docutils literal">origin/master</tt>):</p>
<pre class="literal-block">
$ git rebase origin/master
...
CONFLICT (content): Merge conflict in src/gitreview/cli/exceptions.py
...
When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".
</pre>
<ul>
<li><dl class="first docutils">
<dt><tt class="docutils literal">git rebase origin/master</tt></dt>
<dd><p class="first">Git will first save all the commits not in <tt class="docutils literal">origin/master</tt> (i.e. all your
local commits). Then git will reset to <tt class="docutils literal">origin/master</tt>, and play
back those saved local commits. Remember that your local commits are being
played back. If there is a conflict, the "base" version is
<tt class="docutils literal">origin/master</tt>.</p>
<p>If you mess up during a rebase, you can always do "<tt class="docutils literal">git rebase <span class="pre">--abort</span></tt>"
and start over again.</p>
<p class="last">Note that you can rebase a local branch on top of another local branch as
well. You want to read the manpage carefully for this one ("<tt class="docutils literal">man git
rebase</tt>").</p>
</dd>
</dl>
</li>
</ul>
<p>Our rebase runs into a genuine conflict. Edit
<tt class="docutils literal">src/gitreview/cli/exceptions.py</tt>, fix it and save. Let's use the version
marked as <tt class="docutils literal">HEAD</tt>:</p>
<pre class="literal-block">
class CommandArgumentsError(CLIError):
def __init__(self, msg):
<<<<<<< HEAD
msg = 'bad arguments: %s' % (msg,)
=======
msg = 'Command argument error: %s' % (msg,)
>>>>>>> Test commit
CLIError.__init__(self, msg)
</pre>
<p>For fixing serious conflicts, I highly recommend <tt class="docutils literal">p4merge</tt>, which does
a real 3-way merge. You can invoke it with "<tt class="docutils literal">git mergetool</tt>". Any way,
let's finish the rebase:</p>
<pre class="literal-block">
$ git add src/gitreview/cli/exceptions.py
$ git rebase --continue
Applying: Test commit
Recorded resolution for 'src/gitreview/cli/exceptions.py'.
$ git log --graph --pretty=format:'%h -%d %s'
* 6a91d20 - (HEAD, master) Test commit
* f4aa278 - (origin/master, origin/adam, origin/HEAD) Enhance error logging
* 5c09211 - Updated README.rst with usage and examples
...
</pre>
<p>Our commit is on top of <tt class="docutils literal">origin/master</tt> again. Yeah! But this is just our
local repo. Let's push:</p>
<pre class="literal-block">
$ git push origin HEAD:master
Counting objects: 4, done.
...
To /tmp/git-tutorial
f4aa278..6a91d20 HEAD -> master
</pre>
</div>
</div>
<div class="section" id="code-review">
<h1><a class="toc-backref" href="#id15">6 Code Review</a></h1>
<div class="section" id="review-uncommitted-code">
<h2><a class="toc-backref" href="#id16">6.1 Review Uncommitted Code</a></h2>
<p>These are changes in the working tree that haven't been committed yet. Let's
make a change, and show the diff of <strong>the working tree</strong> against <tt class="docutils literal">HEAD</tt>.</p>
<ol class="arabic">
<li><p class="first">Method 1</p>
<pre class="literal-block">
$ echo "Add a graphical interface" >> TODO
$ git difftool TODO
</pre>
</li>
</ol>
<blockquote>
<ul>
<li><dl class="first docutils">
<dt><tt class="docutils literal">git difftool</tt></dt>
<dd><p class="first">You can configure the <tt class="docutils literal">diff.tool</tt> option in your <tt class="docutils literal">.gitconfig</tt> to avoid
having to specify the tool every time.</p>
<p>If you do not specify any files, i.e. simply <tt class="docutils literal">git difftool</tt>, it will loop
and show you the diff of <strong>all</strong> modified files.</p>
<p class="last">If you have already <tt class="docutils literal">git added</tt> your file, you need to add a <tt class="docutils literal"><span class="pre">--cached</span></tt>
argument.</p>
</dd>
</dl>
</li>
</ul>
</blockquote>
<ol class="arabic" start="2">
<li><p class="first">Method 2</p>
<pre class="literal-block">
$ src/git-review
Now processing modified file tutorial/git_wf.rst
git_wf.rst [diff]>
</pre>
</li>
</ol>
<blockquote>
<ul class="simple">
<li>This code base that we are using as an exercise is actually a code review
tool for git. Enter <tt class="docutils literal">?</tt> for help. It uses <tt class="docutils literal">vimdiff</tt>, tunable with the
<tt class="docutils literal">$GIT_REVIEW_DIFF</tt> environment variable. See installation instructions
at <<a class="reference external" href="https://github.com/bcwalrus/git-review">https://github.com/bcwalrus/git-review</a>>.</li>
</ul>
</blockquote>
</div>
<div class="section" id="review-commits">
<h2><a class="toc-backref" href="#id17">6.2 Review Commits</a></h2>
<p>Make the habit of reviewing your work again before pushing to <tt class="docutils literal">origin</tt>.
The instructions here are also applicable if you are the one reviewing someone
else's commit. Let's say that we are interested in our most recent commit.</p>
<ol class="arabic">
<li><p class="first">Method 1</p>
<pre class="literal-block">
$ git difftool HEAD~..HEAD
</pre>
</li>
</ol>
<blockquote>
<ul>
<li><dl class="first docutils">
<dt><tt class="docutils literal"><span class="pre">origin/master..HEAD</span></tt></dt>
<dd><p class="first">This "<tt class="docutils literal"><span class="pre"><from_commit>..<to_commit></span></tt>" syntax is how you specify a commit
range. As usual, you can use a hash or a pointer to refer to a commit. If
you omit either end, git will use <tt class="docutils literal">HEAD</tt>.</p>
<p class="last">The "<tt class="docutils literal">HEAD~</tt>" syntax means one commit before <tt class="docutils literal">HEAD</tt>. "<tt class="docutils literal">HEAD~2</tt>" means
2 commits before <tt class="docutils literal">HEAD</tt>, and so on. You can use the "<tt class="docutils literal">~</tt>" on anything
that refers to a commit (e.g. <tt class="docutils literal">master~</tt> also works).</p>
</dd>
</dl>
</li>
</ul>
<p>To use another example, suppose we are interested in the "Fix python paths"
commit:</p>
<pre class="literal-block">
$ git log --graph --pretty=format:'%h -%d %s'
* 6a91d20 - (HEAD, origin/master, origin/HEAD, master) Test commit
* f4aa278 - (origin/adam) Enhance error logging
* 5c09211 - Updated README.rst with usage and examples
* 406e897 - Fix python paths in scripts
...
</pre>
<p>We can use the hash and do:</p>
<pre class="literal-block">
$ git difftool 406e897~..406e897
</pre>
</blockquote>
<ol class="arabic" start="2">
<li><p class="first">Method 2. These achieve the same thing:</p>
<pre class="literal-block">
$ src/git-review HEAD~ HEAD
$ src/git-review 406e897~ 406e897
</pre>
</li>
</ol>
</div>
<div class="section" id="prepare-a-patch">
<h2><a class="toc-backref" href="#id18">6.3 Prepare a Patch</a></h2>
<p>If you need to generate a patch for ReviewBoard or whatever reason, do:</p>
<pre class="literal-block">
$ git diff HEAD~..HEAD > /tmp/bug.patch
</pre>
<p>The syntax is similar to <tt class="docutils literal">git difftool</tt>. <tt class="docutils literal">git diff</tt> takes an optional
commit range, and an optional list of paths. If you don't specify any path,
it'll diff all modified files. If you omit the commit range, it'll diff your
working tree against <tt class="docutils literal">HEAD</tt>.</p>
<p>Tip: For ReviewBoard, I find <a class="reference external" href="http://www.reviewboard.org/docs/manual/dev/users/tools/post-review/">post-review</a> more
convenient.</p>
</div>
</div>
<div class="section" id="self-study">
<h1><a class="toc-backref" href="#id19">7 Self-study</a></h1>
<p>Spend at least a week with the git commands you've learned here. When you
get comfortable with them, I recommend these more advanced commands. Read the
manpages and experiment:</p>
<pre class="literal-block">
git rebase -i
git reset
git show
git cherry-pick
git grep
</pre>
</div>
<div class="section" id="my-git-setup">
<h1><a class="toc-backref" href="#id20">8 My Git Setup</a></h1>
<p>I have this alias:</p>
<pre class="literal-block">
alias gitlog="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
</pre>
<p>Here is my <tt class="docutils literal"><span class="pre">~/.gitconfig</span></tt>. I highly recommend enhancing the
colour settings in yours.</p>
<pre class="literal-block">