How To Use Git Hub To Manage A Thonny project

This post will describe how to use Git Hub to manage a Thonny project. If you are developing a complex python program in Thonny you should definitely use a source code repository to manage the project. While I am using Thonny for developing RPI code, these instructions will work for any type of Thonny project.

Assumptions for this post:

  1. Using Thonny on Windows as the IDE for python on RPI development. (see this post for setting up Thonny).
  2. Using Git Hub as the online repository.
  3. The target environment for the python code will be a Raspberry Pi. (see this post for setting up an RPI Pico)

Create Repository In GitHub

If you don’t already have an account on Git Hub, go ahead and create one. Git Hub is free and widely used for source code management.

Once you have an account on GitHub, you can create the repository for your project.

  1. Go to: https://github.com/new
  2. Fill in:
    • Owner: Your real name or your github username
    • Repository name: the name for your repository
    • Description: A brief description of the repository
    • Public (recommended)
    • DO NOT check “Add a README”, “.gitignore”, or “license” as we’ll do it properly from Thonny
  3. Click Create repository

Copy the URL for your repository. It should look something like this: https://github.com/your-username/your-repo-name.git

Install Git for Windows

To enter Git commands in the Thonny shell you need to install Git for Windows. You enter a “!” followed by the Git command.

  1. Go to: https://git-scm.com/download/win
  2. Download & run the installer (just keep clicking NextNextInstall)
  3. When it asks “Adjusting your PATH environment”, choose: “Git from the command line and also from 3rd-party software” (important!)
  4. After install finishes → restart Thonny completely
  5. Test in Thonny shell:
!git --version

The response should say: git version 2.44.x.windows.1 or something similar.

Now all !git commands will work!

Steps To Initialize the Git Repository

This assumes that you have already created some code and you now want to manage the code using Git. Your code exists under a folder referred to as “project-folder” below.

Run the following commands in Thonny’s shell.

Step # 1. Open your project folder in Thonny

!cd C:\Users\xxx\project-folder

Step # 2. Tell Git who you are (only once per computer)

!git config --global user.name "Your name here"
!git config --global user.email "Your email here"

Step #3. Create the .gitignore file (do this in Thonny editor first!)

#    → File → New → Paste the text below → Save As → name it exactly: .gitignore
#    → Make sure you save it in your project root folder!

Copy the following into the .gitignore file. This will block a number of files from being managed by the repository.

# Secrets & credentials
.pypirc
.env

# Python
__pycache__/
*.pyc
*.pyo
*.pyd
*.egg-info/

# Build
build/
dist/

# Thonny & IDE
.thonny/
.vscode/
.idea/

# OS
Thumbs.db
.DS_Store

Step # 4. Initialize Git repository

!git init

Step # 5. Add all files (including your new .gitignore)

!git add .

Step # 6. Make the first clean commit

!git commit -m "Initial commit: your-repo-name v0.1.0"

Step # 7. Rename default branch from “master” to “main” (do this immediately)

!git branch -M main

Step # 8. Link to your GitHub repository (use YOUR correct URL!)

!git remote add origin https://github.com/userid/repo-name.git

Step # 9. Push code + set up tracking

!git push -u origin main

Step # 10. Create and push version tag (for PyPI & GitHub Releases)

!git tag v0.1.0
!git push origin v0.1.0

Step # 11. (Optional) Confirm everything is perfect

!git log --oneline -5

Finally…

This post described how to use Git Hub to manage a Thonny project. You now have a local Git repo and another repo on Git Hub to manage and share your code with other developers. Don’t forget to check out the other posts related to developing python code for Raspberry Pi projects.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts