Setup & Config
6 commandsgit config --global user.name "Your Name"Set the name attached to all your commits globally.
git config --global user.email "you@example.com"Set the email address associated with your commits.
git config --global core.editor "code --wait"Set VS Code as the default editor for commit messages and merges.
git config --listList all current Git configuration settings and their values.
git config --global alias.st statusCreate a shortcut alias — e.g. type git st instead of git status.
git config --global init.defaultBranch mainSet the default branch name for all new repositories to main.
Getting a Repo
5 commandsgit initInitialize a new Git repository in the current directory.
git init <directory>Create a new Git repository inside the named folder.
git clone <url>Clone a remote repository into a new local directory.
git clone <url> <directory>Clone into a specific folder name instead of the repo name.
git clone --depth 1 <url>Shallow clone — only the latest snapshot, no full history. Faster for large repos.
Staging & Snapshots
9 commandsgit statusShow the state of the working tree — staged, unstaged, and untracked files.
git add <file>Stage a specific file for the next commit.
git add .Stage all changes in the current directory and below.
git add -pInteractively stage individual hunks of changes — great for clean, atomic commits.
git diffShow unstaged changes between your working tree and the index.
git diff --stagedShow changes that are staged and ready to be committed.
git commit -m "message"Commit staged changes with an inline message.
git commit --amendReplace the last commit with a new one — useful for fixing typos or adding forgotten files.
git rm --cached <file>Untrack a file without deleting it from disk — ideal for removing accidentally committed files.
Branching & Merging
10 commandsgit branchList all local branches. The current branch is marked with an asterisk.
git branch <name>Create a new branch without switching to it.
git switch <branch>Switch to an existing branch. Modern replacement for git checkout <branch>.
git switch -c <name>Create a new branch and switch to it immediately.
git branch -d <name>Delete a branch that has already been merged. Use -D to force-delete unmerged branches.
git merge <branch>Merge the specified branch into the current branch.
git merge --no-ff <branch>Merge and always create a merge commit — preserves branch history in the log.
git rebase <branch>Reapply commits on top of another branch — produces a cleaner, linear history.
git rebase -i HEAD~<n>Interactive rebase — squash, reorder, or edit the last n commits before pushing.
git cherry-pick <commit>Apply a specific commit from another branch onto the current branch.
Remote Work
8 commandsgit remote -vList all configured remotes with their fetch and push URLs.
git remote add <name> <url>Connect a new remote repository under a given name (typically origin).
git remote remove <name>Remove a remote connection by name.
git fetch <remote>Download all changes from the remote without merging into your working branch.
git pullFetch and immediately merge remote changes into the current branch.
git pull --rebaseFetch and rebase local commits on top of remote changes — avoids unnecessary merge commits.
git push <remote> <branch>Push a local branch up to the remote repository.
git push --force-with-leaseSafer force-push — only overwrites if no one else has pushed since your last fetch.
Inspecting History
8 commandsgit logShow the full commit history for the current branch.
git log --oneline --graph --allCompact visual graph of all branches and their commit history.
git log --author="name" --since="2 weeks ago"Filter commits by author and date range.
git show <commit>Display the full diff and metadata of a specific commit.
git blame <file>Show which commit and author last modified each line of a file.
git diff <branch1>..<branch2>Show the diff between the tips of two branches.
git shortlog -snSummary of commit counts per author, sorted by most commits.
git bisect startBegin a binary search through history to find the commit that introduced a bug.
Undoing Things
9 commandsgit restore <file>Discard unstaged changes in a file, restoring it to the last commit state.
git restore --staged <file>Unstage a file without discarding its changes in the working tree.
git reset --soft HEAD~1Undo the last commit but keep changes staged and ready to recommit.
git reset --mixed HEAD~1Undo the last commit and unstage changes — files are still modified in the working tree.
git reset --hard HEAD~1⚠️ Permanently discard the last commit and all its changes. Cannot be undone easily.
git revert <commit>Create a new commit that undoes a previous commit — safe for shared/public branches.
git stashTemporarily shelve uncommitted changes so you can switch context cleanly.
git stash popRe-apply the most recently stashed changes and remove them from the stash list.
git stash listView all stashed change sets with their index and description.
Tagging & Releases
5 commandsgit tagList all tags in the repository.
git tag <name>Create a lightweight tag pointing to the current commit.
git tag -a <name> -m "message"Create an annotated tag with a message — recommended for release versions.
git push <remote> <tag>Push a specific tag to the remote — tags are not pushed automatically.
git push <remote> --tagsPush all local tags to the remote at once.