First steps: repository, staging and commits

First lesson of the Git and GitHub course, free to read: about 20 minutes, 3 exercises and 3 quiz questions. This is the catalogue's base lesson; in the student space, it is rewritten for your profile.

Objectives

  • Understand what a version control system is for
  • Configure Git and initialize a repository
  • Record changes with add and commit

Git repositoryStaging areaCommit

Who has never had a folder full of rapport_v2.docx, rapport_final.docx, rapport_final_VRAI.docx? Git solves this problem: it is a version control system that records the complete history of a project. You can go back in time, understand who changed what and why, and work with others without overwriting their work. Git is now the standard tool, from showcase websites to data science projects.

Configure Git (just once)

git --version                                   # checks the installation
git config --global user.name "Léa Martin"
git config --global user.email "lea@example.com"
git config --global init.defaultBranch main     # name of the initial branch

The name and email are written into every commit: they identify the author.

Create a repository

mkdir mon-projet && cd mon-projet
git init            # creates the repository: a hidden .git folder containing the whole history
git status          # the command to type without moderation

Never delete the .git folder: it is what holds the history.

The three areas

This is the central concept in Git:

  1. the working directory: your files as you edit them;
  2. the staging area (or index): what you have chosen to include in the next snapshot;
  3. the history: the sequence of recorded commits.
 modified files --git add--> staging area --git commit--> history

A commit is a snapshot of the project at a given moment, along with a message, an author, a date and a unique identifier (a hash such as a1b2c3d).

The basic cycle

echo "# My project" > README.md
git status                 # README.md appears under "Untracked files"
git add README.md          # we place it in the staging area
git status                 # it appears under "Changes to be committed"
git commit -m "Add the README"   # we record the snapshot

Then, with each change:

# ... you edit index.html and style.css ...
git status                 # "modified" files
git add index.html style.css   # or git add . for the whole current directory
git commit -m "Add the home page and its styling"

Why an intermediate step? Because it lets you choose: if you have fixed a bug and started a new feature, you can record them in two separate commits. git add -p even lets you select pieces of a file.

Good commits

  • Small and coherent: one commit = one logical change.
  • Well named: a short message in the imperative that says what the commit does (“Fix the VAT calculation”), not “changes” or “wip”.
  • Frequent: committing often means multiplying the possible points to return to.

Get into the habit of running git status before every add and every commit: Git always tells you where you stand and often suggests the next command.

Exercises

Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.

  1. TaskDifficulty 11 hint

    Configure Git and create a repository

    Configure your name and email, then create a folder atelier-git, initialize a repository in it and check its state.

    • git config --global user.name displays your name
    • git config --global user.email displays your email
    • The atelier-git folder contains a hidden .git subfolder (visible with ls -a)
    • git status reports "No commits yet"
  2. TaskDifficulty 22 hints

    Two separate commits

    In atelier-git, create README.md and notes.txt. Make a first commit containing only the README, then a second one containing notes.txt.

    • Before the first commit, git status shows README.md under "Changes to be committed" and notes.txt under "Untracked files"
    • git log --oneline displays exactly 2 commits
    • git show --stat HEAD~1 mentions only README.md
    • git status reports "nothing to commit, working tree clean"
  3. Multiple choiceDifficulty 11 hint

    The role of git add

    You modified app.py, then ran git commit -m "Corrige" without git add. What happens?

    • The commit contains app.py
    • Git reports that nothing is staged for commit
    • Git deletes the changes

End-of-lesson quiz

  1. What does git init do?

    • Sends the project to GitHub
    • Creates a Git repository in the current directory
    • Records a commit
  2. In what order do you record a change?

    • git commit then git add
    • git add then git commit
    • git push then git commit
  3. Which commit message is best?

    • modifs
    • Corrige le calcul du total du panier
    • commit du 10/11

The rest of the course

  1. Explore the history and undo changes15 min
  2. Branches, merges and conflicts20 min
  3. GitHub: working with a remote repository20 min
  4. Collaborating: .gitignore, pull requests and review20 min

Git and GitHub · Beginner · 3 h 5 min