File: quickstart.rst

package info (click to toggle)
python-git 3.1.45-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,772 kB
  • sloc: python: 18,642; sh: 186; makefile: 78
file content (244 lines) | stat: -rw-r--r-- 7,547 bytes parent folder | download | duplicates (2)
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
.. _quickdoc_toplevel:

.. highlight:: python

.. _quickdoc-label:

==============================
GitPython Quick Start Tutorial
==============================
Welcome to the GitPython Quickstart Guide! Designed for developers seeking a practical and interactive learning experience, this concise resource offers step-by-step code snippets to swiftly initialize/clone repositories, perform essential Git operations, and explore GitPython's capabilities. Get ready to dive in, experiment, and unleash the power of GitPython in your projects!


git.Repo
********

There are a few ways to create a :class:`git.Repo <git.repo.base.Repo>` object

Initialize a new git Repo
#########################

    .. literalinclude:: ../../test/test_quick_doc.py
        :language: python
        :dedent: 8
        :start-after: # [1-test_init_repo_object]
        :end-before: # ![1-test_init_repo_object]

Existing local git Repo
#######################

    .. literalinclude:: ../../test/test_quick_doc.py
        :language: python
        :dedent: 8
        :start-after: # [2-test_init_repo_object]
        :end-before: # ![2-test_init_repo_object]

Clone from URL
##############

For the rest of this tutorial we will use a clone from https://github.com/gitpython-developers/QuickStartTutorialFiles.git

    .. literalinclude:: ../../test/test_quick_doc.py
        :language: python
        :dedent: 8
        :start-after: # [1-test_cloned_repo_object]
        :end-before: # ![1-test_cloned_repo_object]


Trees & Blobs
**************

Latest Commit Tree
##################

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [12-test_cloned_repo_object]
            :end-before: # ![12-test_cloned_repo_object]

Any Commit Tree
###############

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [13-test_cloned_repo_object]
            :end-before: # ![13-test_cloned_repo_object]

Display level 1 Contents
########################

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [14-test_cloned_repo_object]
            :end-before: # ![14-test_cloned_repo_object]

Recurse through the Tree
########################

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [15-test_cloned_repo_object]
            :end-before: # ![15-test_cloned_repo_object]

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [16-test_cloned_repo_object]
            :end-before: # ![16-test_cloned_repo_object]




Usage
****************

Add file to staging area
########################


    .. literalinclude:: ../../test/test_quick_doc.py
        :language: python
        :dedent: 8
        :start-after: # [2-test_cloned_repo_object]
        :end-before: # ![2-test_cloned_repo_object]

    Now lets add the updated file to git

    .. literalinclude:: ../../test/test_quick_doc.py
        :language: python
        :dedent: 8
        :start-after: # [3-test_cloned_repo_object]
        :end-before: # ![3-test_cloned_repo_object]

    Notice the add method requires a list as a parameter

    Warning: If you experience any trouble with this, try to invoke :class:`git <git.cmd.Git>` instead via repo.git.add(path)

Commit
######

    .. literalinclude:: ../../test/test_quick_doc.py
        :language: python
        :dedent: 8
        :start-after: # [4-test_cloned_repo_object]
        :end-before: # ![4-test_cloned_repo_object]

List of commits associated with a file
#######################################

    .. literalinclude:: ../../test/test_quick_doc.py
        :language: python
        :dedent: 8
        :start-after: # [5-test_cloned_repo_object]
        :end-before: # ![5-test_cloned_repo_object]

    Notice this returns a generator object

    .. literalinclude:: ../../test/test_quick_doc.py
        :language: python
        :dedent: 8
        :start-after: # [6-test_cloned_repo_object]
        :end-before: # ![6-test_cloned_repo_object]

    returns list of :class:`Commit <git.objects.commit.Commit>` objects

Printing text files
####################
Lets print the latest version of `<local_dir>/dir1/file2.txt`

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [17-test_cloned_repo_object]
            :end-before: # ![17-test_cloned_repo_object]

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [18-test_cloned_repo_object]
            :end-before: # ![18-test_cloned_repo_object]

    Previous version of `<local_dir>/dir1/file2.txt`

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [18.1-test_cloned_repo_object]
            :end-before: # ![18.1-test_cloned_repo_object]

Status
######
    * Untracked files

        Lets create a new file

        .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [7-test_cloned_repo_object]
            :end-before: # ![7-test_cloned_repo_object]

        .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [8-test_cloned_repo_object]
            :end-before: # ![8-test_cloned_repo_object]

    * Modified files

        .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [9-test_cloned_repo_object]
            :end-before: # ![9-test_cloned_repo_object]

        .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [10-test_cloned_repo_object]
            :end-before: # ![10-test_cloned_repo_object]

        returns a list of :class:`Diff <git.diff.Diff>` objects

        .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [11-test_cloned_repo_object]
            :end-before: # ![11-test_cloned_repo_object]

Diffs
######

Compare staging area to head commit

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [11.1-test_cloned_repo_object]
            :end-before: # ![11.1-test_cloned_repo_object]

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [11.2-test_cloned_repo_object]
            :end-before: # ![11.2-test_cloned_repo_object]

Compare commit to commit

    .. literalinclude:: ../../test/test_quick_doc.py
            :language: python
            :dedent: 8
            :start-after: # [11.3-test_cloned_repo_object]
            :end-before: # ![11.3-test_cloned_repo_object]


More Resources
****************

Remember, this is just the beginning! There's a lot more you can achieve with GitPython in your development workflow.
To explore further possibilities and discover advanced features, check out the full :ref:`GitPython tutorial <tutorial_toplevel>`
and the :ref:`API Reference <api_reference_toplevel>`. Happy coding!