https://git-scm.com/cheat-sheet
1. First-time setup
| Command | One-line description |
| --------------------------------------------- | --------------------------------------- |
| `git config --global user.name "A U Thor"` | Declare your name for commits |
| `git config --global user.email a@b.com` | Declare your e-mail |
| `git config --global init.defaultBranch main` | Make new repos start with branch “main” |
| `git config --global color.ui auto` | Pretty terminal colours |
| `git config --list` | Show all settings |
| `git help <cmd>` | Open the built-in HTML manual |
| `git version` | Confirm the installed Git release |
git config --global user.name "Mohit Bhati"
git config --global user.email mohit.bhati@cognizant.com
git config --global init.defaultBranch main
git config --global color.ui auto
git config --list
git help commit # Opens help for 'commit'
git version # e.g., git version 2.40.1
2. Create / clone a project
| Command | One-line description |
| ----------------------- | --------------------------------------- |
| `git init` | Turn the current folder into a Git repo |
| `git clone <url>` | Download a full repo into a new folder |
| `git clone <url> <dir>` | Same, but choose the folder name |
git init # Initializes empty repo in current dir
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-project
3. Everyday snapshotting
| Command | One-line description |
| ------------------------ | ------------------------------------------- |
| `git status` | What changed / what’s staged |
| `git add <file>` | Stage that file’s changes |
| `git add .` | Stage every new/modified file under the cwd |
| `git add -A` | Stage everything (respects .gitignore) |
| `git restore <file>` | Throw away unstaged edits (safe) |
| `git rm <file>` | Delete and stage the deletion |
| `git mv old new` | Rename and stage the rename |
| `git commit -m "msg"` | Save the staged snapshot |
| `git commit -a -m "msg"` | Stage tracked files AND commit in one go |
| `git commit --amend` | Fix the last commit (message or content) |
git status
git add README.md
git add .
git add -A
git restore config.json # Discard local changes to config.json
git rm old-file.txt
git mv old-name.js new-name.js
git commit -m "Add login feature"
git commit -a -m "Update dependencies" # Skips 'git add' for tracked files
git commit --amend -m "Fix typo in previous commit"
4. Inspecting history
| Command | One-line description |
| -------------------------------------- | ---------------------------------- |
| `git log` | Commit list (newest first) |
| `git log --oneline --graph --decorate` | Compact ASCII graph |
| `git log --follow <file>` | History of one file across renames |
| `git show <commit>` | Diff + metadata for that commit |
| `git diff` | Unstaged changes |
| `git diff --staged` | Staged changes |
| `git diff HEAD~1` | Changes since previous commit |
| `git blame <file>` | Line-by-line authorship |
| `git grep "pattern"` | Search every commit for a string |
git log
git log --oneline --graph --decorate
git log --follow src/utils.js
git show abc1234
git diff # See unstaged changes
git diff --staged # See staged changes
git diff HEAD~1 # See changes since last commit
git blame app.py
git grep "TODO" # Finds all lines with "TODO"
5. Branching & merging
| Command | One-line description |
| ------------------------------ | ------------------------------------------- |
| `git branch` | List local branches |
| `git branch -r` | List remote branches |
| `git branch -a` | List all branches |
| `git branch <name>` | Create branch |
| `git checkout <name>` | Switch to branch (old syntax) |
| `git switch <name>` | Switch to branch (new syntax) |
| `git switch -c <name>` | Create & switch in one step |
| `git merge <branch>` | Merge it into current branch |
| `git merge --abort` | Bail out of a conflicted merge |
| `git rebase <branch>` | Replay commits on top of <branch> |
| `git rebase -i HEAD~3` | Interactive rebase (squash, re-order, drop) |
| `git tag v1.0` | Lightweight tag |
| `git tag -a v1.0 -m "Release"` | Annotated tag |
| `git push origin v1.0` | Share the tag |
git branch # Shows current branches
git branch -r # Shows remote branches
git branch -a # Shows all
git branch feature/login
git checkout feature/login # Switch (legacy)
git switch main # Switch (modern)
git switch -c hotfix/bug-42 # Create + switch
git merge feature/login
git merge --abort # If merge conflict
git rebase main # Replay current branch on main
git rebase -i HEAD~3 # Interactive: squash last 3 commits
git tag v1.2
git tag -a v1.2 -m "Production release"
git push origin v1.2
6. Stashing work-in-progress
| Command | One-line description |
| --------------------------- | --------------------------- |
| `git stash` | Save dirty state to a stack |
| `git stash -u` | Stash untracked files too |
| `git stash list` | Show stack |
| `git stash pop` | Apply & remove top stash |
| `git stash apply stash@{2}` | Apply without removing |
| `git stash drop stash@{1}` | Delete a stash |
| `git stash clear` | Empty the entire stack |
git stash
git stash -u # Include untracked files
git stash list
git stash pop
git stash apply stash@{2}
git stash drop stash@{1}
git stash clear
7. Remotes & collaboration
| Command | One-line description |
| --------------------------------- | ---------------------------------- |
| `git remote -v` | List remote nicknames & URLs |
| `git remote add origin <url>` | Add a remote |
| `git remote set-url origin <url>` | Change the URL |
| `git remote rename old new` | Rename a remote |
| `git remote remove origin` | Delete a remote |
| `git fetch` | Download objects & refs (no merge) |
| `git pull` | Fetch + merge current branch |
| `git pull --rebase` | Fetch + rebase instead |
| `git push` | Upload current branch |
| `git push -u origin feature` | Push & set upstream once |
| `git push --tags` | Share all tags |
| `git push origin :old` | Delete remote branch (old syntax) |
| `git push origin --delete old` | Delete remote branch (new syntax) |
git remote -v
git remote add origin https://github.com/user/myapp.git
git remote set-url origin https://github.com/user/myapp-new.git
git remote rename origin upstream
git remote remove origin
git fetch
git pull
git pull --rebase
git push
git push -u origin feature/dashboard
git push --tags
git push origin :old-feature # Delete remote branch (old)
git push origin --delete old-feature # Delete (new)
8. Undoing things
| Command | One-line description |
| -------------------------- | ---------------------------------------- |
| `git checkout -- <file>` | Discard unstaged edits (dangerous) |
| `git restore <file>` | Safer way to do the same |
| `git reset HEAD <file>` | Unstage a file |
| `git reset --soft HEAD~1` | Undo last commit, keep changes staged |
| `git reset --mixed HEAD~1` | Undo last commit, unstage changes |
| `git reset --hard HEAD~1` | Nuke last commit & all changes |
| `git revert <commit>` | Create new commit that undoes <commit> |
| `git reflog` | Safety net: every position HEAD has been |
git checkout -- config.toml # Discard changes (old way)
git restore config.toml # Discard changes (new)
git reset HEAD notes.md # Unstage notes.md
git reset --soft HEAD~1 # Undo commit, keep staged
git reset --mixed HEAD~1 # Undo commit, unstage
git reset --hard HEAD~1 # ⚠️ Delete commit & changes!
git revert abc1234 # Safe undo via new commit
git reflog # Recover lost commits
9. Submodule & subtree helpers
| Command | One-line description |
| ----------------------------------------- | ------------------------------ |
| `git submodule add <url> path` | Embed another repo |
| `git submodule update --init --recursive` | Pull submodules |
| `git subtree add --prefix=dir <url> main` | Merge repo into a subdirectory |
git submodule add https://github.com/user/shared-utils.git lib/shared
git submodule update --init --recursive
git subtree add --prefix=vendor/ui https://github.com/user/ui-kit.git main
10. Debugging & bisecting
| Command | One-line description |
| -------------------------- | ----------------------------- |
| `git bisect start` | Begin binary search for a bug |
| `git bisect good <commit>` | Mark commit as good |
| `git bisect bad <commit>` | Mark commit as bad |
| `git bisect reset` | Exit bisect mode |
git bisect start
git bisect bad HEAD
git bisect good v1.0
# Git checks out intermediate commits—test each and mark good/bad
git bisect reset # Exit bisect
11. House-keeping & server admin
| Command | One-line description | |
| --------------------------------------------------- | -------------------------------- | --------------- |
| `git gc` | Garbage-collect loose objects | |
| `git fsck` | Sanity-check repository | |
| `git clean -fd` | Remove untracked files & dirs | |
| \`git archive --format=tar v1.0 | gzip > v1.0.tar.gz\` | Export snapshot |
| `git bundle create full.bundle --all` | Offline transferable backup | |
| `git daemon --reuseaddr --base-path=. --export-all` | Serve repo read-only over git:// | |
git gc # Clean up repo
git fsck # Check for corruption
git clean -fd # Remove untracked files & dirs
git archive --format=tar v1.0 | gzip > v1.0.tar.gz
git bundle create repo.bundle --all
git daemon --reuseaddr --base-path=. --export-all
Comments
Post a Comment