Lesson 2 · Python for Data Science
2 - Git and GitHub Basics for Python Projects
Git helps you save your Python code changes. GitHub lets you share and back up your code online. Let us start exploring how to use them in your coding…
- CoursePython for Data Science
- Lesson2 of 38
- Video15 min
- FormatJupyter notebook · 29 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynb
Welcome to Git and GitHub Basics for Python Projects#
Git helps you save your Python code changes. GitHub lets you share and back up your code online.
Let us start exploring how to use them in your coding journey!
What is Version Control?#
Version control means saving your project in steps. You can go back if you make a mistake. Git is the most popular version control tool.
# Let us check if Git is installed on your system
import os
result = os.system('git --version')
print('If you see a version above, Git is installed!')
Getting Ready for Your First Git Project#
Before you use Git, you need a folder with some code. You can use any Python file you have or create a new one.
Let us create and use a demo folder in the next step.
# Let us create a folder for your Git project
import os
folder_name = "my_first_git_project"
if not os.path.exists(folder_name):
os.mkdir(folder_name)
print(f'Folder "{folder_name}" is ready!')
# Make a simple Python file in your new folder
file_path = os.path.join(folder_name, "greeting.py")
with open(file_path, "w") as f:
f.write("print(\"Hello, Git!\")\n")
print(f'File created at {file_path}')
Initializing a Git Repository#
A repository is like a special folder where Git watches your changes. We start by telling Git to track our folder.
Let us do this step now.
# Initialize Git in your project folder
init_result = os.system(f'cd {folder_name} && git init')
print('Your folder is now a Git repository!')
# Check the status of your new repository
os.system(f'cd {folder_name} && git status')
Staging and Committing Changes#
Git tracks changes in two steps:
- Stage your changes (pick what you want to save)
- Commit your changes with a message
Let us try both steps next.
# Stage your new file with git add
os.system(f'cd {folder_name} && git add greeting.py')
print('File is staged and ready to commit.')
# Commit your staged file with a message
os.system(f'cd {folder_name} && git commit -m "Add greeting.py"')
print('First commit is saved. Congratulations!')
# Make a small edit and see Git notice the change
with open(file_path, "a") as f:
f.write("print(\"Update: Welcome to Git!\")\n")
print('We changed greeting.py. Check git status to see!')
# What files has Git noticed as changed?
os.system(f'cd {folder_name} && git status')
# See your project's commit history
os.system(f'cd {folder_name} && git log --oneline')
Undoing Changes Safely#
Sometimes you make a mistake. Git lets you undo changes or reset to a safe point.
Let us explore how to fix mistakes next.
# Discard your last edit in greeting.py
os.system(f'cd {folder_name} && git checkout -- greeting.py')
print('Your file is reset to the last commit!')
# Create a .gitignore to skip files you do not want to track
gitignore_path = os.path.join(folder_name, ".gitignore")
with open(gitignore_path, "w") as f:
f.write("*.log\n__pycache__/\n")
os.system(f'cd {folder_name} && git add .gitignore && git commit -m "Add .gitignore"')
print('.gitignore is now set up!')
Sharing Code with GitHub#
GitHub is a website for storing and sharing your Git repositories. You can work with others and access your code anywhere.
We will discuss how to set up a remote next.
# Add a remote GitHub repository
github_url = input('Paste your GitHub repo URL here: ')
os.system(f'cd {folder_name} && git remote add origin {github_url}')
print('Remote added! Now your code can reach GitHub.')
# Push your code to GitHub
os.system(f'cd {folder_name} && git branch -M main && git push -u origin main')
print('Code sent to GitHub!')
# Clone a GitHub repo to your computer
clone_url = input('Enter a repo URL to clone: ')
os.system(f'git clone {clone_url}')
print('Repository cloned!')
# See a summary of recent changes from GitHub
os.system(f'cd {folder_name} && git pull')
# Who made which changes? Run git log with author
os.system(f'cd {folder_name} && git log --oneline --author=')
# Mini-project: Make a changelog.txt and save updates over time
changelog_path = os.path.join(folder_name, "changelog.txt")
with open(changelog_path, "a") as log:
update = input('Describe your latest project change (e.g., Fixed a bug): ')
log.write(update + "\n")
os.system(f'cd {folder_name} && git add changelog.txt && git commit -m "Update changelog.txt"')
print('Changelog updated and saved to GitHub!')
# Mini-project part 2: Branches for experiments
os.system(f'cd {folder_name} && git checkout -b experimental-feature')
with open(file_path, "a") as f:
f.write("print(\"Experimental feature!\")\n")
os.system(f'cd {folder_name} && git add greeting.py && git commit -m "Try experimental feature"')
print('Experimental feature added on a new branch!')
# Merge your branch changes back into main
os.system(f'cd {folder_name} && git checkout main && git merge experimental-feature')
print('Experimental feature merged to main!')
# See a simple commit graph with git log --graph
os.system(f'cd {folder_name} && git log --oneline --graph')
# Clean up: Remove a file and save that change
os.remove(file_path)
os.system(f'cd {folder_name} && git rm greeting.py && git commit -m "Remove greeting.py"')
print('greeting.py removed from your project and version history!')
# Top 3 mistakes beginners make with Git
mistakes = [
'Forgetting to add and commit often',
'Not writing clear commit messages',
'Losing track of which branch you are on'
]
for m in mistakes:
print('- ' + m)
# Git tip: Use git diff to see exactly what you changed
os.system(f'cd {folder_name} && git diff')
# Challenge: Make a project with at least 3 commits
print('Challenge: Make a Python repo, add a file, commit, update it, and commit again.')
Recap: What did you learn?#
- How to use Git for saving your work
- How to use GitHub to share with others
- Working with commits, branches, and remotes
- Tips for fixing mistakes and working together
Remember, regular practice is the best way to get comfortable!
Thank you for learning Git and GitHub!#
If this video helped you, please give it a thumbs up. Comment with your project ideas below. Subscribe for more beginner tutorials. Share this lesson with your friends!
Happy coding!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



