Git cheat sheet: everyday commands

This sheet is the loop you run every day. Git's model has three places a file can live: the working tree (what you edit), the index (staged, ready to commit) and the HEAD (the latest commit). git add copies changes from the working tree to the index; git commit records the index as a new commit and moves HEAD to it.

The commands below are the only ones most developers need for day-to-day work, and they behave identically on Windows, macOS and Linux.

Published

The daily loop

A typical session, end to end
# 1. See what changed
git status

# 2. Stage the files you want in this commit
git add src/app.js README.md
#    or stage everything in the current directory and below:
git add .

# 3. Commit the staged changes
git commit -m "Fix login redirect"

# 4. Share it
git push
The commands you will type most
CommandWhat it does
git statusshows staged, unstaged and untracked files, plus the current branch
git add <file>stages changes; git add . stages everything in and under the current directory
git commit -m "msg"records the staged changes as a commit with the given message
git pushuploads local commits to the remote (origin) tracking branch
git pulldownloads remote commits and merges them into the current branch (= fetch + merge)
git fetchdownloads remote commits and branches without touching your working tree
git log --onelineone commit per line; add --graph --all for a branch map
git diffunstaged changes only; git diff --staged shows the staged changes

First-time setup and remotes

Configure your identity (needed before the first commit)

git config --global user.name "Ada Lovelace"
sets your name for every repo on this machine
git config --global user.email "ada@example.com"
sets the email attached to your commits
git config --global init.defaultBranch main
names the default branch for new repos (main instead of master)
git config --global core.editor "code --wait"
uses VS Code as the editor for commit messages and rebases

Starting and linking a repository

git init
creates a new repository in the current directory
git clone <url>
downloads a repository and its full history into a new folder
git remote add origin <url>
links the local repo to a remote called origin
git remote -v
shows the fetch and push URLs for every remote
git push -u origin main
first push: uploads main and sets upstream tracking so plain git push works afterwards

Inspecting history

The log views that answer 'what happened?'
# Compact history with a branch graph
git log --oneline --graph --all --decorate

# Files changed in the last commit
git show --stat HEAD

# Who changed each line of a file
git blame src/app.js

# Show one commit in full
git show a1b2c3d

# Search commit messages
git log --grep="bugfix" --oneline
Commit hashes are full SHA-1 IDs, but Git accepts any unambiguous prefix — git show a1b2c3d is shorthand for the full hash. HEAD means the current commit, HEAD~1 its parent, HEAD~2 its grandparent, and so on. A short name like origin/main means the local copy of the main branch on the origin remote.

Ignoring files

A typical .gitignore (place it at the repository root)
# Dependencies and build output
node_modules/
dist/
*.class

# Environment and secrets
.env
.env.*

# Editor and OS junk
.DS_Store
Thumbs.db
*.log
Ignore rules only apply to untracked files. A file that is already committed stays tracked even after you add it to .gitignore — remove it from the index first with git rm --cached <file> (which keeps the working-tree copy). Patterns like *.log match in any directory; prefix with / (e.g. /build) to anchor a rule to the repository root.

References

FAQ

What is the difference between git pull and git fetch?

git fetch downloads new commits and branch pointers from the remote but does not touch your working tree or current branch — your local branch stays where it is. git pull runs git fetch and then merges the remote branch into the current branch. Use fetch when you want to inspect before integrating, pull when you simply want to get up to date.

What is the difference between git add and git commit?

git add copies changes from the working tree into the index (the staging area). git commit records everything currently in the index as a new snapshot in history and moves HEAD to it. Changes you have not staged are not part of the commit — that is why you can edit five files and commit only two of them.

How do I change the message of my last commit?

git commit --amend opens the editor with the previous message loaded; save it to replace it. Add --no-edit to keep the message and only add newly staged changes. Only amend commits you have not pushed to a shared remote — amending rewrites history, which will conflict with anyone who already pulled the original commit.

What does 'origin' mean in git?

origin is the default short name Git gives the remote you cloned from (or the first remote you add). It is just a label for a URL — git remote -v shows what origin actually points to, and you could rename it or add more remotes such as upstream for a fork.