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.
| Command | What it does |
|---|---|
| git init | Create a new, empty Git repository in the current directory. |
| git clone | Download a remote repository and its full history to your machine. |
| git status | Show changed, staged and untracked files in the working tree. |
| git add | Stage changes (files) to be included in the next commit. |
| git commit | Record staged changes as a new commit with a message. |
| git log | Display the commit history of the repository. |
| git diff | Show unstaged or staged differences between states. |
| git branch | List, create or delete branches in the repository. |
| git checkout / switch | Switch branches or restore files (switch is the modern command). |
| git merge | Join another branch's history into the current branch. |
| git rebase | Reapply commits on top of another base commit, rewriting history. |
| git pull | Fetch from a remote and merge the changes into the current branch. |
| git push | Upload local commits to a remote repository. |
| git stash | Temporarily shelve uncommitted changes to reapply later. |
| git remote | Manage the set of remote repositories you track. |
Notes
- Most commands accept flags (like -m for message or -a for all); run 'git help <command>' for the full list.
- Branch names are cheap and disposable — commit often on a feature branch, then merge to main.
- Before destructive commands like rebase or reset, make sure your work is committed or stashed.
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'.