Skip to content

Quick Reference

Fast lookup for common Git commands. For detailed explanations, click on any command.

Offline Access

Sync and download for local use.


Command Categories

Jump to: SetupCreateSnapshotBranchShareInspectPatchDebugAdmin


Setup and Config

CommandDescription
git config --global user.name "name"Set the name attached to commits
git config --global user.email "email"Set the email attached to commits
git config --global color.ui autoEnable colorization of command output
git config --listList all configured settings
git help <command>Get help for a Git command

Getting and Creating Projects

CommandDescription
git initInitialize a new Git repository
git init <directory>Create a new local repository in specified directory
git clone <url>Clone a repository from a remote source
git clone <url> <directory>Clone a repository into a specific directory

Basic Snapshotting

CommandDescription
git statusShow the status of the working directory
git add <file>Add a file to the staging area
git add .Add all new and changed files to staging
git add -pInteractive staging - choose hunks to add
git diffShow unstaged changes
git diff --stagedShow staged changes
git commit -m "message"Commit staged changes with a message
git commit -am "message"Stage all tracked files and commit
git commit --amendAmend the last commit
git restore <file>Discard changes in working directory
git restore --staged <file>Unstage a file
git reset <file>Unstage a file (keep changes)
git reset --hard HEADDiscard all local changes
git rm <file>Remove file from working directory and staging
git mv <old> <new>Rename or move a file

Branching and Merging

CommandDescription
git branchList all local branches
git branch -aList all branches (local and remote)
git branch <branch>Create a new branch
git branch -d <branch>Delete a branch
git branch -m <new-name>Rename current branch
git checkout <branch>Switch to a branch
git checkout -b <branch>Create and switch to new branch
git switch <branch>Switch to a branch (modern)
git switch -c <branch>Create and switch to new branch (modern)
git merge <branch>Merge a branch into current branch
git merge --no-ff <branch>Merge with a merge commit
git merge --abortAbort a merge in progress
git logShow commit history
git log --onelineShow compact commit history
git log --graph --allShow branch graph
git stashTemporarily save uncommitted changes
git stash popApply and remove most recent stash
git stash listList all stashes
git tag <name>Create a lightweight tag
git tag -a <name> -m "msg"Create an annotated tag

Sharing and Updating Projects

CommandDescription
git remote -vList all configured remotes
git remote add <name> <url>Add a new remote
git remote remove <name>Remove a remote
git fetchDownload objects and refs from remote
git fetch <remote>Fetch from specific remote
git pullFetch and merge from remote
git pull --rebaseFetch and rebase from remote
git pushPush changes to remote
git push <remote> <branch>Push branch to remote
git push -u <remote> <branch>Push and set upstream
git push --force-with-leaseSafely force push
git push --tagsPush all tags to remote

Inspection and Comparison

CommandDescription
git show <commit>Show changes of a commit
git show HEADShow last commit
git logShow commit history
git log --follow <file>Show commit history for file
git log --author="name"Filter commits by author
git log --grep="pattern"Search commit messages
git log --since="2 weeks ago"Filter by date
git diff <branch1>..<branch2>Compare two branches
git diff <commit1> <commit2>Compare two commits
git shortlogSummarized commit history

Patching

CommandDescription
git cherry-pick <commit>Apply a commit to current branch
git cherry-pick --abortAbort cherry-pick
git rebase <branch>Rebase current branch onto another
git rebase -i HEAD~<n>Interactive rebase last n commits
git rebase --continueContinue after resolving conflicts
git rebase --abortAbort rebase
git revert <commit>Create new commit that undoes a commit
git revert --no-commit <commit>Revert without auto-committing

Debugging

CommandDescription
git bisect startStart binary search for bad commit
git bisect badMark current commit as bad
git bisect good <commit>Mark commit as good
git bisect resetEnd bisect session
git blame <file>Show who changed each line
git blame -L 10,20 <file>Blame specific line range
git grep <pattern>Search for pattern in tracked files

Administration

CommandDescription
git clean -nShow what would be deleted
git clean -fDelete untracked files
git clean -fdDelete untracked files and directories
git gcCleanup and optimize local repository
git fsckVerify database integrity
git reflogShow reference log (recovery tool)
git reflog show <branch>Show reflog for specific branch

Common Workflows

Start a new project

bash
git init
git add .
git commit -m "Initial commit"
git remote add origin <url>
git push -u origin main

Clone and work on existing project

bash
git clone <url>
cd <project>
git checkout -b feature/my-feature
# Make changes
git add .
git commit -m "Add feature"
git push -u origin feature/my-feature

Update local repository

bash
git fetch origin
git pull origin main
# or
git pull --rebase origin main

Fix last commit

bash
# Forgot to add file
git add forgotten-file.txt
git commit --amend --no-edit

# Fix commit message
git commit --amend -m "New message"

Undo changes

bash
# Undo unstaged changes
git restore <file>

# Undo staged changes
git restore --staged <file>

# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1

Resolve merge conflicts

bash
git merge feature-branch
# Conflicts occur
# Edit files to resolve conflicts
git add <resolved-files>
git commit

Git Cheat Sheet (One Page)

Setup

bash
git config --global user.name "Name"
git config --global user.email "email@example.com"

Create

bash
git init
git clone <url>

Local Changes

bash
git status
git diff
git add .
git commit -m "Message"

Branch

bash
git branch
git checkout -b <branch>
git merge <branch>

Remote

bash
git remote -v
git fetch
git pull
git push

History

bash
git log
git log --oneline --graph
git show <commit>

Undo

bash
git restore <file>
git reset --soft HEAD~1
git revert <commit>

See Also


Tip: Bookmark this page for quick reference. Use Ctrl+F / Cmd+F to search for specific commands.

Released under the MIT License.