Setup
git config --global user.name 'Your Name'Sets the name attached to commits, globally for every repo.
git config --global user.email 'you@example.com'Sets the email attached to commits.
git initCreates a new, empty Git repository in the current directory.
git clone git@host:org/repo.gitClones a remote repository, including its full history, into a new local directory.
Status & Diff
git statusShows staged, unstaged and untracked changes relative to the last commit.
git diffShows unstaged changes line by line.
git diff --stagedShows changes that are staged but not yet committed.
git log --oneline --graph --allShows commit history as a compact, one-line-per-commit graph across every branch.
Branching
git branchLists local branches, marking the current one.
git checkout -b feature/my-changeCreates a new branch from the current commit and switches to it.
git switch feature/my-changeSwitches to an existing branch. The modern, narrower alternative to checkout for this specific action.
git branch -d feature/my-changeDeletes a local branch, but only if it's already merged. Use -D to force-delete an unmerged branch.
Commits
git add .Stages every changed file in the current directory and below.
git add -pInteractively stages changes hunk by hunk, for splitting unrelated edits into separate commits.
git commit -m 'message'Commits staged changes with a message.
git commit --amendRewrites the most recent commit instead of creating a new one. Never amend a commit that's already been pushed and pulled by someone else — it changes the commit hash.
Rebase
git rebase mainReplays the current branch's commits on top of main's latest commit, producing linear history instead of a merge commit.
git rebase -i HEAD~3Opens an interactive rebase for the last 3 commits — squash, reorder, reword or drop them before they're shared.
git rebase --continueResumes a rebase after resolving a conflict in the current commit.
git rebase --abortCancels an in-progress rebase and returns the branch to its state before it started.
Never rebase shared history
Rebasing rewrites commit hashes. If anyone else has already pulled the branch, rebasing and force-pushing rewrites history out from under them — every collaborator's local branch now diverges. Rebase local, unpushed work freely; for shared branches, merge instead.
Stash
git stashShelves uncommitted changes and restores a clean working directory.
git stash listLists all stashed changesets.
git stash popReapplies the most recent stash and removes it from the stash list.
git stash applyReapplies the most recent stash but keeps it in the stash list — useful for applying the same stash to multiple branches.
Remotes
git remote -vLists configured remotes and their URLs.
git fetch originDownloads new commits and branches from a remote without merging them into your working branch.
git pullFetches and merges (or rebases, depending on config) the current branch's upstream in one step.
git push -u origin feature/my-changePushes a branch and sets it to track the remote branch, so future push/pull need no arguments.
git push --force-with-leaseForce-pushes, but fails safely if the remote branch has commits you haven't seen — unlike plain --force, it won't silently overwrite someone else's work.
Undoing Things
git restore FILE_NAMEDiscards unstaged changes to a file, reverting it to the last commit.
git restore --staged FILE_NAMEUnstages a file without discarding its changes.
git reset --soft HEAD~1Undoes the last commit but keeps its changes staged.
git reset --hard HEAD~1Undoes the last commit and discards its changes completely — unrecoverable through normal means once the reflog entry expires.
git revert COMMIT_HASHCreates a new commit that undoes a previous one, without rewriting history. The safe way to undo a change that's already been pushed.
Recovery
git reflogShows a log of everywhere HEAD has pointed — including commits a hard reset or rebase seemingly threw away. Almost always the way back from a 'lost' commit.
git cherry-pick COMMIT_HASHApplies a single commit from anywhere in history onto the current branch.
Nothing is really gone
Git rarely deletes commit objects immediately — a hard reset or an abandoned branch just stops pointing at them.
git reflog finds the hash, and git checkout COMMIT_HASH or git cherry-pick gets it back, until garbage
collection eventually runs.