Git Command-Line Reference Guide¶
A structured, detailed, cross-platform command-line reference for Git operations. Every command below is in its own isolated block so you can copy it with a single click.
0. Command-Line Syntax Conventions¶
When reading command-line documentation, replace any <placeholder> (including the angle brackets) with your actual file name, branch name, or URL:
<placeholder>— Required argument (e.g., replace<branch>withmain).[optional]— Optional argument or flag.HEAD— Points to the currently checked-out branch and its latest commit.HEAD~1— Points to $1$ commit beforeHEAD(HEAD~2is $2$ commits back, etc.).origin— The default name for your remote repository (e.g., on GitHub or GitLab).
1. Configuration & Authentication (git config)¶
Check Installed Git Version¶
Set Your Global Username¶
(Attached to every commit you make)
Set Your Global Email Address¶
(Must match the email on your GitHub/GitLab account)
Set Default Branch Name to main¶
Set Default Text Editor to VS Code¶
(Prevents getting trapped in Vim during commits or merges)
Set Default Text Editor to Nano (Simple Terminal Editor)¶
View All Active Configuration Settings¶
Authenticate via GitHub CLI (Recommended)¶
Generate a New SSH Key Pair¶
2. Repository Initialization (init, clone)¶
Initialize a New Local Repository in the Current Folder¶
Clone a Remote Repository via HTTPS¶
Clone a Remote Repository via SSH¶
Clone a Remote Repository into a Custom Folder Name¶
Shallow Clone (Download Only the Latest Commit for Large Repos)¶
3. Tracking, Staging & Committing (status, add, commit)¶
Check Full Repository Status¶
(Shows untracked, modified, and staged files)
Check Compact Repository Status¶
(M = modified, A = added, ?? = untracked)
Stage a Specific File¶
Stage All Files (New, Modified, and Deleted) Across the Entire Repo¶
Stage All Files in the Current Directory and Subdirectories¶
Stage Only Modified and Deleted Files (Ignores New Untracked Files)¶
Interactively Stage Specific Chunks/Lines Inside a File¶
(Press y to stage chunk, n to skip chunk, q to quit)
Commit Staged Changes with an Inline Message¶
Stage All Tracked, Modified Files and Commit in One Step¶
(Note: Does not include brand-new untracked files)
Edit the Previous Commit Message¶
(Only do this before pushing to a shared remote branch)
Add Newly Staged Files into the Previous Commit Without Changing Its Message¶
4. Inspection, Diffs & History (diff, log, show, blame)¶
(Note: If the terminal shows : or (END) at the bottom, press q to exit back to the command prompt).
View Unstaged Changes (Working Directory vs. Staging Area)¶
View Staged Changes About to Be Committed (Staging Area vs. Last Commit)¶
View All Uncommitted Changes (Both Staged and Unstaged)¶
Compare Differences Between Two Branches¶
View Summary of Changed Files (Insertions/Deletions Count Only)¶
View Standard Commit History¶
View Compact One-Line Commit History¶
View Visual Branch Graph of All Commits Across All Branches¶
View Only the Last 5 Commits¶
View Commit History Along with Full Code Diffs¶
Inspect a Specific Commit's Metadata and Code Changes¶
See Who Last Modified Each Line of a File¶
5. Branching & Merging (branch, switch, merge)¶
List All Local Branches¶
(* indicates the branch you are currently on)
List Both Local and Remote Branches¶
Create a New Branch and Switch to It Immediately¶
Switch to an Existing Branch¶
Switch Back to the Previously Checked-Out Branch¶
Legacy Command: Create and Switch to a New Branch¶
Legacy Command: Switch to an Existing Branch¶
Rename the Currently Checked-Out Branch¶
Safely Delete a Local Branch (Only Works if Already Merged)¶
Force Delete a Local Branch (Deletes Even if Unmerged)¶
Merge Another Branch into Your Currently Active Branch¶
(First run git switch main, then merge your feature branch into it)
Abort an Active Merge (Return Everything to Pre-Merge State)¶
Complete a Merge After Manually Fixing Conflict Markers and Running git add¶
6. Remote Synchronization (remote, fetch, pull, push)¶
View Configured Remote URLs¶
Link a Local Repository to a Remote Repository (origin)¶
Change an Existing Remote URL¶
Download Remote History Without Modifying Your Local Working Files¶
(Also cleans up deleted remote branches)
Download Remote Changes and Merge Them into Your Current Branch¶
Download Remote Changes and Rebase Your Local Commits on Top of Them¶
(Keeps commit history straight and clean without extra merge commits)
Push a New Local Branch to Remote for the First Time (-u Links Them)¶
Push Commits on an Already-Linked Branch¶
Safely Force Push After Amending or Rebasing¶
(Refuses to overwrite if a teammate pushed new code you haven't fetched yet)
Delete a Branch on the Remote Server¶
7. Stashing Work (git stash)¶
Use stash when you need to switch branches immediately without committing half-finished work.
Stash All Modified Tracked Files¶
Stash Modified AND Untracked Files with a Descriptive Note¶
List All Saved Stashes¶
Restore the Most Recent Stash and Remove It from the Stash List¶
Restore a Specific Stash Without Removing It from the Stash List¶
Delete the Most Recent Stash¶
Delete All Stashes Permanently¶
8. Undoing, Restoring & Resetting (restore, reset, revert, clean, reflog)¶
Unstage a File (Keep Your File Edits, Just Remove from git add)¶
Discard Local Unstaged Edits in a File (Destructive: Reverts File to Last Commit)¶
Restore a File to Match a Specific Older Commit¶
Stop Tracking a File in Git Without Deleting It from Your Computer¶
(Useful when you accidentally committed a .env file)
Undo Last Commit, Keep All Changes Staged¶
(Moves HEAD back 1 commit; files stay green/staged)
Undo Last Commit, Keep All Changes Unstaged¶
(Moves HEAD back 1 commit; files stay modified in your working folder)
Destroy Last Commit AND Permanently Wipe All Uncommitted Changes¶
(Warning: Completely deletes uncommitted work)
Safely Undo a Older Commit on a Shared Branch¶
(Creates a brand-new commit that reverses the target commit without rewriting history)
Preview Which Untracked Files Would Be Deleted by git clean¶
Permanently Delete All Untracked Files and Folders¶
View the Recovery Ledger of Every Action You Have Taken Locally¶
(Use this to find the <commit-hash> of accidentally deleted commits or branches)
Recover a Lost Commit or Branch Found in git reflog¶
9. Quick Troubleshooting & Emergency Commands¶
Committed to the Wrong Branch by Mistake (Before Pushing)¶
Run these three commands in order to move the commit to a new branch and rewind the current branch:
Force Git to Recognize a File Capitalization Rename (file.js to File.js)¶
Escape the Vim Terminal Editor¶
If your terminal opens a screen full of ~ lines after git commit or git merge, press the Escape key, type the following characters, and press Enter: