DS Git and GitHub

Git records every change to your files, and GitHub stores those records online. Data science projects change constantly: notebooks evolve, models get retrained, and code gets rewritten. Git lets you return to any earlier version, work with teammates without overwriting each other, and show your work to employers.

Think of a game that lets you save at many points. If a level goes wrong, you reload an earlier save. Git gives your project that safety.

Git versus GitHub

ToolRoleRuns Where
GitTracks versions of filesYour computer
GitHubHosts repositories online and supports teamworkThe web

The Workflow

 Working folder --git add--> Staging area --git commit--> Local history
                                                              |
                                                          git push
                                                              |
                                                              v
                                                    GitHub (online copy)
                                                              |
                                                          git pull
                                                              |
                                                       Teammates' computers

Set Up Git

git --version
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Install Git from its official site if the version command fails.

Start a Repository

mkdir house-price-project
cd house-price-project
git init

A repository, or repo, is a project folder that Git watches.

Save Changes with Commits

git status
git add notebooks/01_explore.ipynb README.md
git commit -m "Add exploration notebook and README"
git log --oneline

A commit is a labeled snapshot. Write messages that say what changed, such as Fix date parsing in cleaning step.

Commands You Will Use Daily

CommandPurpose
git statusShows changed and untracked files
git addStages files for the next commit
git commitSaves a snapshot with a message
git logLists past commits
git diffShows exact line changes
git restoreDiscards unsaved changes in a file

Ignore Files That Do Not Belong

Create a file named .gitignore to keep clutter and secrets out of the repository.

.ipynb_checkpoints/
__pycache__/
.env
data/raw/
*.pkl
.venv/

Large raw datasets and passwords should never enter a repo. Document where to download data in the README, and use specialized tools such as Git LFS or DVC for big files.

Branches: Safe Spaces for Experiments

 main     o----o----o--------------o----o
                     \            /
 feature              o----o----o
                  (try a new model here)
git switch -c try-gradient-boosting
git add .
git commit -m "Try gradient boosting model"
git switch main
git merge try-gradient-boosting

A branch holds an experiment apart from the main line. Merge it only when the results justify it, and delete the branch afterwards.

Connect to GitHub

git remote add origin https://github.com/yourname/house-price-project.git
git branch -M main
git push -u origin main

Create an empty repository on GitHub first, and copy its address. Use a personal access token or SSH key for sign-in, and keep it private.

Teamwork with Pull Requests

  • Create a branch for each task.
  • Push the branch to GitHub and open a pull request.
  • Teammates read the changes and leave comments.
  • Merge after approval.
  • Pull the latest main before starting new work.

Tips for Notebooks

  • Clear heavy outputs before committing, since images inflate file size.
  • Move reusable code into .py files, which compare cleanly.
  • Pin library versions in requirements.txt.
  • Commit small and often.

Undo Mistakes Safely

SituationCommand
Discard changes in one filegit restore filename
Remove a file from staginggit restore --staged filename
Fix the last commit messagegit commit --amend
Undo a pushed commit safelygit revert commit-id

Git keeps history, so most mistakes stay recoverable. Revert creates a new commit that cancels an old one, which keeps shared history intact for teammates.

Resolve Merge Conflicts

A conflict appears when two people change the same lines. Git marks both versions inside the file. Open the file, choose or combine the correct lines, delete the marker lines, save, and commit. Talk with the teammate when the choice is unclear.

Key Points

  • Git tracks versions locally, and GitHub shares them online.
  • Commits, branches, and pull requests organize experiments and teamwork.
  • A .gitignore file keeps data dumps and secrets out of the repository.