Python for AIPython for AI
Tools

Clone and create

Working with repositories

Let's explore how to get existing projects and start your own, all while using a version control system.

Two things you'll do often

When working with code, you'll often do two main things. You'll bring code from somewhere else onto your computer. Or, you'll start a brand new project and begin building it.

Both of these actions are fundamental. They get you set up to write, test, and share your work.

Cloning a repository

To get a copy of someone else's project, you 'clone' their repository. This creates a full local copy, including all its history and branches.

You'll need the repository's URL. This is usually found on the project's page where it's hosted online.

Example: Clone an AI project

Let's say you found an interesting AI project called simple-ml online. You want to try it out.

bash
git clone https://github.com/someuser/simple-ml.git

This command downloads the entire simple-ml project into a new folder on your computer.

What just happened?

The git clone command did a few things:

  • It created a new directory named simple-ml in your current location.
  • It copied all the project's files into that directory.
  • It set up important configuration files inside the hidden .git directory, linking your local copy to the original project.

Now you have a local working copy of the project. You can explore its files and run its code.

Creating your own repository

When you start a new project from scratch, you'll want to create your own repository. This helps you track changes and collaborate.

The process generally involves initializing a local repository and then connecting it to a remote location.

Step 0: Create .gitignore file

Before you create your repository, make a .gitignore file. This tells your version control system which files to ignore, like temporary files or sensitive information.

bash
touch .gitignore

Add common ignore patterns. For Python, this includes compiled bytecode, virtual environments, and data files.

text
# Python bytecode
__pycache__/
*.pyc

# Virtual environment
venv/
.venv/

# Data and logs
*.log
*.csv
*.json

Step 1: Create on GitHub

You'll often create an empty repository on a platform like GitHub first. This gives you a remote place to push your code to.

Follow the platform's steps to create a *new, empty* repository. Do not initialize it with a README or .gitignore yet, as we'll do that locally.

Remember to copy the repository's HTTPS or SSH URL. You'll need it soon.

Step 2: Connect your local project

Now, let's create a local project and link it to the remote repository you just made.

bash
mkdir my_new_project
cd my_new_project
git init
echo "print('Hello, world!')" > main.py
git add .
git commit -m "Initial project setup"
git branch -M main
git remote add origin <YOUR_REPOSITORY_URL_HERE>
git push -u origin main

This sequence of commands creates a new folder, initializes a local repository, adds some files, commits them, sets up the remote connection, and pushes your code online.

The daily workflow

Once set up, your daily work flow usually involves three steps: make changes, stage them, and commit them.

  • git add <file>: Stage specific changes or git add . for all changes.
  • git commit -m "Your message here": Save your staged changes with a descriptive message.
  • git push: Send your local commits to the remote repository.

You'll also frequently git pull to fetch and apply changes made by others or on another computer.

Common tasks

Here are a few common commands you'll use often:

  • git status: See what files have been changed or are untracked.
  • git log: View the history of commits.
  • git diff: Show differences between your working directory and the last commit, or between staged changes.
There's a lot more to learn about version control, but these commands cover the basics of day-to-day usage.

Practice exercise

Create a new project named my_calculator.

  • Initialize a new local repository.
  • Create a .gitignore file that ignores __pycache__/.
  • Add a calculator.py file with functions for addition and subtraction.
  • Commit your changes with a meaningful message.
  • Push your project to a new remote repository you create online.

What's next?

Now that you can get projects and start your own, you're ready to explore more advanced version control features. Next, you might want to learn about branches and merging.

These concepts are crucial for collaborative development and managing different lines of work in your projects.