Cheat Sheets Development

Git Cheat Sheet

The Git commands you actually use, grouped by task, plus a table for undoing the usual mistakes and habits that keep history clean.

Last reviewed:

AdSense Placeholder
Slot: header_reference_page
On this page

Git records snapshots of a project. Three places matter: the working tree (your files), the staging area (what will go into the next commit) and the repository (the saved history). Most commands move changes between them. Every command below was checked against a real Git installation.

Commands by task

Set up

Command What it does
git config --global user.name "Ana" Set the name stored in your commits
git init Start a new repository in this folder
git clone <url> Copy a remote repository, history included

Save changes

Command What it does
git status Show changed, staged and untracked files
git add <file> Stage a file for the next commit (git add -p stages chosen pieces)
git commit -m "message" Record the staged changes with a message
git commit --amend Rewrite the last commit (message or content); only before pushing
git diff Show unstaged changes (--staged for staged ones)

Branches

Command What it does
git branch List branches (-d name deletes a merged one)
git switch <branch> Move to another branch (-c name creates it first)
git merge <branch> Merge another branch into the current one
git rebase <branch> Replay your commits on top of another branch (-i edits, squashes, reorders)
git cherry-pick <commit> Copy one commit onto the current branch

Remotes

Command What it does
git remote -v List remote repositories and their URLs
git fetch Download new commits without changing your files
git pull --rebase Fetch and replay your local commits on top (a tidier history than a merge)
git push -u origin <branch> Publish a branch and remember it for plain git push
git push --force-with-lease Overwrite the remote branch only if nobody else pushed meanwhile

Look around

Command What it does
git log --oneline --graph Compact history with branch lines
git show <commit> Show one commit and its changes
git blame <file> Show who last changed each line
git grep "text" Search tracked files
git bisect start Binary-search the history for the commit that broke something

Undo

Command What it does
git restore <file> Discard changes in a file (--staged unstages it instead)
git revert <commit> Add a new commit that undoes an old one; safe for shared history
git reset --soft HEAD~1 Undo the last commit but keep its changes staged
git reset --hard <commit> Move the branch and throw away all later changes: destructive
git reflog List where HEAD has been; the way back from most mistakes
git clean -fd Delete untracked files and folders; try -n first

Stash and tags

Command What it does
git stash push -u Shelve your work in progress, untracked files included
git stash pop Bring back the latest stash and drop it
git tag -a v1.0 -m "Release" Mark a release with an annotated tag

Undoing mistakes

Situation Fix
I edited a file and want the old version back git restore <file>
I staged the wrong file git restore --staged <file>
My last commit message has a typo git commit --amend
I committed to the wrong branch git switch <right-branch>, git cherry-pick <commit>, then git reset --hard HEAD~1 on the wrong branch
I pushed a bad commit others already pulled git revert <commit>
I ran a reset or rebase and lost commits git reflog, then git reset --hard <old-hash>

Commands that rewrite history (reset --hard, rebase, push --force) are safe on commits only you have. Once others have pulled them, prefer git revert.

Good habits

  • Commit small, related changes with a message that says why, in the imperative: "Fix login redirect".
  • Pull or fetch before you push, and use --force-with-lease instead of --force.
  • Keep secrets out of the repository. A committed password stays in the history even after you delete the file. Use a .gitignore for build output and local files: the .gitignore Generator writes one for your stack.
  • Use git status constantly. It also tells you the next command to run.

See Git for the idea behind the tool, and Linux Command Line Cheat Sheet for the shell commands you will use alongside it.

Try these tools

See also

  • Glossary Git
    Git is a distributed version control system that records snapshots of a project's files so you can track changes, go back and collaborate.
  • Cheat sheet Linux Command Line Cheat Sheet
    The terminal commands for files, search, text, processes, disk, network and archives, plus pipes, redirection.
AdSense Placeholder
Slot: footer_leaderboard