Quick-reference for the most essential Git commands. Placeholders like <name> should be replaced with your actual values.
| git config --global user.name "<name>" |
Set your name. Git tags every commit with this. Do this once after installing. |
| git config --global user.email "<email>" |
Set your email. Should match your GitHub/GitLab account. |
| git init | Create a new repo. Turns the current folder into a Git-tracked project. |
| git clone <url> | Download an existing repo. Creates a local copy including all history. |
| git status | See what's changed. Shows modified, staged, and untracked files. Run this often. |
| git add <file> | Stage a specific file. Marks it to be included in the next commit. |
| git add . | Stage everything. Stages all changes in the current folder and subfolders. |
| git commit -m "<msg>" | Save a snapshot. Records staged changes with a description. Use present tense: "Add login page". |
| git commit --amend | Fix the last commit. Edit the message or add forgotten files â before pushing. |
| git branch | List all branches. The current branch is marked with *. |
| git branch <name> | Create a branch. Makes a new branch but doesn't switch to it. |
| git switch <name> | Switch branches. Moves your working directory to that branch. |
| git switch -c <name> | Create & switch. Shortcut to make a new branch and jump to it immediately. |
| git merge <branch> | Merge a branch. Brings commits from another branch into your current one. |
| git branch -d <name> | Delete a branch. Safe delete â only works if already merged. |
| git remote -v | List remotes. Shows the URL(s) your local repo is connected to. |
| git remote add origin <url> | Connect to remote. Links your local repo to a remote for the first time. |
| git push | Upload commits. Sends your local commits to the remote repository. |
| git push -u origin <branch> | Push a new branch. Creates the branch on the remote and sets tracking. |
| git pull | Download & merge. Fetches remote changes and merges them into your branch. |
| git fetch | Download only. Gets remote changes but doesn't merge â lets you review first. |
| git restore <file> | Discard file changes. Reverts a file to how it was at the last commit. Unsaved edits are lost. |
| git restore --staged <file> | Unstage a file. Removes from staging area but keeps your edits in the working directory. |
| git revert <hash> | Undo a commit safely. Creates a new commit that reverses an old one. Safe to use on shared branches. |
| git reset --soft HEAD~1 | Undo last commit (keep changes staged). Moves the pointer back but keeps your work ready to recommit. |
| git reset --hard HEAD~1 â destructive | Erase last commit & changes. Permanently discards the commit and all its changes. Cannot be undone. |
| git log | View commit history. Shows all commits, newest first. Press q to quit. |
| git log --oneline | Compact history. One line per commit â great for a quick overview. |
| git log --oneline --graph | Visual branch history. Shows a text-art diagram of how branches diverge and merge. |
| git diff | See unstaged changes. Shows exactly what lines changed in your working directory. |
| git diff --staged | See staged changes. Shows what will be included in your next commit. |
| git show <hash> | Inspect a commit. Shows the changes made in a specific commit. |
| git stash | Shelve changes temporarily. Saves your dirty working state so you can switch tasks cleanly. |
| git stash pop | Restore stashed changes. Brings back what you stashed and removes it from the stash list. |
| git cherry-pick <hash> | Copy one commit. Applies a single commit from another branch onto your current branch. |
| git rebase <branch> | Replay commits on top of another. Keeps history clean and linear. Don't use on shared branches. |
| git tag <name> | Mark a version. Creates a named label (e.g. v1.0) on a commit for releases. |
| .gitignore | Exclude files from tracking. A text file listing patterns Git should ignore (e.g. node_modules/, *.env). |
Use the present tense and be specific: "Fix login redirect bug" beats "fix stuff". Your future self will thank you when reading history.
Always work on a new branch for each feature or fix. Never commit directly to main. Branches are cheap â use them freely.
Commands marked destructive can't be undone. When in doubt, use git revert instead of reset --hard â it's safer on shared repos.