Setup & Config
git config --global user.name "Name" Set your global username git config --global user.email "email@example.com" Set your global email git config --global core.editor "code --wait" Set VS Code as default editor git config --list List all config settings git config --global alias.st status Create a command alias (st → status) Creating & Cloning
git init Initialize a new local repository git init <directory> Create a new repo in a subdirectory git clone <url> Clone a remote repository locally git clone <url> <directory> Clone into a specific folder name git clone --depth 1 <url> Shallow clone — only latest commit Staging & Committing
git status Show working tree status git add <file> Stage a specific file git add . Stage all changes in current directory git add -p Interactively stage hunks (partial stage) git commit -m "message" Commit staged changes with message git commit --amend Amend the last commit (message or files) git commit --amend --no-edit Amend last commit without editing message git reset HEAD <file> Unstage a file (keep working changes) git restore <file> Discard working directory changes git restore --staged <file> Unstage a file (Git 2.23+) Branching
git branch List local branches git branch -a List all branches (local + remote) git branch <name> Create a new branch git branch -d <name> Delete a branch (safe — must be merged) git branch -D <name> Force delete a branch git branch -m <old> <new> Rename a branch git checkout <branch> Switch to an existing branch git checkout -b <branch> Create and switch to a new branch git switch <branch> Switch branch (Git 2.23+) git switch -c <branch> Create and switch (Git 2.23+) Merging & Rebasing
git merge <branch> Merge a branch into current branch git merge --no-ff <branch> Merge with a merge commit (no fast-forward) git merge --squash <branch> Squash branch commits into one staged change git merge --abort Abort a merge in progress git rebase <branch> Rebase current branch onto another git rebase -i HEAD~n Interactive rebase — squash/edit last n commits git rebase --continue Continue rebase after resolving conflicts git rebase --abort Abort a rebase in progress git cherry-pick <commit> Apply a specific commit to current branch Remote Repositories
git remote -v List remote connections with URLs git remote add origin <url> Add a remote named "origin" git remote remove <name> Remove a remote git remote set-url origin <url> Change remote URL git fetch Download remote changes (no merge) git fetch --prune Fetch and remove stale remote-tracking branches git pull Fetch and merge from tracking branch git pull --rebase Fetch and rebase instead of merge git push origin <branch> Push branch to remote git push -u origin <branch> Push and set upstream tracking git push --force-with-lease Force push (safer — fails if remote changed) git push origin --delete <branch> Delete a remote branch Stashing
git stash Stash current working directory changes git stash push -m "message" Stash with a descriptive name git stash list List all stashes git stash pop Apply most recent stash and remove it git stash apply stash@{n} Apply a specific stash (keep it) git stash drop stash@{n} Delete a specific stash git stash clear Remove all stashes git stash branch <branch> Create a branch from a stash Log & Diff
git log Show commit history git log --oneline --graph --decorate Compact branch graph view git log -p Show commits with diffs git log --author="Name" Filter commits by author git log --since="2 weeks ago" Filter commits by date git log --follow <file> Show file history including renames git diff Show unstaged changes git diff --staged Show staged changes git diff <branch1>..<branch2> Compare two branches git blame <file> Show who changed each line git show <commit> Show commit details and diff Undoing & Reset
git revert <commit> Create a new commit that undoes a commit git revert HEAD Revert most recent commit git reset --soft HEAD~1 Undo last commit — keep changes staged git reset --mixed HEAD~1 Undo last commit — keep changes unstaged git reset --hard HEAD~1 Undo last commit — discard all changes git clean -fd Remove untracked files and directories git reflog Show history of HEAD movements (recovery tool) Tags
git tag List all tags git tag v1.0.0 Create a lightweight tag git tag -a v1.0.0 -m "message" Create an annotated tag git push origin v1.0.0 Push a specific tag to remote git push origin --tags Push all tags to remote git tag -d v1.0.0 Delete a local tag git push origin --delete v1.0.0 Delete a remote tag Advanced
git bisect start Start binary search for a bug-introducing commit git bisect good <commit> Mark commit as working (good) git bisect bad <commit> Mark commit as broken (bad) git worktree add <path> <branch> Check out a branch into a new directory git submodule add <url> Add a git submodule git submodule update --init --recursive Initialize and update all submodules git archive --format zip HEAD > out.zip Export current HEAD as a ZIP git shortlog -sn Commit count per author