Git Commands

Essential Git commands — from initial setup to advanced branching and collaboration

Setup & Configuration Init

git initInitialize a new Git repository in current directory
git clone <url>Clone (copy) an existing repository
git config --global user.name "Your Name"Set your name for all repositories
git config --global user.email "email@example.com"Set your email for all repositories
git config --listList all Git configuration settings
git config --global core.editor "code --wait"Set VS Code as default editor
git config --global init.defaultBranch mainSet default branch name to main
💡 Tip: Use --local instead of --global to set config for a single repository.

Basic Workflow Stage & Commit

git statusShow current state of working directory and staging area
git add <file>Stage a specific file for commit
git add .Stage all changes (new, modified, deleted) in current directory
git add -AStage all changes in the entire repository
git add -pStage changes interactively (hunk by hunk)
git commit -m "message"Commit staged changes with a message
git commit -a -m "message"Stage all tracked files and commit (skip git add)
git commit --amend -m "new message"Change the last commit message or add forgotten files
git diffShow unstaged changes (not yet added)
git diff --stagedShow changes that are staged (will be committed)
git rm <file>Remove file from working directory and stage deletion
git mv <old> <new>Rename/move a file and stage the change

Branching & Merging Branch

git branchList all local branches (current branch highlighted)
git branch <branch-name>Create a new branch
git switch <branch-name>Switch to an existing branch (newer, safer than checkout)
git switch -c <branch-name>Create and switch to a new branch
git checkout <branch-name>Switch to a branch (older command)
git checkout -b <branch-name>Create and switch to a new branch (older)
git merge <branch-name>Merge branch-name into the current branch
git merge --abortAbort a merge that has conflicts
git branch -d <branch-name>Delete a branch (safe: only if merged)
git branch -D <branch-name>Force delete a branch (even if not merged)
git branch -m <old-name> <new-name>Rename a branch
git log --oneline --graph --allVisualize branch structure in a compact way
🌿 Best practice: Use git switch and git restore instead of git checkout — they separate branch switching from file restoring.

Remote Repositories Remote

git remote -vList all remote repositories with URLs
git remote add origin <url>Add a remote named origin
git remote rename <old> <new>Rename a remote
git remote remove <name>Remove a remote
git push -u origin <branch>Push branch and set upstream (tracking) relationship
git push origin <branch>Push a branch to remote
git push --all originPush all branches to remote
git push --tagsPush all tags to remote
git pullFetch from remote and merge into current branch
git pull --rebaseFetch and rebase instead of merge (cleaner history)
git fetchDownload changes from remote without merging
git fetch --pruneFetch and remove remote-tracking branches that no longer exist
📦 Pro tip: After git push -u, you can just use git push and git pull without specifying the remote/branch.

Viewing History Log

git logShow commit history (full format)
git log --onelineShow compact history (one line per commit)
git log --oneline --graphShow history with ASCII branch graph
git log -pShow full diff for each commit
git log --since="2 days ago"Show commits from the last 2 days
git log --author="Name"Show commits by a specific author
git log --grep="pattern"Search commit messages for a pattern
git log -S "function()"Show commits that added or removed function()
git show <commit-hash>Show details of a specific commit
git show HEADShow the latest commit
git show HEAD~2Show the commit two steps before latest
git blame <file>Show who changed each line of a file (with commit info)

Undoing Changes Reset

git restore <file>Discard changes in working directory (unstage + revert)
git restore --staged <file>Unstage a file (keep changes in working directory)
git restore --source=HEAD~1 <file>Restore file to state from one commit ago
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --mixed HEAD~1Undo last commit, keep changes unstaged (default)
git reset --hard HEAD~1Undo last commit and discard all changes (⚠️ destructive)
git reset --hard <commit-hash>Reset to a specific commit (discards everything after)
git revert <commit-hash>Create a new commit that undoes a specific commit (safe)
git stashTemporarily save uncommitted changes (clean working directory)
git stash listList all stashes
git stash popApply the latest stash and remove it from stash list
git stash applyApply the latest stash without removing it
git stash dropRemove the latest stash
⚠️ Warning: git reset --hard permanently discards changes. Use git revert for safe undo on shared branches.

Rebasing Rebase

git rebase <branch>Reapply commits from current branch on top of branch
git rebase -i HEAD~nInteractive rebase for last n commits (squash, reword, etc.)
git rebase --continueContinue rebase after resolving conflicts
git rebase --abortAbort rebase and return to original state
git rebase --skipSkip the current commit during rebase (rarely used)
git pull --rebasePull changes and rebase local commits on top (cleaner history)
🔀 Interactive rebase: Use git rebase -i HEAD~3 to squash 3 commits into one, reword messages, or reorder commits.

Tags & Releases Tag

git tagList all tags
git tag <tag-name>Create a lightweight tag at current commit
git tag -a <tag-name> -m "message"Create an annotated tag with a message
git tag <tag-name> <commit-hash>Tag a specific commit
git push origin <tag-name>Push a specific tag to remote
git push --tagsPush all tags to remote
git tag -d <tag-name>Delete a local tag
git push --delete origin <tag-name>Delete a remote tag

Advanced & Useful Power

git bisect startStart binary search to find a buggy commit
git bisect good <commit>Mark a commit as "good" during bisect
git bisect bad <commit>Mark a commit as "bad" during bisect
git bisect resetEnd bisect and return to original state
git cherry-pick <commit-hash>Apply a specific commit to the current branch
git grep "pattern"Search for a pattern in tracked files
git shortlog -s -nShow commit count per author (sorted)
git reflogShow history of all branch and HEAD movements (recover lost commits)
git clean -fdRemove untracked files and directories (⚠️ destructive)
git worktree add <path> <branch>Create a new worktree to work on multiple branches simultaneously
🔍 Reflog: If you lost a commit (e.g., after a hard reset), git reflog shows all HEAD movements — you can recover it.

Git Aliases & Shortcuts Alias

git config --global alias.co checkoutCreate alias git co for git checkout
git config --global alias.br branchAlias git br for git branch
git config --global alias.ci commitAlias git ci for git commit
git config --global alias.st statusAlias git st for git status
git config --global alias.lg "log --oneline --graph --all"Alias for a pretty log view
git config --global alias.unstage "restore --staged"Alias git unstage to unstage files
git config --global alias.last "log -1 HEAD"Alias git last to show the last commit
⚡ Speed up: Add your favorite aliases to ~/.gitconfig to save typing. For example, git st instead of git status.
Git commands reference — designed in the same style as the Linux and Vim cheatsheets.