Git commands cheatsheet

Git tracks changes to your files as a series of commits in a repository. These commands cover the daily workflow: starting a repo, saving work, branching, merging and sharing with a remote. Keep this sheet open while you learn.

Updated:

CommandWhat it does
git initCreate a new, empty Git repository in the current directory.
git cloneDownload a remote repository and its full history to your machine.
git statusShow changed, staged and untracked files in the working tree.
git addStage changes (files) to be included in the next commit.
git commitRecord staged changes as a new commit with a message.
git logDisplay the commit history of the repository.
git diffShow unstaged or staged differences between states.
git branchList, create or delete branches in the repository.
git checkout / switchSwitch branches or restore files (switch is the modern command).
git mergeJoin another branch's history into the current branch.
git rebaseReapply commits on top of another base commit, rewriting history.
git pullFetch from a remote and merge the changes into the current branch.
git pushUpload local commits to a remote repository.
git stashTemporarily shelve uncommitted changes to reapply later.
git remoteManage the set of remote repositories you track.

Notes

Frequently asked questions

What is the difference between merge and rebase?
Merge combines histories with a new commit that has two parents. Rebase replays your commits on top of another branch, producing a linear history but rewriting commit IDs.
How do I undo a commit?
Use 'git revert <commit>' to add a new commit that undoes the change (safe, shared history). 'git reset' rewrites history and should not be used on shared branches.
What is the difference between git pull and git fetch?
Fetch downloads commits and refs from a remote without touching your working tree. Pull does a fetch and then merges or rebases those changes into your current branch.
When should I use git stash?
Stash temporarily shelves uncommitted changes so you can switch branches or pull cleanly, then restore them later with 'git stash pop'.