The Basics of Git for Python Code

Keep your code under source control by leveraging the most popular version control tool out there: Git

You may have heard of GitHub - a place that is home to well over 420 million repositories of code. In order to use GitHub to host, share, and collaborate on code, you need to understand how to use Git.

Git is a tool that allows developers to track changes of their code over time. You’re able to take “snapshots” of your code, work on different features/bugs (without having to rename each file something like script_v1_final.py), and collaborate with your peers.

Here, I am going to show you the basics of using Git and how you can integrate it with your Python code.

Note: If you’re not comfortable using terminal, there is a user interface you can use. However, this tutorial will not be using this interface.

Key Terminology

More often times than not, users who are new to Git often get lost in the terminology. Here’s a listing of terminology that is used here:

  • Repository - A folder containing your project’s code.

  • Commit - Create a “snapshot” of the code in its current state.

Getting Started with Git

First, you need to have Git installed. The official Git installation instructions can be found on the Git website for Windows, Mac, and Linux.

Once you have it downloaded, setup your name and email using the command line:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

This will set your name and email for all of your commits in every repository.

Note: You can also set individual repositories by navigating into the repository and omitting --global flag.

Making Changes using Git

From here, you’ll create a project. Create a directory where you want all of your files to go and navigate into it:

mkdir my-project
cd my-project

Then, start your project by initializing git (this creates a .git folder in this folder):

git init

Now, create a few Python files and write some code. When you get to a point to where you want to “save your progress”, you’re going to want to check to see which file(s) you’ve edited by running the git status command:

git status

This will provide a listing of all of your files that have changed since the last “saved progress”.

Then, stage your file(s) by using the add command:

git add . # the period adds all the files

# OR add individual file(s) (based off of `git status`)

# assuming app.py is the name of the file in your repository.
git add app.py

Then, make a commit (“snapshot”) explaining the changes. This comment can be whatever you wish.

git commit -m "Initial commit with some initial code"

Note: the -m flag is important, as this allows you to make your descriptive message in-line. Without this, it’ll open an editor.

Note: The quotation marks are also important - these must be double quotes.

At this point, you’ve essentially “saved your progress” in your code base. More advanced tutorials tell you to push your commits. While it’s not covered here, pushing is when you take the commits on your machine and put them into GitHub.

Happy coding!

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

or to participate.