Git is the backbone of modern software collaboration. It lets developers track changes like a time machine, experiment safely on branches, and merge work from teammates without stepping on each other's toes. In this post, I'm sharing a comprehensive, category-based list of Git commands I mostly use at my work.
Git basics
git init: It initialize the current directory as a git repositorygit clone: Downloads an existing repository from remotegit status: See changed files, staged files, and your branchgit add: Mark changes to include in the next commit
git add file.js
git add file.js / .git restore: Unstage or pull a file out of the staging area
git restore --staged file.jsgit commit: Save a snapshot with a message. If you want to learn how to write a good git commit message, please checkout this amazing website
git commit -m "feat(login) : add login page"git mv: Renames or moves while preserving history:
git mv old.js new.jsgit rm: Removes a tracked file from repo and working directory
git rm .env.local
git commit -m "Remove env file"Undoing changes
git restore: Discard unstaged changes in a file (safe)
git restore file.js
# in all files
git restore .-
git revert <commit-sha>: Revert a commit ( safe for shared history). It creates a new commit that undoes an older one. -
git reset --soft: Reset last commit but keep changes staged
git reset --soft HEAD~1-
git reset HEAD~1: Reset last commit and unstage changes (keep in files): -
git reset --hard: Rest last commit and discard its changes (dangerous)
git reset --hard HEAD~1git clean -fd: Clean untracked files/folders (irreversible)
Git config
- Set your name and email (global):
git config --global user.name "Your Name"
git config --global user.email "you@example.com"-
git conig --list: List config -
Default branch name for new repo:
git config --global init.defaultBranch master- Safer force-push with lease (recommended alias):
git config --global alias.pushf "push --force-with-lease"Logs
git log: List basic log with commit-sha, author, date and commit message- Compact, all branches, graph
git log --oneline --graph --decorate --allgit show <commit-sha>: Show a single commitgit log --stat: Stats per commit- Filter by file
git log --online -- file.js- Search by messages/author
git log --grep "login"
git log --author="Rudra"Re-writing git history (use with care)
git commit --amend: Ammend last commit (message or staged files)git rebase -i HEAD~3: Interactive rebase (reorder/squash/edit n commits)- Autosquash fixup commits
git commit --fixup=<sha>
git rebase -i autosquash <base>Git branches
git branch: List branches- Create and switch to a branch
git switch -c feature/login
# or
git checkout -b feature/login- Switch to different branch
git switch master
# or
git checkout master- Delete local or remote branch
git push origin --delete feature/login # remote
git branch -d feature/login # merged
git branch -D wip/experiment # local force Delete- List out merged and unmerged branches
git branch --merged
git branch --no-mergedRemote repositories
git remote -v: See remotes- Add, rename, remove, change, and prune a remote
git remote add origin <gitlab or github repo url>
git remote rename origin primary
git remote remove origin
# Change remote
git remote set-url origin <new gitlab or github repo url>
# Prune
git fetch --pruneCompare anything with anything
git diff: Unstaged changesgit diff --staged: Staged changes- Compare between commits / branches
git diff master..features/login
git diff <old-sha> <new-sha>- Only names of changed files
git diff --name-only master..HEAD- Word-level diff (nice for prose)
git diff --word-diffReset
Move HEAD (and maybe index/working tree) to a commit
- Keep file staged:
git reset --soft <commit>- Unstage files (keep file changes):
git reset --mixed <commit> # default- Discard file changes too (dangerous)
git reset --hard <commit>- Reset just a file's staged state
git reset HEAD -- path/to/fileRebase (clean, linear history)
- Rebase your branch on top of master
git switch feature/login
git rebase master- Continue, Skip, Abort after conflicts
git rebase --continue
git rebase --skip
git rebase --abort- Changes base of a branch
git rebase --onto new-base old-base feature/loginPull (bring remote changes in workspace)
Note: Don't be confused git pull and git fetch are two difference things on its behavior. Git fetch only downloads and updates remote-tracking branches (e.g., origin/master) in the local repository (.git), while Git pull downloads and integrate, meaning it does a fetch and then merge (or rebase if you ask) into your current branch (workspace).
Use git fetch when you want to see what's new first (review commits, diff, then decide how to integrate).
Use git pull when you're read to bring remote changes into your current branch.
git pull: Default (fetch + merge)git pull --rebase: Rebase instead of merge (cleaner)git pull --ff-only: Fast-forward only (avoid merge commits)
Push (publish your work)
- First push a new branch (set upstream)
git push -u origin feature/login- Regular push
git push- Push tags / all tags
git push origin v1.0
git push --tags- Force push safely (don't overwrite teammates)
git push --force-with-leaseBonus: helpful inspection and stash
git blame app.js: who changed which linegit reflog: where has HEAD moved (time-machine)- Stash work and come back later
git stash
git stash list
git stash popThanks for taking time to read this post. I hope it becomes your go-to reference for everyday git works.