Git: the history of your project

Git remembers versions of your code, lets you go back to a working state and work together. GitHub is a website where copies of such projects are kept.

An old account card: everything that happened with a client is written down by date

The first commit

A commit is a saved snapshot of the project with a clear description.

Terminal: once per computer
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
Terminal: in the project folder
git init
git status
git add app.py
git commit -m "Add the home page"
git log --oneline

The three states of a file

  1. Modified: you changed the file, but Git has not saved it yet.
  2. Staged: the git add command added it to the next commit.
  3. Committed: the git commit command wrote the snapshot into the history.

What not to keep in Git

The .gitignore file lists what Git should not track.

.gitignore
.venv/
__pycache__/
*.db
db.sqlite3
.env

If a password or key has already reached a commit on GitHub, deleting the file is not enough: it stays in the history. Treat the key as exposed and replace it with a new one.

Branches

A branch lets you build a new feature separately without breaking the working version.

Terminal
git switch -c search-form
git add .
git commit -m "Add a search form"
git switch main
git merge search-form
git branch -d search-form

If there is a conflict

A conflict means two branches changed the same line. Open the file, keep the version you need, remove the markers <<<<<<<, ======= and >>>>>>>, then run git add and git commit.

Working with GitHub

First create an empty repository on github.com.

Terminal: the first push
git remote add origin https://github.com/USER/REPO.git
git push -u origin main
Terminal: an ordinary day
git pull
git add .
git commit -m "Fix the login form"
git push

How to undo changes

Before undoing anything, look at the current state with git status.

What to do in different situations
SituationCommandImportant
Unstage a filegit restore --staged app.pyThe edits in the file stay
Discard edits in a filegit restore app.pyUnsaved edits are lost for good
Undo a commit that is already pushedgit revert abc1234Creates a new commit, the history is not broken
Fix the message of the last commitgit commit --amendOnly if the commit has not been pushed yet

The commands git reset --hard and git push --force can delete work for good. A beginner is better off without them.

A good commit message

Describe what changed and why. In a month you will read the history like the project's diary.

Clear

  • Add a check for an empty task
  • Fix login with a wrong password

Unclear

  • edits
  • fix
  • again

Next: deployment

Your code is on GitHub. All that is left is to put the site online.