Git Cheatsheet
Complete reference guide for all Git commands and workflows
Setup & Configuration
Set User Name
Sets the name that will be attached to your commits.
Set User Email
Sets the email that will be attached to your commits.
Set Default Editor
Sets VS Code as the default editor for Git messages.
View Configuration
Lists all Git configuration settings.
Set Credential Helper
Caches credentials in memory for a short time (default 15 minutes).
Set Auto CRLF Handling
Automatically handles line endings (Windows). Use input for Linux/Mac.
Basic Commands
Initialize Repository
Creates a new Git repository in the current directory.
Clone Repository
Creates a local copy of a remote repository.
Add Files to Staging
Adds a specific file to the staging area. Use . to add all files.
Commit Changes
Records changes to the repository with a descriptive message.
View Status
Shows the state of the working directory and staging area.
Restore File
Discards changes in working directory (Git 2.23+).
View Commit History
Shows commit history in a compact, graphical format.
View Changes
Shows unstaged changes since last commit.
Branching
List Branches
Lists all local branches. Add -a to show all branches including remote.
Create Branch
Creates a new branch from the current HEAD.
Switch Branch
Switches to the specified branch.
Create & Switch Branch
Creates a new branch and switches to it immediately.
Delete Branch
Deletes the specified branch (safe delete). Use -D for force delete.
Rename Branch
Renames the current branch.
Track Remote Branch
Sets up tracking relationship with a remote branch.
Merging & Rebasing
Merge Branch
Merges the specified branch into the current branch.
Rebase Branch
Reapplies commits on top of another base branch.
Abort Rebase
Cancels an ongoing rebase operation.
Continue Rebase
Continues a rebase after resolving conflicts.
Squash Commits
Interactive rebase to squash last n commits.
Abort Merge
Cancels an ongoing merge operation.
Remote Repositories
List Remotes
Lists all remote repositories with their URLs.
Add Remote
Adds a new remote repository with the name "origin".
Fetch from Remote
Downloads objects and refs from remote repository without merging.
Pull from Remote
Fetches and merges changes from a remote branch.
Push to Remote
Uploads local branch commits to the remote repository.
Remove Remote
Removes the remote named "origin".
Update Remote URL
Changes the URL for the remote named "origin".
Track Remote Branch
Creates a local branch that tracks a remote branch.
Stashing
Stash Changes
Temporarily stores modified tracked files.
Stash with Message
Stashes changes with a descriptive message.
List Stashes
Lists all stashed changesets.
Apply Stash
Applies the most recent stash without removing it from stash list.
Pop Stash
Applies and removes the most recent stash from the stash list.
Drop Stash
Deletes the stash at index n.
Clear Stash
Removes all stashed entries.
Inspection & Comparison
View Commit Log
Shows the last 10 commits in a compact format.
View Changes Between Commits
Shows changes between two commits.
Show Commit Details
Shows information about a specific commit.
View Branch History
Shows a graphical representation of branch history.
Search Commit Messages
Filters commits by message content.
View Commits by Author
Shows commits by a specific author.
View File History
Shows commit history with changes for a specific file.
View Commits by Date
Shows commits from the last 2 weeks.
Advanced Commands
Undo Last Commit
Undoes the last commit but keeps changes staged.
Hard Reset
Completely removes the last commit and all changes.
Revert Commit
Creates a new commit that undoes the changes of a previous commit.
Find by Hash
Shows information about a specific commit using its hash.
Create Tag
Creates an annotated tag for a release point.
List Tags
Lists all tags in the repository.
Clean Untracked Files
Removes untracked files and directories. Use with caution!
Bisect for Debugging
git bisect bad
git bisect good <commit>
Binary search to find the commit that introduced a bug.