Git Commands

Git commands cheatsheet.

The Git commands you reach for daily — staging, branching, syncing and undoing. Tap any command to copy it.

Setup & config
git initCreate a new Git repository in the current folder
git clone <url>Copy a remote repository to your machine
git config --global user.name "Name"Set the name attached to your commits
git config --global user.email "you@example.com"Set the email attached to your commits
Staging & committing
git statusShow changed, staged and untracked files
git add <file>Stage a specific file for the next commit
git add .Stage all changes in the current folder
git commit -m "message"Commit staged changes with a message
git commit -am "message"Stage tracked files and commit in one step
git restore <file>Discard uncommitted changes to a file
git reset <file>Unstage a file but keep its changes
Branches & merging
git branchList local branches
git switch -c <name>Create a new branch and switch to it
git switch <name>Switch to an existing branch
git merge <name>Merge another branch into the current one
git branch -d <name>Delete a merged branch
git rebase <name>Reapply commits on top of another branch
Remote & sync
git remote -vShow configured remote repositories
git remote add origin <url>Connect a local repo to a remote
git pushUpload commits to the remote
git push -u origin <branch>Push and set the upstream branch
git pullFetch and merge changes from the remote
git fetchDownload remote changes without merging
History & undo
git log --onelineShow a compact commit history
git diffShow unstaged changes line by line
git stashShelve uncommitted changes for later
git stash popReapply the most recently stashed changes
git revert <commit>Create a new commit that undoes a commit
git reset --hard <commit>Reset the branch and working tree to a commit
git cherry-pick <commit>Apply a specific commit onto the current branch

The handful of commands you'll use constantly

Git has hundreds of options, but day-to-day work runs on a small core. You check what's changed with git status, stage the changes you want with git add, record them with git commit -m "message", and share them with git push. To bring in others' work you use git pull. If you do nothing else, getting fluent with this loop covers the large majority of real Git usage. Everything else on this page is for the moments that loop isn't enough.

Branching is where Git earns its keep

Branches let you work on something without disturbing the main line of code. Create and switch to one with git switch -c feature-name, commit freely, then merge it back with git merge once it's ready. The mental model that helps most beginners: a branch is just a movable label pointing at a commit, and switching branches changes which version of the files you see. Keeping each piece of work on its own branch keeps your history readable and makes collaborating far less stressful.

Undoing things without panic

The commands people fear most are the undo ones, usually because the wrong choice can lose work. The safe rule of thumb: use git revert to undo a commit that others may already have (it adds a new commit that reverses the old one, changing nothing about history), and reserve git reset --hard for local commits no one else has seen, because it genuinely discards changes. git stash is the unsung hero here — it shelves your uncommitted work so you can switch tasks, then git stash pop brings it back.

Tips that prevent headaches

Commit in small, logical chunks with clear messages — your future self reads them constantly. Run git status before every commit so you know exactly what you're including. Pull before you start work to reduce merge conflicts. And when something looks scary, remember that committed work in Git is remarkably hard to truly lose. Pair these with the VS Code shortcuts for editing and the terminal commands for navigating your project, and you've got the core developer workflow covered.

FAQ

How do I undo the last Git commit?
Use git revert <commit> to create a new commit that undoes it (safe for shared history), or git reset --hard <commit> to move the branch back and discard changes (only on local, unshared work).
What's the difference between git switch and git checkout?
git switch is the newer, clearer command for changing branches (git switch -c <name> creates one), while git checkout does that plus file restoring. Both work; switch is recommended for branches.

More cheatsheets