|
.. _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: |
|
:end-before: |
|
|
|
Existing local git Repo |
|
|
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
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: |
|
:end-before: |
|
|
|
|
|
Trees & Blobs |
|
************** |
|
|
|
Latest Commit Tree |
|
|
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
Any Commit Tree |
|
|
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
Display level 1 Contents |
|
|
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
Recurse through the Tree |
|
|
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
|
|
|
|
|
|
Usage |
|
**************** |
|
|
|
Add file to staging area |
|
|
|
|
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
Now lets add the updated file to git |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
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: |
|
:end-before: |
|
|
|
List of commits associated with a file |
|
|
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
Notice this returns a generator object |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
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: |
|
:end-before: |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
Previous version of `<local_dir>/dir1/file2.txt` |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
Status |
|
|
|
* Untracked files |
|
|
|
Lets create a new file |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
* Modified files |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
returns a list of :class:`Diff <git.diff.Diff>` objects |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
Diffs |
|
|
|
|
|
Compare staging area to head commit |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
Compare commit to commit |
|
|
|
.. literalinclude:: ../../test/test_quick_doc.py |
|
:language: python |
|
:dedent: 8 |
|
:start-after: |
|
:end-before: |
|
|
|
|
|
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! |
|
|