Moment 3: Version Control with Git and GitHub

Reading time: approx. 12 min

You now have a project with an isolated Python environment and a dependency file. But what happens when you start writing code and making changes? How do you keep track of what worked? And how do you easily share your project with a colleague, a student, or the whole world? The answer is Git and GitHub.

In this moment we take the project we created and learn the most basic but powerful workflow in software development: tracking local changes with Git and then "pushing" them to a remote repository on GitHub. GitHub is a web platform that functions as a social hub and a cloud-based backup for your Git projects.


What You Will Learn

After this moment you will be able to:

  • Initiate a new Git repository in an existing project.
  • Understand and use the basic Git flow: add, commit.
  • Create a new, empty repository on GitHub.
  • Connect your local repository with the one on GitHub.
  • "Push" your changes to GitHub.

The Basics: Local vs. Remote

It's important to understand the difference between Git and GitHub.

  • Git is the program on your computer that tracks changes. It is local. Your project with all its history lives in a hidden folder called .git inside your project folder.
  • GitHub is a web service where you can store copies of your Git repositories. It is remote. This gives you a backup and makes it possible for others to see and collaborate on your code.

The workflow is almost always: Work locally, save changes with Git, send up to GitHub.


Practical Examples: From Local Change to GitHub

We continue with the project we created in moment 2, my_project_2.

Step 1: Create a GitHub account If you don't already have one, go to github.com and create a free account.

Step 2: Initialize a local Git repository Navigate to your project folder in the terminal.

cd /path/to/my_project_2

Now we tell Git to start tracking this folder.

git init

Git will respond with something like "Initialized empty Git repository in...". You have now created the local .git folder.

Step 3: Make your first "commit" (saved snapshot) A commit is a saved snapshot of your project at a specific point in time. The process has two steps:

  1. git add: Choose which files you want to include in your snapshot. We want to include our requirements.txt file.
    git add requirements.txt
    
  2. git commit: Create the actual snapshot with a descriptive comment (-m stands for message).
    git commit -m "First commit: added requirements"
    

Step 4: Create a new repository on GitHub

  1. Go to your profile on GitHub and click "New repository".
  2. Give the repository a name, e.g. my-first-project.
  3. Keep it "Public".
  4. IMPORTANT: Do not check any of the boxes for "Add a README file", "Add .gitignore" or "Choose a license". We want a completely empty repository because we already have a local one.
  5. Click "Create repository".

Step 5: Connect local and remote GitHub will now show you a page with some commands. We are interested in the second box: "...or push an existing repository from the command line". Copy the two command lines and paste them into your terminal. They will look something like this (with your username):

git remote add origin https://github.com/your-username/my-first-project.git
git push -u origin main
  • git remote add origin ...: This command tells your local Git repository that there is a "remote location" with the nickname origin at the specified GitHub address.
  • git push -u origin main: This command "pushes" your main branch (the default branch) to origin.

GitHub may ask you to log in. Approve this.

Done! Go back to your project page on GitHub and refresh the browser. You will now see your requirements.txt file there!


Exercise: Make a Change and Synchronize

Now you will see the entire cycle.

  1. Make a change locally: Open the project in VS Code. Create a new file called main.py. Write a simple Python line in it, e.g. print("Hello GitHub!"), and save.
  2. Check status: Go to the terminal and type git status. Git will tell you that there is a new, untracked file (main.py).
  3. Add and commit: Run the same process as before to save your new change.
    git add main.py
    git commit -m "Added main.py with a hello message"
    
  4. Push the change: Now that you have made a new local commit you need to send it up to GitHub.
    git push
    
    (Since we have already connected origin and main, git push alone is now enough).

Go to GitHub again and refresh the page. Your new main.py file is now there. You have mastered the basic workflow!


Next Steps

Fantastic! You can now create projects, track changes and back up your code in the cloud. This is an incredibly important foundation. In the next moment, Your Professional Code Editor: Visual Studio Code, we will get to know the tool where you will spend most of your time and learn how to work efficiently with it.