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.
git clone https://github.com/someuser/simple-ml.gitThis 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-mlin your current location. - It copied all the project's files into that directory.
- It set up important configuration files inside the hidden
.gitdirectory, 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.
touch .gitignoreAdd common ignore patterns. For Python, this includes compiled bytecode, virtual environments, and data files.
# Python bytecode
__pycache__/
*.pyc
# Virtual environment
venv/
.venv/
# Data and logs
*.log
*.csv
*.jsonStep 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.
Step 2: Connect your local project
Now, let's create a local project and link it to the remote repository you just made.
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 mainThis 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 orgit 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.
Practice exercise
Create a new project named my_calculator.
- Initialize a new local repository.
- Create a
.gitignorefile that ignores__pycache__/. - Add a
calculator.pyfile 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.