GitHub Remote Backups & .gitignore Security
Once you have saved commits locally on your computer, you should back them up online to GitHub and configure rules to keep private keys safe.
1. Pushing Local Code to GitHub
Step 1: Create an empty repo on GitHub
- Go to github.com and click New Repository.
- Give it a name (e.g.
python-ai-project). - Leave README and .gitignore unchecked and click Create Repository.
Step 2: Link and Push from Terminal
# 1. Rename your default branch to main:
git branch -M main
# 2. Link your local project to your GitHub URL:
git remote add origin https://github.com/YOUR_USERNAME/python-ai-project.git
# 3. Push your commits online (first time only uses -u):
git push -u origin main
(On subsequent commits, simply typing git push is enough!)
2. Protecting Secrets with .gitignore
In modern AI engineering, you work with sensitive API keys (e.g., OpenAI, Gemini, database passwords). You must never publish these secrets to GitHub.
To prevent Git from tracking unwanted files, create a file named exactly .gitignore in your project root:
# Sensitive credentials & API keys
.env
*.pem
# Heavy virtual environment folder (megabytes of downloads)
.venv/
myenv/
# Python compiled bytecode cache
__pycache__/
*.pyc
# OS temporary metadata
.DS_Store
Thumbs.db
Golden Rule
Always create and save your .gitignore file before running git add .. If you commit a secret file once, it remains permanently stored in Git's historical logs!
Quick Summary
- Remote Repositories:
git remote add origin <URL>links local Git repos to cloud hosts like GitHub. - Pushing Code:
git push -u origin mainuploads commits to GitHub for backup and team sharing. .gitignoreRules: Exclude.venv/,.env(API keys/passwords), and temporary OS metadata (__pycache__/,.DS_Store) before staging files.
What's Next?
Let's explore how teams collaborate simultaneously without overwriting each other's work using Branches & Merging in the next lesson!