|
| 1 | +title: First Steps with GitPython |
| 2 | +slug: first-steps-gitpython |
| 3 | +meta: Learn to use the GitPython library to programmatically interact with Git repositories. |
| 4 | +category: post |
| 5 | +date: 2017-11-29 |
| 6 | +modified: 2017-11-29 |
| 7 | +headerimage: /img/171129-gitpython/header.jpg |
| 8 | +headeralt: Python and Git logos, copyright their respective owners. |
| 9 | + |
| 10 | + |
| 11 | +[GitPython](http://gitpython.readthedocs.io/) is a Python code library |
| 12 | +for programmatically reading from and writing to [Git](/git.html) |
| 13 | +[source control](/source-control.html) repositories. |
| 14 | + |
| 15 | +Let's learn how to use GitPython by quickly installing it and reading from |
| 16 | +a local cloned Git repository. |
| 17 | + |
| 18 | + |
| 19 | +## Our Tools |
| 20 | +This tutorial should work with either [Python 2.7 or 3](/python-2-or-3.html), |
| 21 | +but Python 3, especially 3.6+, is strongly recommended for all new |
| 22 | +applications. I used |
| 23 | +[Python 3.6.3](https://www.python.org/downloads/release/python-363/) to |
| 24 | +write this post. In addition to Python, throughout this tutorial we |
| 25 | +will also use the following |
| 26 | +[application dependencies](/application-dependencies.html): |
| 27 | + |
| 28 | +* [Git](/git.html), |
| 29 | + a [source (version) control](/static-site-generator.html) implementation, |
| 30 | + [version 2.15.1](https://github.com/git/git/tree/v2.15.1) |
| 31 | +* [GitPython](https://github.com/gitpython-developers/GitPython/tree/2.1.7) |
| 32 | + version [2.1.7](https://github.com/gitpython-developers/GitPython/tree/2.1.7) |
| 33 | +* [pip](https://pip.pypa.io/en/stable/) and |
| 34 | + [virtualenv](https://virtualenv.pypa.io/en/latest/), which come |
| 35 | + packaged with Python 3, to install and isolate the GitPython library |
| 36 | + from any of your other Python projects |
| 37 | + |
| 38 | +Take a look at |
| 39 | +[this guide for setting up Python 3 and Flask on Ubuntu 16.04 LTS](/blog/python-3-flask-green-unicorn-ubuntu-1604-xenial-xerus.html) |
| 40 | +if you need specific instructions to get a base |
| 41 | +[Python development environment](/development-environments.html) set up. |
| 42 | + |
| 43 | +All code in this blog post is available open source under the MIT license |
| 44 | +on GitHub under the |
| 45 | +[first-steps-gitpython directory of the blog-code-examples repository](https://github.com/fullstackpython/blog-code-examples/tree/master/first-steps-gitpython). |
| 46 | +Use and abuse the source code as you like for your own applications. |
| 47 | + |
| 48 | + |
| 49 | +## Install GitPython |
| 50 | +Start by creating a new virtual environment for your project. My virtualenv |
| 51 | +is named `testgit` but you can name yours whatever matches the project |
| 52 | +you are creating. |
| 53 | + |
| 54 | +```bash |
| 55 | +python3 -m venv gitpy |
| 56 | +``` |
| 57 | + |
| 58 | +Activate the newly-created virtualenv. |
| 59 | + |
| 60 | +``` |
| 61 | +source gitpy/bin/activate |
| 62 | +``` |
| 63 | + |
| 64 | +The virtualenv's name will be prepended to the command prompt after |
| 65 | +activation. |
| 66 | + |
| 67 | +<img src="/img/171129-gitpython/activate-virtualenv.png" width="100%" class="technical-diagram img-rounded" style="border:1px solid #ccc" alt="Create and activate the Python virtual environment."> |
| 68 | + |
| 69 | +Now that the virutalenv is activated we can use the `pip` command to install |
| 70 | +GitPython. |
| 71 | + |
| 72 | + |
| 73 | +```bash |
| 74 | +pip install gitpython==2.1.7 |
| 75 | +``` |
| 76 | + |
| 77 | +Run the `pip` command and after everything is installed you should see output |
| 78 | +similar to the following "Successfully installed" message. |
| 79 | + |
| 80 | +```bash |
| 81 | +(gitpy) $ pip install gitpython==2.1.7 |
| 82 | +Collecting gitpython==2.1.7 |
| 83 | + Downloading GitPython-2.1.7-py2.py3-none-any.whl (446kB) |
| 84 | + 100% |████████████████████████████████| 450kB 651kB/s |
| 85 | +Collecting gitdb2>=2.0.0 (from gitpython==2.1.7) |
| 86 | + Downloading gitdb2-2.0.3-py2.py3-none-any.whl (63kB) |
| 87 | + 100% |████████████████████████████████| 71kB 947kB/s |
| 88 | +Collecting smmap2>=2.0.0 (from gitdb2>=2.0.0->gitpython==2.1.7) |
| 89 | + Downloading smmap2-2.0.3-py2.py3-none-any.whl |
| 90 | +Installing collected packages: smmap2, gitdb2, gitpython |
| 91 | +Successfully installed gitdb2-2.0.3 gitpython-2.1.7 smmap2-2.0.3 |
| 92 | +``` |
| 93 | + |
| 94 | +Next we can start programmatically interacting with Git repositories in our |
| 95 | +Python applications with the GitPython installed. |
| 96 | + |
| 97 | + |
| 98 | +## Clone Repository |
| 99 | +GitPython can work with remote repositories but for simplicity in this |
| 100 | +tutorial we'll use a cloned repository on our local system. |
| 101 | + |
| 102 | +Clone a repository you want to work with to your local system. If you don't |
| 103 | +have a specific one in mind use the |
| 104 | +[open source Full Stack Python Git repository](https://github.com/mattmakai/fullstackpython.com) |
| 105 | +that is hosted on GitHub. |
| 106 | + |
| 107 | +```bash |
| 108 | +git clone git@github.com:mattmakai/fullstackpython.com fsp |
| 109 | +``` |
| 110 | + |
| 111 | +Take note of the location where you cloned the repository because we need |
| 112 | +the path to tell GitPython what repository to handle. Change into the |
| 113 | +directory for the new Git repository with `cd` then run the `pwd` (present |
| 114 | +working directory) command to get the full path. |
| 115 | + |
| 116 | +```bash |
| 117 | +cd fsp |
| 118 | +pwd |
| 119 | +``` |
| 120 | + |
| 121 | +You will see some output like `/Users/matt/devel/py/fsp`. This path is your |
| 122 | +absolute path to the base of the Git repository. |
| 123 | + |
| 124 | +Use the `export` command to set an environment variable for the absolute path |
| 125 | +to the Git repository. |
| 126 | + |
| 127 | +```bash |
| 128 | +export GIT_REPO_PATH='/Users/matt/devel/py/fsp' # make sure this your own path |
| 129 | +``` |
| 130 | + |
| 131 | +Our Git repository and path environment variable are all set so let's write |
| 132 | +the Python code that uses GitPython. |
| 133 | + |
| 134 | + |
| 135 | +## Read Repository and Commit Data |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +## What's next? |
| 140 | +We just cloned a [Git](/git.html) repository and used the GitPython |
| 141 | +library to read a slew of data about the repository and all of its commits. |
| 142 | + |
| 143 | +GitPython can do more than just read data though - it can also create and |
| 144 | +write to Git repositories! Take a look at the |
| 145 | +[modifying references](http://gitpython.readthedocs.io/en/stable/tutorial.html#modifying-references) |
| 146 | +documentation page in the official GitPython tutorial or check back here in |
| 147 | +the future when I get a chance to write up a more advanced GitPython |
| 148 | +walkthrough. |
| 149 | + |
| 150 | +Questions? Let me know via |
| 151 | +[a GitHub issue ticket on the Full Stack Python repository](https://github.com/mattmakai/fullstackpython.com/issues), |
| 152 | +on Twitter |
| 153 | +[@fullstackpython](https://twitter.com/fullstackpython) |
| 154 | +or [@mattmakai](https://twitter.com/mattmakai). |
| 155 | + |
| 156 | +See something wrong in this blog post? Fork |
| 157 | +[this page's source on GitHub](https://github.com/mattmakai/fullstackpython.com/blob/master/content/posts/171129-first-steps-gitpython.markdown) |
| 158 | +and submit a pull request. |
0 commit comments