git initCreate an empty Git repo in the current directory. Run with no arguments to init where you are.
git clone[repo]Clone a repo to your machine. The repo can be on the local FS or remote over HTTP/SSH.
git config user.name[name]Set the author for commits. Add --global to apply to every repo.
git add[path]Stage changes to prepare a commit. Use . to stage all, -p to stage hunk by hunk.
git commit -m"msg"Commit the staged snapshot with an inline message.
git statusShow staged / unstaged / untracked files in the working tree.
Git Cheatsheet
Quick reference of Git commands by group — copy & paste when you need it.
git commit --amendReplace the latest commit with the staged changes merged in. Stage nothing → just edit the message.
git rebase[base]Rebase the current branch onto base. base can be a commit ID, branch, tag, or HEAD~N.
git rebase -i HEAD~NInteractive rebase — squash, reword, drop, edit each commit.
git reflogHistory of every HEAD change — a lifesaver after an accidental reset --hard.
git reset --hard[commit]Clear staging + reset the working tree to the given commit. Dangerous.
Fix branches, commits and clean up history
git branchList local branches. Add -a to see remotes too.
git switch -c[name]Create and check out a new branch (replaces git checkout -b).
git switch[name]Switch to another branch.
git branch -d[name]Delete a merged local branch. -D to force-delete.
git push origin --delete[name]Delete a branch on the remote.
git merge[branch]Merge the given branch into the current one.
git merge --squash[branch]Combine all of a branch’s commits into a single commit.
git rebase mainReplay the current branch on top of main for a straight history.
git rebase --continueContinue the rebase after resolving conflicts. --abort to cancel.
git cherry-pick[hash]Bring a specific commit from another branch into the current one.
git log --oneline --graph --allA compact commit-history graph for all branches.
git log -p[file]Change history of a single file with diffs.
git diffCompare the working tree with staging.
git diff --stagedCompare staging with the latest commit.
git blame[file]See who changed which line, in which commit.
git show[hash]Show the full contents of a commit.
git stashTemporarily shelve working changes to switch branches cleanly.
git stash push -m"wip"Stash with a descriptive message.
git stash listList shelved stashes.
git stash popPop the latest stash and remove it from the list.
git restore[file]Restore a file (unstaged) back to the HEAD state.
git restore --staged[file]Unstage a file.
git reset --soft HEAD~1Drop the latest commit, keeping staged changes.
git reset --mixed HEAD~1Drop the commit + unstage (the default of git reset).
git reset --hard HEAD~1Drop the commit + discard all changes. Be careful!
git revert[hash]Create an inverse commit to undo an old one — safe for shared branches.
git clean -fdRemove untracked files/folders. -n to dry-run first.
git remote -vList remote URLs.
git remote add origin[url]Add a remote named origin.
git fetch[remote]Download objects from the remote without merging.
git pull --rebaseFetch + rebase the current branch onto upstream — straight history.
git push origin[branch]Push a branch to the remote.
git push --force-with-leaseSafe force push — fails if the remote has new commits.
git tag v1.0.0Create a lightweight tag at HEAD.
git tag -a v1.0.0 -m"release"Annotated tag (carries metadata, recommended for releases).
git push origin v1.0.0Push a specific tag to the remote.
git push origin --tagsPush all tags.
git tag -d v1.0.0Delete a local tag.
git config --global user.name"Your Name"Configure the global author name.
git config --global user.email"you@x.com"Configure the author email.
git config --global init.defaultBranch mainSet the default branch for git init.
git config --global pull.rebase trueMake pull use rebase by default instead of merge.
git config --global core.editor"code --wait"Use VS Code as the editor for commit messages.