Skip to content
Everyone-Website
Cheat sheet

Git Commands Cheat Sheet

The most useful Git commands in one place — setup, everyday commits, branching, remotes, undoing mistakes and stashing — with clear, copy-ready examples.

Updated 2 June 2026 5 min read

Git is the version-control system behind almost every modern software project, but most day-to-day work comes down to a couple of dozen commands. This cheat sheet collects the ones you’ll actually use, grouped by task, with short examples you can copy and adapt. Run these in a terminal from inside your project folder, and swap placeholders like <branch>, <file> and <url> for your own values.

Setup & config

Set your identity once per machine, then create or clone repositories as needed.

CommandWhat it does
git config --global user.name "Your Name"Sets the name attached to your commits
git config --global user.email "you@example.com"Sets the email attached to your commits
git config --global init.defaultBranch mainMakes main the default branch in new repos
git config --listLists all current configuration values
git initStarts a new Git repository in the current folder
git clone <url>Copies a remote repository to your machine
git clone <url> <folder>Clones into a folder name you choose

Everyday basics

The core loop: check what changed, stage it, commit it, and review the history.

CommandWhat it does
git statusShows changed, staged and untracked files
git add <file>Stages a specific file for the next commit
git add .Stages all changes in the current folder and below
git add -AStages all changes across the whole repository
git commit -m "message"Commits the staged changes with a message
git commit -am "message"Stages tracked files and commits in one step
git diffShows unstaged changes line by line
git diff --stagedShows changes that are already staged
git logShows the full commit history
git log --onelineShows a compact, one-line-per-commit history

Branching & merging

Branches let you work on features in isolation, then combine them back together.

CommandWhat it does
git branchLists local branches (current one is marked *)
git branch <branch>Creates a new branch without switching to it
git switch <branch>Switches to an existing branch
git switch -c <branch>Creates a new branch and switches to it
git checkout <branch>Older way to switch branches
git checkout -b <branch>Older way to create and switch in one step
git merge <branch>Merges the named branch into your current branch
git branch -d <branch>Deletes a branch that’s already merged
git branch -D <branch>Force-deletes a branch even if unmerged

git rebase <branch> is an alternative to merging: instead of creating a merge commit, it replays your commits on top of another branch for a cleaner, linear history. It rewrites commit history, so avoid rebasing commits you’ve already shared with others.

Working with remotes

Remotes are the shared copies of your repo, usually hosted on GitHub, GitLab or similar.

CommandWhat it does
git remote -vLists configured remotes and their URLs
git remote add origin <url>Connects your repo to a remote named origin
git fetchDownloads new commits from the remote without merging
git pullFetches and merges remote changes into your branch
git pushUploads your commits to the remote
git push -u origin <branch>Pushes a branch and sets it to track the remote
git push origin --delete <branch>Deletes a branch on the remote

Undoing things

Mistakes happen. These commands roll back changes at different levels — some are safe, some discard work (see the note at the end).

CommandWhat it does
git restore <file>Discards unstaged changes to a file
git restore --staged <file>Unstages a file but keeps your edits
git reset --soft <commit>Moves the branch back but keeps changes staged
git reset --mixed <commit>Moves the branch back and unstages changes (the default)
git reset --hard <commit>Moves the branch back and discards all changes
git revert <commit>Creates a new commit that undoes an earlier one
git commit --amendEdits the most recent commit’s message or contents

Stashing

Stashing shelves your work-in-progress so you can switch tasks with a clean working tree.

CommandWhat it does
git stashSaves uncommitted changes and reverts to a clean tree
git stash -uStashes untracked files as well
git stash listLists all stashed change sets
git stash popRestores the latest stash and removes it from the list
git stash applyRestores the latest stash but keeps it in the list
git stash dropDeletes the latest stash without restoring it

Inspecting history

When you need to understand how the code got to its current state.

CommandWhat it does
git log --oneline --graphShows a compact history with a branch/merge graph
git log --oneline --graph --allSame graph, but for every branch at once
git show <commit>Shows the details and changes of one commit
git blame <file>Shows who last changed each line of a file
git diff <commit1> <commit2>Compares two commits

A note on safety

A few commands can permanently discard work: git reset --hard throws away uncommitted changes and committed history with no easy undo, and force-pushing (git push --force) can overwrite commits on the remote that teammates rely on. When in doubt, prefer git revert (which is reversible) and use git push --force-with-lease instead of a plain force push so you don’t clobber someone else’s work.