Git
Setup
git config --global user.name
"Name"
Set your name globally (used on all commits)
git config --global user.email
"email"
Set your email globally (appears in commit metadata)
git config --global core.editor
vim
Set default editor for commit messages and rebasing
git config --list
Show all config values (global and local)
git config --local user.name
"Local Name"
Set name for current repo only (overrides global)
Init & Clone
git init
Initialise a new repo in current directory
git clone <url>
Clone a remote repo
git clone <url>
<dir>
Clone into a specific folder
git clone --depth 1
<url>
Shallow clone — latest commit only
Staging
git status
Show working tree status
git add <file>
Stage a specific file
git add .
Stage all changes in current directory
git add -A
Stage all changes everywhere (including deletions)
Interactively stage chunks — review each hunk before staging
very useful for careful commits
git restore --staged
<file>
Unstage a file without discarding changes
git restore <file>
Discard working directory changes
destructive
Commits
git commit -m
"message"
Commit staged changes with a message
git commit -am
"message"
Stage all tracked file changes and commit in one step
(modified files only)
git commit --amend
Edit the last commit message or add staged changes without
creating new commit
rewrites history
git commit --amend --no-edit
Add staged changes to last commit, keep original
message
Branches
git branch
List local branches — current marked with *
git branch -a
List all branches (local and remote)
git branch <name>
Create a new branch at current commit
git branch <name>
<commit>
Create branch from specific commit
git switch <name>
Switch to a branch — working directory updates
git switch -c
<name>
Create and switch in one step (modern replacement for
checkout -b)
git branch -m
<old>
<new>
Rename a branch
git branch -d
<name>
Delete a merged branch (safe — won't delete unmerged)
git branch -D
<name>
Force delete a branch
destructive — discards work
git branch -v
Show branches with last commit info
Merge & Rebase
git merge <branch>
Merge branch into current — creates merge commit if branches
diverged
git merge --no-ff
<branch>
Merge and always create a merge commit (for history)
git merge --squash
<branch>
Combine all commits from branch into one new commit
git rebase <branch>
Replay current commits on top of another branch
rewrites history — use
only on unpushed commits
git rebase -i HEAD~n
Interactive rebase — squash, reorder, edit, or drop
commits
git rebase --continue
Resume after resolving rebase conflicts
git rebase --abort
Cancel an in-progress rebase and return to original
state
git merge --abort
Cancel an in-progress merge (useful during conflicts)
Remote
git remote
List remote names (usually 'origin')
git remote -v
List remotes with fetch/push URLs
git remote show origin
Detailed info about a remote
git remote add
upstream <url>
Add a new remote (common for forks: upstream = original
repo)
git fetch
Download all remote changes without modifying working
directory
git fetch <remote>
Fetch from specific remote (e.g., upstream, origin)
git pull
Fetch from upstream and merge into current branch
git pull --rebase
Fetch and rebase instead of merge — cleaner linear history
recommended for feature branches
git push
Push current branch to its upstream remote
git push -u origin
<branch>
Push and set upstream tracking (do this first time per
branch)
git push origin
feature-branch
Push specific branch to specific remote
git push --force-with-lease
Safer force push — fails if remote has newer commits
always use over --force
git push origin --delete
<branch>
Delete a remote branch
Log & Diff
git log
Show commit history (newest first)
git log
--oneline --graph --all
Compact visual branch history with all branches
git log <branch>
Show history of specific branch
git log -p
<file>
Show commit history with full diffs for a file
git log --since="2 weeks ago"
Show commits since a specific time
git diff
Show unstaged changes between working tree and index
git diff --staged
Show staged changes (what's ready to commit)
git diff <branch1>..<branch2>
Diff between two branches
git blame <file>
Show who changed each line
Stash
git stash
Stash current changes
git stash push -m
"message"
Stash with a label
git stash list
List all stashes
git stash pop
Apply latest stash and remove it
git stash apply stash@{n}
Apply a specific stash without removing it
git stash drop stash@{n}
Delete a specific stash
Undoing
git revert <hash>
Create a new commit that undoes a commit — safe for shared
branches
git reset --soft HEAD~1
Undo last commit, keep changes staged — good for
amending
git reset --mixed HEAD~1
Undo last commit, keep changes unstaged (default)
most common
git reset --hard HEAD~1
Undo last commit, discard all changes
destructive — but reflog saves you
git reflog
History of HEAD changes — recover lost commits from
resets/rebases
lifesaver for accidents
git reset --hard
<hash>
Reset to a specific commit (dangerous on shared
branches)
git clean -fd
Delete untracked files and directories
destructive
git clean -n
Preview what would be deleted (always check first!)
Cherry-pick & Tags
git cherry-pick <hash>
Apply a specific commit to current branch
git cherry-pick <hash1>..<hash2>
Apply a range of commits
git tag <name>
Create a lightweight tag
git tag -a
<name>
-m
"msg"
Create an annotated tag
git push origin --tags
Push all tags to remote
Search
git grep "pattern"
Search working tree for a pattern
git log -S
"string"
Find commits that added or removed a string
git log --all --grep="message"
Search commit messages
git bisect start
Start binary search to find a bad commit