git Distributed version control system, created by Linus Torvalds

Basic Workflow

This is the loop you’ll run dozens of times a day: check what changed, stage it, commit it, inspect history.

Starting a repository

git init                                   # new, empty repository here
git clone <url>                            # copy an existing repository
git clone <url> my-dir                     # ...into a specific directory
git clone --depth 1 <url>                  # shallow clone: history from HEAD only

git status

The single most-run Git command. Shows the current branch, files staged for commit, files modified but not staged, and untracked files:

git status
git status -s      # short format: one line per file

Short-format prefixes: M modified, A added, D deleted, R renamed, ?? untracked, and a staged change shows in the first column vs. an unstaged one in the second (e.g. M  file is staged,  M file is not).

Staging changes

git add file.txt              # stage one file
git add dir/                  # stage a directory
git add .                     # stage everything in and below the cwd
git add -A                    # stage everything in the whole repo
git add -p                    # interactively stage hunks within files

Committing

git commit -m "Short summary of the change"
git commit                    # opens $EDITOR for a longer message
git commit -am "message"      # stage every tracked, modified file, then commit
git commit --amend            # rewrite the most recent commit

A well-formed commit message is a short (~50 char) summary line, a blank line, then optional detail wrapped at ~72 characters explaining why, not just what.

--amend rewrites history — only use it on commits you haven’t pushed yet, or that you’re certain nobody else has already pulled.

Inspecting changes

git diff                      # working directory vs. staging area
git diff --staged             # staging area vs. last commit (a.k.a. --cached)
git diff HEAD                 # working directory vs. last commit
git diff main..feature        # compare two branches
git diff -- path/to/file       # limit to one path

Viewing history

git log                                   # full history, newest first
git log --oneline                         # one line per commit
git log --oneline --graph --all           # ASCII graph of every branch
git log -p                                # show the diff for each commit
git log -n 5                              # last 5 commits
git log --author="Name"                   # filter by author
git log --since="2 weeks ago"             # filter by date
git log -- path/to/file                   # history of one file
git show <sha>                            # full details of one commit

Removing and renaming

git rm file.txt                # delete a file and stage the deletion
git rm --cached file.txt       # stop tracking a file, keep it on disk
git mv old.txt new.txt         # rename/move, staged automatically

Ignoring files

Create a .gitignore file listing patterns Git should never track or prompt you about — build output, dependencies, secrets:

# .gitignore
.env
node_modules/
build/
*.log
.dart_tool/

Already-tracked files aren’t affected retroactively by .gitignore; untrack them first with git rm --cached.

Next: Branching & Merging to work on more than one line of development at once.