A Comprehensive list of Git commands I use at Work

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 add file.js
git add file.js / .
git restore --staged file.js
git commit -m "feat(login) : add login page"
git mv old.js new.js
git rm .env.local
git commit -m "Remove env file"

Undoing changes

git restore file.js
 
# in all files
git restore .
git reset --soft HEAD~1
git reset --hard HEAD~1

Git config

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch master
git config --global alias.pushf "push --force-with-lease"

Logs

git log --oneline --graph --decorate --all
git log --online -- file.js
git log --grep "login"
git log --author="Rudra"

Re-writing git history (use with care)

git commit --fixup=<sha>
git rebase -i autosquash <base>

Git branches

git switch -c feature/login
 
# or 
 
git checkout -b feature/login
git switch master
 
# or
 
git checkout master
git push origin --delete feature/login      # remote
git branch -d feature/login   # merged
 
git branch -D wip/experiment  # local force Delete
git branch --merged
git branch --no-merged

Remote repositories

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 --prune

Compare anything with anything

git diff master..features/login
git diff <old-sha> <new-sha>
git diff --name-only master..HEAD
git diff --word-diff

Reset

Move HEAD (and maybe index/working tree) to a commit

git reset --soft <commit>
git reset --mixed <commit>  # default
git reset --hard <commit>
git reset HEAD -- path/to/file

Rebase (clean, linear history)

git switch feature/login
git rebase master
git rebase --continue
git rebase --skip
git rebase --abort
git rebase --onto new-base old-base feature/login

Pull (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.

Push (publish your work)

git push -u origin feature/login
git push
git push origin v1.0
git push --tags
git push --force-with-lease

Bonus: helpful inspection and stash

git stash
git stash list
git stash pop

Thanks for taking time to read this post. I hope it becomes your go-to reference for everyday git works.