Git Commands Cheat Sheet

Common Git commands: clone, add, commit, branch, merge, rebase, stash and more, with notes for everyday version control.

CommandDescription
git initInitialize a new repository
git cloneClone a remote repo locally
git configSet user.name/email etc.
git statusShow working tree status
git addStage changes (-A all)
git commitCommit staged changes (-m message)
git pushPush commits to remote
git pullFetch and merge remote changes
git fetchFetch remote updates only (no merge)
git branchList/create branches
git checkoutSwitch branch or restore file
git switchSwitch branch (more semantic)
git mergeMerge a branch into current
git rebaseReapply commits onto another branch
git stashStash unfinished changes
git logShow commit history (--oneline)
git diffShow change diff
git resetReset to a commit (--soft/--hard)
git revertUndo a commit with a new commit
git cherry-pickApply a commit to current branch
git tagCreate a tag (version mark)
git remoteManage remotes (-v list)
git cleanRemove untracked files (-fd force)
git rmStop tracking and delete file
git showShow 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>`).