Complete Git command reference β from setup and staging to branching, merging, rebasing, and advanced workflows.
git config --global user.name "Name"Setup & ConfigSet global author name
e.g. git config --global user.name "Jane Doe"
git config --global user.email "email"Setup & ConfigSet global author email
e.g. git config --global user.email "[email protected]"
git config --listSetup & ConfigList all configuration settings
git config --global core.editor "code"Setup & ConfigSet VS Code as default editor
git config --global alias.<alias> <cmd>Setup & ConfigCreate a command alias
e.g. git config --global alias.st status
git initSetup & ConfigInitialize a new local repository
git init <dir>Setup & ConfigInitialize a repo in a specific directory
e.g. git init my-project
git clone <url>Setup & ConfigClone a remote repository
e.g. git clone https://github.com/user/repo.git
git clone <url> <dir>Setup & ConfigClone into a specific folder
e.g. git clone https://github.com/user/repo.git my-dir
git statusStaging & BasicsShow working tree status
git status -sStaging & BasicsShort/compact status output
git add <file>Staging & BasicsStage a specific file
e.g. git add README.md
git add .Staging & BasicsStage all changes in current directory
git add -pStaging & BasicsInteractively stage hunks of changes
git commit -m "msg"Staging & BasicsCommit staged changes with a message
e.g. git commit -m "feat: add login page"
git commit -am "msg"Staging & BasicsStage tracked files and commit in one step
git commit --amenddestructiveStaging & BasicsModify the most recent commit (message or content)
git diffStaging & BasicsShow unstaged changes
git diff --stagedStaging & BasicsShow staged changes (vs last commit)
git diff <branch1>..<branch2>Staging & BasicsCompare two branches
e.g. git diff main..feature
git rm <file>Staging & BasicsRemove a file from working tree and index
e.g. git rm old-file.txt
git rm --cached <file>Staging & BasicsUntrack a file without deleting it
e.g. git rm --cached secret.txt
git mv <old> <new>Staging & BasicsMove or rename a tracked file
e.g. git mv old.js new.js
git branchBranching & MergingList local branches
git branch -aBranching & MergingList all branches (local + remote)
git branch <name>Branching & MergingCreate a new branch
e.g. git branch feature/auth
git checkout <branch>Branching & MergingSwitch to an existing branch
e.g. git checkout main
git checkout -b <branch>Branching & MergingCreate and switch to a new branch
e.g. git checkout -b feature/auth
git switch <branch>Branching & MergingSwitch branches (modern syntax)
e.g. git switch main
git switch -c <branch>Branching & MergingCreate and switch (modern syntax)
e.g. git switch -c feature/auth
git branch -d <branch>Branching & MergingDelete a merged branch
e.g. git branch -d feature/auth
git branch -D <branch>destructiveBranching & MergingForce delete a branch (unmerged too)
e.g. git branch -D feature/old
git branch -m <old> <new>Branching & MergingRename a branch
e.g. git branch -m old-name new-name
git merge <branch>Branching & MergingMerge a branch into current branch
e.g. git merge feature/auth
git merge --no-ff <branch>Branching & MergingMerge with a merge commit (no fast-forward)
git merge --squash <branch>Branching & MergingSquash all commits from branch into one staged change
git merge --abortBranching & MergingAbort an in-progress merge
git remote -vRemote OperationsList remote connections with URLs
git remote add <name> <url>Remote OperationsAdd a new remote
e.g. git remote add origin https://github.com/user/repo.git
git remote remove <name>Remote OperationsRemove a remote connection
e.g. git remote remove origin
git remote rename <old> <new>Remote OperationsRename a remote
e.g. git remote rename origin upstream
git fetchRemote OperationsDownload all changes from remote (no merge)
git fetch <remote>Remote OperationsFetch from a specific remote
e.g. git fetch origin
git pullRemote OperationsFetch and merge changes from remote
git pull --rebaseRemote OperationsFetch and rebase onto remote branch
git push <remote> <branch>Remote OperationsPush branch to remote
e.g. git push origin main
git push -u origin <branch>Remote OperationsPush and set upstream tracking
e.g. git push -u origin feature/auth
git push --force-with-leasedestructiveRemote OperationsSafe force push (fails if others pushed)
git push --tagsRemote OperationsPush all tags to remote
git push origin --delete <branch>destructiveRemote OperationsDelete a remote branch
e.g. git push origin --delete feature/old
git restore <file>destructiveUndoing ChangesDiscard changes in working directory
e.g. git restore index.html
git restore --staged <file>Undoing ChangesUnstage a file (keep changes)
e.g. git restore --staged index.html
git reset HEAD <file>Undoing ChangesUnstage a file (older syntax)
e.g. git reset HEAD index.html
git reset --soft HEAD~1Undoing ChangesUndo last commit, keep changes staged
git reset --mixed HEAD~1Undoing ChangesUndo last commit, unstage changes (default)
git reset --hard HEAD~1destructiveUndoing ChangesUndo last commit, discard all changes
git reset --hard <commit>destructiveUndoing ChangesReset to a specific commit, discard everything after
git revert <commit>Undoing ChangesCreate a new commit that undoes a specific commit
e.g. git revert a1b2c3d
git revert HEADUndoing ChangesRevert the most recent commit
git clean -fddestructiveUndoing ChangesRemove untracked files and directories
git clean -nUndoing ChangesDry run: show what would be cleaned
git stashStashingStash current working directory changes
git stash push -m "msg"StashingStash with a descriptive message
e.g. git stash push -m "WIP: auth feature"
git stash listStashingList all stashes
git stash popStashingApply most recent stash and remove it
git stash apply stash@{n}StashingApply a specific stash without removing
e.g. git stash apply stash@{0}
git stash drop stash@{n}StashingDelete a specific stash
e.g. git stash drop stash@{0}
git stash cleardestructiveStashingRemove all stashes
git stash branch <branch>StashingCreate a branch from a stash
e.g. git stash branch feature/from-stash
git rebase <branch>Rebase & AdvancedRebase current branch onto another
e.g. git rebase main
git rebase -i HEAD~nRebase & AdvancedInteractive rebase for last n commits
e.g. git rebase -i HEAD~3
git rebase --continueRebase & AdvancedContinue rebase after resolving conflicts
git rebase --abortRebase & AdvancedAbort an in-progress rebase
git cherry-pick <commit>Rebase & AdvancedApply a specific commit to current branch
e.g. git cherry-pick a1b2c3d
git cherry-pick <c1>..<c2>Rebase & AdvancedApply a range of commits
e.g. git cherry-pick a1b2c3d..f6e5d4c
git bisect startRebase & AdvancedStart binary search for a bug
git bisect good <commit>Rebase & AdvancedMark a commit as good in bisect
git bisect bad <commit>Rebase & AdvancedMark a commit as bad in bisect
git bisect resetRebase & AdvancedEnd bisect session
git worktree add <path> <branch>Rebase & AdvancedCheck out a branch into a separate directory
git logInspection & LogShow commit history
git log --onelineInspection & LogCompact one-line log
git log --oneline --graph --allInspection & LogVisual branch graph in terminal
git log -n <count>Inspection & LogShow last N commits
e.g. git log -n 5
git log --author="name"Inspection & LogFilter commits by author
e.g. git log --author="Jane Doe"
git log --since="2 weeks ago"Inspection & LogShow commits since a date
git log -p <file>Inspection & LogShow changes to a specific file over time
e.g. git log -p README.md
git log --follow <file>Inspection & LogFollow renames of a file in history
git show <commit>Inspection & LogShow details of a specific commit
e.g. git show a1b2c3d
git blame <file>Inspection & LogShow who last modified each line
e.g. git blame src/app.tsx
git shortlog -snInspection & LogSummarize commits by author
git reflogInspection & LogShow history of HEAD movements (recovery tool)
git grep "pattern"Inspection & LogSearch working directory for a pattern
e.g. git grep "TODO"
git tagTagsList all tags
git tag <name>TagsCreate a lightweight tag
e.g. git tag v1.0.0
git tag -a <name> -m "msg"TagsCreate an annotated tag
e.g. git tag -a v1.0.0 -m "Release v1.0.0"
git tag -a <name> <commit>TagsTag a past commit
e.g. git tag -a v0.9.0 a1b2c3d
git show <tag>TagsShow tag metadata and commit
e.g. git show v1.0.0
git push origin <tag>TagsPush a specific tag to remote
e.g. git push origin v1.0.0
git push origin --tagsTagsPush all tags to remote
git tag -d <tag>TagsDelete a local tag
e.g. git tag -d v1.0.0
git push origin --delete <tag>TagsDelete a remote tag
e.g. git push origin --delete v1.0.0
git merge combines two branches by creating a merge commit, preserving the full history of both branches. git rebase moves commits from one branch onto another, rewriting history to create a linear, cleaner commit history. Use merge for public branches and shared work; use rebase for cleaning up local feature branches before merging.
Use git reset --soft HEAD~1 to undo the last commit while keeping changes staged. Use git reset HEAD~1 (or --mixed) to unstage changes but keep them in the working directory. Use git reset --hard HEAD~1 to permanently discard the commit and all changes (destructive operation).
git fetch downloads changes from the remote but does NOT merge them into your current branch. It updates your remote-tracking branches (origin/main). git pull is git fetch + git merge (or git rebase with --rebase flag). Use fetch when you want to inspect changes before integrating them.
After a conflicting merge/rebase, Git marks conflicted files with '<<<<<<', =======, >>>>>> markers. Open each conflicted file, manually choose which changes to keep, remove the markers, then stage the resolved files with git add '<'file> and complete with git merge --continue or git commit.