You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found the extra 8 spaces at the start of the examples in the tutorial
to be distracting. The Sphinx dedent option removes these extra spaces
from the rendered code blocks.
I also got a warning about the shell code example not being lexed as
Python, so I converted this to an explicit shell code block.
Copy file name to clipboardExpand all lines: doc/source/tutorial.rst
+50-1
Original file line number
Diff line number
Diff line change
@@ -19,41 +19,47 @@ The first step is to create a :class:`git.Repo <git.repo.base.Repo>` object to r
19
19
20
20
.. literalinclude:: ../../git/test/test_docs.py
21
21
:language: python
22
+
:dedent: 8
22
23
:start-after: # [1-test_init_repo_object]
23
24
:end-before: # ![1-test_init_repo_object]
24
25
25
26
In the above example, the directory ``self.rorepo.working_tree_dir`` equals ``/Users/mtrier/Development/git-python`` and is my working repository which contains the ``.git`` directory. You can also initialize GitPython with a *bare* repository.
26
27
27
28
.. literalinclude:: ../../git/test/test_docs.py
28
29
:language: python
30
+
:dedent: 8
29
31
:start-after: # [2-test_init_repo_object]
30
32
:end-before: # ![2-test_init_repo_object]
31
33
32
34
A repo object provides high-level access to your data, it allows you to create and delete heads, tags and remotes and access the configuration of the repository.
33
35
34
36
.. literalinclude:: ../../git/test/test_docs.py
35
37
:language: python
38
+
:dedent: 8
36
39
:start-after: # [3-test_init_repo_object]
37
40
:end-before: # ![3-test_init_repo_object]
38
41
39
42
Query the active branch, query untracked files or whether the repository data has been modified.
40
43
41
44
.. literalinclude:: ../../git/test/test_docs.py
42
45
:language: python
46
+
:dedent: 8
43
47
:start-after: # [4-test_init_repo_object]
44
48
:end-before: # ![4-test_init_repo_object]
45
49
46
50
Clone from existing repositories or initialize new empty ones.
:class:`Heads <git.refs.head.Head>` Heads are branches in git-speak. :class:`References <git.refs.reference.Reference>` are pointers to a specific commit or to other references. Heads and :class:`Tags <git.refs.tag.TagReference>` are a kind of references. GitPython allows you to query them rather intuitively.
73
80
74
81
.. literalinclude:: ../../git/test/test_docs.py
75
82
:language: python
83
+
:dedent: 8
76
84
:start-after: # [8-test_init_repo_object]
77
85
:end-before: # ![8-test_init_repo_object]
78
86
79
87
You can also create new heads ...
80
88
81
89
.. literalinclude:: ../../git/test/test_docs.py
82
90
:language: python
91
+
:dedent: 8
83
92
:start-after: # [9-test_init_repo_object]
84
93
:end-before: # ![9-test_init_repo_object]
85
94
86
95
... and tags ...
87
96
88
97
.. literalinclude:: ../../git/test/test_docs.py
89
98
:language: python
99
+
:dedent: 8
90
100
:start-after: # [10-test_init_repo_object]
91
101
:end-before: # ![10-test_init_repo_object]
92
102
93
103
You can traverse down to :class:`git objects <git.objects.base.Object>` through references and other objects. Some objects like :class:`commits <git.objects.commit.Commit>` have additional meta-data to query.
94
104
95
105
.. literalinclude:: ../../git/test/test_docs.py
96
106
:language: python
107
+
:dedent: 8
97
108
:start-after: # [11-test_init_repo_object]
98
109
:end-before: # ![11-test_init_repo_object]
99
110
100
111
:class:`Remotes <git.remote.Remote>` allow to handle fetch, pull and push operations, while providing optional real-time progress information to :class:`progress delegates <git.util.RemoteProgress>`.
101
112
102
113
.. literalinclude:: ../../git/test/test_docs.py
103
114
:language: python
115
+
:dedent: 8
104
116
:start-after: # [12-test_init_repo_object]
105
117
:end-before: # ![12-test_init_repo_object]
106
118
107
119
The :class:`index <git.index.base.IndexFile>` is also called stage in git-speak. It is used to prepare new commits, and can be used to keep results of merge operations. Our index implementation allows to stream date into the index, which is useful for bare repositories that do not have a working tree.
108
120
109
121
.. literalinclude:: ../../git/test/test_docs.py
110
122
:language: python
123
+
:dedent: 8
111
124
:start-after: # [13-test_init_repo_object]
112
125
:end-before: # ![13-test_init_repo_object]
113
126
114
127
:class:`Submodules <git.objects.submodule.Submodule>` represent all aspects of git submodules, which allows you query all of their related information, and manipulate in various ways.
115
128
116
129
.. literalinclude:: ../../git/test/test_docs.py
117
130
:language: python
131
+
:dedent: 8
118
132
:start-after: # [14-test_init_repo_object]
119
133
:end-before: # ![14-test_init_repo_object]
120
134
@@ -126,27 +140,31 @@ Examining References
126
140
127
141
.. literalinclude:: ../../git/test/test_docs.py
128
142
:language: python
143
+
:dedent: 8
129
144
:start-after: # [1-test_references_and_objects]
130
145
:end-before: # ![1-test_references_and_objects]
131
146
132
147
:class:`Tags <git.refs.tag.TagReference>` are (usually immutable) references to a commit and/or a tag object.
133
148
134
149
.. literalinclude:: ../../git/test/test_docs.py
135
150
:language: python
151
+
:dedent: 8
136
152
:start-after: # [2-test_references_and_objects]
137
153
:end-before: # ![2-test_references_and_objects]
138
154
139
155
A :class:`symbolic reference <git.refs.symbolic.SymbolicReference>` is a special case of a reference as it points to another reference instead of a commit.
140
156
141
157
.. literalinclude:: ../../git/test/test_docs.py
142
158
:language: python
159
+
:dedent: 8
143
160
:start-after: # [3-test_references_and_objects]
144
161
:end-before: # ![3-test_references_and_objects]
145
162
146
163
Access the :class:`reflog <git.refs.log.RefLog>` easily.
147
164
148
165
.. literalinclude:: ../../git/test/test_docs.py
149
166
:language: python
167
+
:dedent: 8
150
168
:start-after: # [4-test_references_and_objects]
151
169
:end-before: # ![4-test_references_and_objects]
152
170
@@ -156,20 +174,23 @@ You can easily create and delete :class:`reference types <git.refs.reference.Ref
156
174
157
175
.. literalinclude:: ../../git/test/test_docs.py
158
176
:language: python
177
+
:dedent: 8
159
178
:start-after: # [5-test_references_and_objects]
160
179
:end-before: # ![5-test_references_and_objects]
161
180
162
181
Create or delete :class:`tags <git.refs.tag.TagReference>` the same way except you may not change them afterwards.
163
182
164
183
.. literalinclude:: ../../git/test/test_docs.py
165
184
:language: python
185
+
:dedent: 8
166
186
:start-after: # [6-test_references_and_objects]
167
187
:end-before: # ![6-test_references_and_objects]
168
188
169
189
Change the :class:`symbolic reference <git.refs.symbolic.SymbolicReference>` to switch branches cheaply (without adjusting the index or the working tree).
170
190
171
191
.. literalinclude:: ../../git/test/test_docs.py
172
192
:language: python
193
+
:dedent: 8
173
194
:start-after: # [7-test_references_and_objects]
174
195
:end-before: # ![7-test_references_and_objects]
175
196
@@ -183,27 +204,31 @@ In GitPython, all objects can be accessed through their common base, can be comp
183
204
184
205
.. literalinclude:: ../../git/test/test_docs.py
185
206
:language: python
207
+
:dedent: 8
186
208
:start-after: # [8-test_references_and_objects]
187
209
:end-before: # ![8-test_references_and_objects]
188
210
189
211
Common fields are ...
190
212
191
213
.. literalinclude:: ../../git/test/test_docs.py
192
214
:language: python
215
+
:dedent: 8
193
216
:start-after: # [9-test_references_and_objects]
194
217
:end-before: # ![9-test_references_and_objects]
195
218
196
219
:class:`Index objects <git.objects.base.IndexObject>` are objects that can be put into git's index. These objects are trees, blobs and submodules which additionally know about their path in the file system as well as their mode.
197
220
198
221
.. literalinclude:: ../../git/test/test_docs.py
199
222
:language: python
223
+
:dedent: 8
200
224
:start-after: # [10-test_references_and_objects]
201
225
:end-before: # ![10-test_references_and_objects]
202
226
203
227
Access :class:`blob <git.objects.blob.Blob>` data (or any object data) using streams.
204
228
205
229
.. literalinclude:: ../../git/test/test_docs.py
206
230
:language: python
231
+
:dedent: 8
207
232
:start-after: # [11-test_references_and_objects]
208
233
:end-before: # ![11-test_references_and_objects]
209
234
@@ -217,34 +242,39 @@ Obtain commits at the specified revision
217
242
218
243
.. literalinclude:: ../../git/test/test_docs.py
219
244
:language: python
245
+
:dedent: 8
220
246
:start-after: # [12-test_references_and_objects]
221
247
:end-before: # ![12-test_references_and_objects]
222
248
223
249
Iterate 50 commits, and if you need paging, you can specify a number of commits to skip.
224
250
225
251
.. literalinclude:: ../../git/test/test_docs.py
226
252
:language: python
253
+
:dedent: 8
227
254
:start-after: # [13-test_references_and_objects]
228
255
:end-before: # ![13-test_references_and_objects]
229
256
230
257
A commit object carries all sorts of meta-data
231
258
232
259
.. literalinclude:: ../../git/test/test_docs.py
233
260
:language: python
261
+
:dedent: 8
234
262
:start-after: # [14-test_references_and_objects]
235
263
:end-before: # ![14-test_references_and_objects]
236
264
237
265
Note: date time is represented in a ``seconds since epoch`` format. Conversion to human readable form can be accomplished with the various `time module <http://docs.python.org/library/time.html>`_ methods.
238
266
239
267
.. literalinclude:: ../../git/test/test_docs.py
240
268
:language: python
269
+
:dedent: 8
241
270
:start-after: # [15-test_references_and_objects]
242
271
:end-before: # ![15-test_references_and_objects]
243
272
244
273
You can traverse a commit's ancestry by chaining calls to ``parents``
245
274
246
275
.. literalinclude:: ../../git/test/test_docs.py
247
276
:language: python
277
+
:dedent: 8
248
278
:start-after: # [16-test_references_and_objects]
249
279
:end-before: # ![16-test_references_and_objects]
250
280
@@ -257,41 +287,47 @@ A :class:`tree <git.objects.tree.Tree>` records pointers to the contents of a di
257
287
258
288
.. literalinclude:: ../../git/test/test_docs.py
259
289
:language: python
290
+
:dedent: 8
260
291
:start-after: # [17-test_references_and_objects]
261
292
:end-before: # ![17-test_references_and_objects]
262
293
263
294
Once you have a tree, you can get its contents
264
295
265
296
.. literalinclude:: ../../git/test/test_docs.py
266
297
:language: python
298
+
:dedent: 8
267
299
:start-after: # [18-test_references_and_objects]
268
300
:end-before: # ![18-test_references_and_objects]
269
301
270
302
It is useful to know that a tree behaves like a list with the ability to query entries by name
271
303
272
304
.. literalinclude:: ../../git/test/test_docs.py
273
305
:language: python
306
+
:dedent: 8
274
307
:start-after: # [19-test_references_and_objects]
275
308
:end-before: # ![19-test_references_and_objects]
276
309
277
310
There is a convenience method that allows you to get a named sub-object from a tree with a syntax similar to how paths are written in a posix system
278
311
279
312
.. literalinclude:: ../../git/test/test_docs.py
280
313
:language: python
314
+
:dedent: 8
281
315
:start-after: # [20-test_references_and_objects]
282
316
:end-before: # ![20-test_references_and_objects]
283
317
284
318
You can also get a commit's root tree directly from the repository
285
319
286
320
.. literalinclude:: ../../git/test/test_docs.py
287
321
:language: python
322
+
:dedent: 8
288
323
:start-after: # [21-test_references_and_objects]
289
324
:end-before: # ![21-test_references_and_objects]
290
325
291
326
As trees allow direct access to their intermediate child entries only, use the traverse method to obtain an iterator to retrieve entries recursively
292
327
293
328
.. literalinclude:: ../../git/test/test_docs.py
294
329
:language: python
330
+
:dedent: 8
295
331
:start-after: # [22-test_references_and_objects]
296
332
:end-before: # ![22-test_references_and_objects]
297
333
@@ -304,13 +340,15 @@ Modify the index with ease
304
340
305
341
.. literalinclude:: ../../git/test/test_docs.py
306
342
:language: python
343
+
:dedent: 8
307
344
:start-after: # [23-test_references_and_objects]
308
345
:end-before: # ![23-test_references_and_objects]
309
346
310
347
Create new indices from other trees or as result of a merge. Write that result to a new index file for later inspection.
311
348
312
349
.. literalinclude:: ../../git/test/test_docs.py
313
350
:language: python
351
+
:dedent: 8
314
352
:start-after: # [24-test_references_and_objects]
315
353
:end-before: # ![24-test_references_and_objects]
316
354
@@ -321,13 +359,15 @@ Handling Remotes
321
359
322
360
.. literalinclude:: ../../git/test/test_docs.py
323
361
:language: python
362
+
:dedent: 8
324
363
:start-after: # [25-test_references_and_objects]
325
364
:end-before: # ![25-test_references_and_objects]
326
365
327
366
You can easily access configuration information for a remote by accessing options as if they where attributes. The modification of remote configuration is more explicit though.
328
367
329
368
.. literalinclude:: ../../git/test/test_docs.py
330
369
:language: python
370
+
:dedent: 8
331
371
:start-after: # [26-test_references_and_objects]
332
372
:end-before: # ![26-test_references_and_objects]
333
373
@@ -343,7 +383,9 @@ This one sets a custom script to be executed in place of `ssh`, and can be used
343
383
with repo.git.custom_environment(GIT_SSH=ssh_executable):
344
384
repo.remotes.origin.fetch()
345
385
346
-
Here's an example executable that can be used in place of the `ssh_executable` above::
386
+
Here's an example executable that can be used in place of the `ssh_executable` above:
@@ -383,13 +426,15 @@ Diffs can be made between the Index and Trees, Index and the working tree, trees
383
426
384
427
.. literalinclude:: ../../git/test/test_docs.py
385
428
:language: python
429
+
:dedent: 8
386
430
:start-after: # [27-test_references_and_objects]
387
431
:end-before: # ![27-test_references_and_objects]
388
432
389
433
The item returned is a DiffIndex which is essentially a list of Diff objects. It provides additional filtering to ease finding what you might be looking for.
390
434
391
435
.. literalinclude:: ../../git/test/test_docs.py
392
436
:language: python
437
+
:dedent: 8
393
438
:start-after: # [28-test_references_and_objects]
394
439
:end-before: # ![28-test_references_and_objects]
395
440
@@ -413,13 +458,15 @@ To switch between branches similar to ``git checkout``, you effectively need to
413
458
414
459
.. literalinclude:: ../../git/test/test_docs.py
415
460
:language: python
461
+
:dedent: 8
416
462
:start-after: # [29-test_references_and_objects]
417
463
:end-before: # ![29-test_references_and_objects]
418
464
419
465
The previous approach would brutally overwrite the user's changes in the working copy and index though and is less sophisticated than a ``git-checkout``. The latter will generally prevent you from destroying your work. Use the safer approach as follows.
420
466
421
467
.. literalinclude:: ../../git/test/test_docs.py
422
468
:language: python
469
+
:dedent: 8
423
470
:start-after: # [30-test_references_and_objects]
424
471
:end-before: # ![30-test_references_and_objects]
425
472
@@ -430,6 +477,7 @@ In this example, we will initialize an empty repository, add an empty file to th
430
477
431
478
.. literalinclude:: ../../git/test/test_docs.py
432
479
:language: python
480
+
:dedent: 8
433
481
:start-after: def test_add_file_and_commit
434
482
:end-before: # ![test_add_file_and_commit]
435
483
@@ -441,6 +489,7 @@ In case you are missing functionality as it has not been wrapped, you may conven
0 commit comments