Git Commands Cheat Sheet
Common Git commands: clone, add, commit, branch, merge, rebase, stash and more, with notes for everyday version control.
| Command | Description |
|---|---|
| git init | Initialize a new repository |
| git clone | Clone a remote repo locally |
| git config | Set user.name/email etc. |
| git status | Show working tree status |
| git add | Stage changes (-A all) |
| git commit | Commit staged changes (-m message) |
| git push | Push commits to remote |
| git pull | Fetch and merge remote changes |
| git fetch | Fetch remote updates only (no merge) |
| git branch | List/create branches |
| git checkout | Switch branch or restore file |
| git switch | Switch branch (more semantic) |
| git merge | Merge a branch into current |
| git rebase | Reapply commits onto another branch |
| git stash | Stash unfinished changes |
| git log | Show commit history (--oneline) |
| git diff | Show change diff |
| git reset | Reset to a commit (--soft/--hard) |
| git revert | Undo a commit with a new commit |
| git cherry-pick | Apply a commit to current branch |
| git tag | Create a tag (version mark) |
| git remote | Manage remotes (-v list) |
| git clean | Remove untracked files (-fd force) |
| git rm | Stop tracking and delete file |
| git show | Show a commit/object detail |
Frequently Asked Questions
What is the difference between merge and rebase?
merge creates a merge commit and keeps the full branchy history; rebase replays your commits onto the target branch for a linear history, but rewrites their hashes, so never rebase a shared branch that others have pulled.
How do I undo the last commit?
To keep the changes use `git reset --soft HEAD~1`; to discard them use `git reset --hard HEAD~1`; safer is `git revert HEAD` which adds a new undo commit.
How do I temporarily save unfinished work?
Use `git stash` to shelve changes and `git stash pop` to restore. You can stash multiple times and list them with `git stash list`.
How do I unstage a file but keep the changes?
Use `git reset HEAD <file>` to unstage (changes stay in the working tree); to also discard them use `git checkout -- <file>` (or `git restore <file>`).