Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Git

    Your First Commits

    A repository (repo) is a project folder that Git tracks. Git keeps all its history in a hidden folder

    Chapter 2 of 115 min read

    The Plain Picture#

    Taking a group photo isn't instant. First you arrange who's in the shot ("you, you, and you — step in"), then you press the button. Some people in the room might not be in this photo.

    The Real Names#

    • A repository (repo) is a project folder that Git tracks. Git keeps all its history in a hidden folder called .git inside it.

    • The working directory (working tree) is the normal files you see and edit.

    • The staging area (also called the index) is the "arranging people for the photo" step: the exact set of changes that will go into the next commit.

    • A commit is the photo: a saved snapshot of the staged files, with a message, author and time.

       WORKING DIRECTORY              STAGING AREA                  REPOSITORY (.git)
       (files you edit)               (what the next commit         (all saved commits)
                                       will contain)
            │                               │                              │
            │ ──────── git add ───────────▶ │                              │
            │                               │ ──────── git commit ───────▶ │
            │ ◀─────────────────────── git restore / git switch ────────── │

    Under the Hood — a real first session#

    First, tell Git who you are (once per computer):

    Terminal
    git config --global user.name  "Priya"
    git config --global user.email "priya@example.com"

    Then:

    Terminal
    mkdir ~/git-practice && cd ~/git-practice
    git init -b main                 # create a repo; the first branch is called "main" (Ch 4)

    Create quiz.py:

    Python
    def score(correct):
        return correct * 10
    Terminal
    git status --short               # →  ?? quiz.py        (?? = untracked: Git sees it but isn't tracking it)
    git add quiz.py                  # stage it
    git status --short               # →  A  quiz.py        (A = added to staging)
    git commit -m "Add quiz scoring"
    git log --oneline                # →  7ef9500 Add quiz scoring

    7ef9500 is the start of the commit's ID (Ch 3).

    Why a staging area? It lets you commit part of your work. If you fixed a bug and started a new feature in the same afternoon, you can git add only the bug-fix files and commit them alone, with a clear message — then commit the feature separately. git add -p goes further: it lets you pick individual chunks of changes inside one file.

    Status codes you'll see in git status --short:

    CodeMeaning
    ??untracked — new file Git has never been told about
    Aadded (staged, new file)
    M (left column)modified and staged
    M (right column)modified but not staged
    Ddeleted
    UUunmerged — a conflict to resolve (Ch 5)

    Good commit messages: a short summary line in the imperative mood ("Add bonus points", not "added stuff"), under about 50 characters, then optionally a blank line and a longer explanation of why. Future-you, reading git log at 11 p.m. during an incident, will be grateful.

    Gotchas

    • git commit -am "msg" stages and commits all changes to already-tracked files in one go — but it does not add new untracked files.

    • Commit small and often. Small commits are easy to review, easy to revert and easy to search (Ch 11).

    Check yourself

    4 questions
    1

    Name the three areas a change moves through.

    Show answer

    Working directory → staging area → repository.

    2

    What does git add do? git commit?

    Show answer

    add puts changes into the staging area; commit saves the staged snapshot into history.

    3

    Why is the staging area useful?

    Show answer

    It lets you choose exactly which changes go into each commit, so unrelated work can be committed separately.

    4

    What does ?? mean in git status --short?

    Show answer

    The file is untracked — Git sees it but has never been told to track it.