Git: Getting Started
Git is a distributed version control system. Every working copy is a full repository with complete history — there's no central server required, though one is often used for collaboration. This covers the commands you'll use first: setting up a repo, configuring Git, and understanding the working tree.
Commands Covered
git init · git clone · git config · git status · git help · git version
git init
Creates a new empty Git repository in the current directory (or a specified path). Git stores everything in a .git/ subdirectory.
# Initialize a repo in the current directory $ git init # Initialize a named directory (creates it if it doesn't exist) $ git init my-project # Initialize a bare repository (no working tree — used for servers/remotes) $ git init --bare my-project.git
After git init, the directory contains a .git/ folder but no commits yet. The repository is empty until you add and commit files.
git clone
Copies an existing repository — full history included. The clone knows about the original (called origin) and can push/pull changes to it.
# Clone from GitHub (HTTPS) $ git clone https://github.com/user/repo.git # Clone into a specific directory name $ git clone https://github.com/user/repo.git my-local-name # Clone via SSH (requires SSH key set up with GitHub/GitLab) $ git clone git@github.com:user/repo.git # Shallow clone — only the latest commit (faster, less disk) $ git clone --depth 1 https://github.com/user/repo.git # Clone a specific branch $ git clone --branch develop https://github.com/user/repo.git # Clone a local repository $ git clone /path/to/existing/repo new-copy
git config
Gets and sets configuration values. Config is layered: system-wide, per-user (~/.gitconfig), and per-repo (.git/config). Per-repo overrides per-user, which overrides system.
# Set your identity (required before your first commit) $ git config --global user.name "Your Name" $ git config --global user.email "you@example.com" # Set your default editor (used for commit messages) $ git config --global core.editor vim $ git config --global core.editor "nano" $ git config --global core.editor "code --wait" # VS Code # Set default branch name for new repos $ git config --global init.defaultBranch main # Enable coloured output $ git config --global color.ui auto # Set up a credential helper (avoids re-entering password) $ git config --global credential.helper store # saves to disk (plain text) $ git config --global credential.helper cache # saves in memory for 15min $ git config --global credential.helper 'cache --timeout=3600' # View all current config values $ git config --list $ git config --list --show-origin # shows which file each setting comes from # Read a specific value $ git config user.email # Set a per-repo setting (omit --global) $ git config user.email "work@company.com" # Create an alias (shortcut for a command) $ git config --global alias.st status $ git config --global alias.co checkout $ git config --global alias.lg "log --oneline --graph --decorate" $ git st # now runs: git status
git status
Shows the state of the working tree and staging area — which files are modified, staged, or untracked. Run this constantly.
$ git status # Short format (compact output) $ git status -s # Output columns: XY filename # X = staging area status # Y = working tree status # M = modified, A = added, D = deleted, ? = untracked, ! = ignored $ git status --short --branch # include current branch in short format $ git status --ignored # also show ignored files
| Symbol | Meaning |
|---|---|
M (left) | Staged (modified in index vs last commit) |
M (right) | Unstaged (modified in working tree vs index) |
A | Newly staged file |
D | Deleted |
R | Renamed |
?? | Untracked file |
!! | Ignored file |
git help
# Open the man page for a command $ git help commit $ git help log # Short summary (one-liners for all commands) $ git help --all $ git help -a # same # List conceptual guides (not commands) $ git help --guides $ git help -g # Open a specific guide $ git help revisions # how to specify commits/ranges $ git help hooks # how hooks work $ git help attributes # .gitattributes format
git version
$ git version # git version 2.47.0 # Useful when troubleshooting — some features require specific versions. # Git 2.23+ introduced git switch and git restore as clearer alternatives # to the overloaded git checkout command.
The Three Areas
Understanding Git's three areas is fundamental to everything else:
| Area | What it is | Commands that affect it |
|---|---|---|
| Working tree | Files on disk as you edit them | Your editor, git restore |
| Staging area (index) | Snapshot queued for the next commit | git add, git rm, git restore --staged |
| Repository (.git/) | Committed history — permanent record | git commit, git reset |
dispelled