Git CLI Cheat sheet
Git is the backbone of modern version control. While GUIs are nice, mastering the CLI gives you speed and the ability to fix complex “how did I break this?” situations.
—
⚙️ Configuration & Setup
Before you start committing, make sure Git knows who you are.
| Command | Description |
|---|---|
| git config --global user.name “Name” | Set your identifier name globally. |
| git config --global user.email “email@example.com” | Set your identifier email globally. |
| git init | Initialize a new local Git repository in the current folder. |
| git clone <url> | Download an existing repository from a remote source. |
—
🔄 The Development Lifecycle
This is the core “Edit → Stage → Commit” loop.
- git status: See which files are modified, staged, or untracked. Run this often.
- git add <file>: Add a specific file to the staging area.
- git add .: Add all changed files to the staging area.
- git commit -m “Message”: Create a snapshot of your staged changes with a descriptive note.
- git commit --amend: Add changes to the previous commit or fix the last commit message.
—
🌿 Branching & Merging
Branches allow you to work on features in isolation without breaking the “main” code.
- git branch: List all local branches.
- git checkout -b <branch-name>: Create a new branch and switch to it immediately.
- git switch <branch-name>: Move from your current branch to another one.
- git merge <branch-name>: Merge the specified branch into your current branch.
- git branch -d <branch-name>: Delete a branch that has been merged.
—
🌐 Remote Synchronization
Commands for interacting with platforms like GitHub, GitLab, or Bitbucket.
| Command | Description |
|---|---|
| git remote add origin <url> | Link your local repo to a remote server. |
| git pull | Fetch changes from the remote and merge them into your current branch. |
| git push origin <branch> | Upload your local commits to the remote repository. |
| git fetch | Download info from the remote without merging it (useful for “checking” updates). |
—
🛠️ Inspection & Undo
For when things go wrong (or you just need to remember what you did).
- git log --oneline: Show a condensed history of your commits.
- git diff: Show exactly what lines changed in your files but haven’t been staged yet.
- git checkout -- <file>: Discard changes in a specific file and revert it to the last commit state.
- git reset --soft HEAD~1: “Undo” the last commit but keep your changes in the staging area.
- git reset --hard HEAD~1: Warning: Completely nukes the last commit and all changes associated with it.
- git stash: Temporarily “hide” your uncommitted changes so you can switch branches quickly. Use git stash pop to bring them back.
Pro Tip: If you ever get stuck in a “Vim” screen (the scary text editor that opens for commit messages), type :q! and hit Enter to exit without saving, or :wq to save and exit.