iCapSolutions

Cloud Computing – DevSecOps – IT Services

Git Cheat Sheet

git cheat sheet — version control commands quick reference

Git Cheat Sheet: Essential Commands for Daily Use

Git is the free and open-source distributed version control system at the heart of GitHub and most modern development workflows. This git cheat sheet covers the most important and commonly used Git commands for easy reference. Bookmark this git cheat sheet for quick access during your daily development workflow. Commands are grouped by task, and each block is copy-paste ready: replace anything in [brackets] with your own value.

Most-Used Git Commands at a Glance

TaskCommand
See what changedgit status
Stage everythinggit add .
Commitgit commit -m "message"
Create and switch to a branchgit switch -c [branch]
Get the latest changesgit pull
Publish a branchgit push -u origin [branch]
Undo unstaged edits to a filegit restore [file]
Fix the last commit messagegit commit --amend
Find a “lost” commitgit reflog

Setup

Configure user information used across all local repositories.

# Set your name for commit history
git config --global user.name "Firstname Lastname"

# Set your email for commit history
git config --global user.email "you@example.com"

# Enable colorized output
git config --global color.ui auto

# Cache credentials (stored in plaintext — use a credential manager in production)
git config --global credential.helper store

Initialize a Repository

# Initialize a new local repo
git init

# Clone an existing repo
git clone https://github.com/user/repo.git

Create a New Repo on GitHub via CLI

echo "# my-project" >> README.md
git init
git add .
git commit -m "init commit"
git branch -M main

# Create the remote repo via GitHub API
curl -u USER:TOKEN https://api.github.com/user/repos \
  -d '{"name":"PROJECTNAME","description":"Project description","private":true}'

git remote add origin https://github.com/USER/PROJECTNAME.git
git push -u origin main

Staging & Committing

# Show modified files in working directory
git status

# Stage a specific file
git add [file]

# Stage all changes
git add .

# Unstage a file (keep changes in working directory)
git reset [file]

# Show unstaged changes
git diff

# Show staged (not yet committed) changes
git diff --staged

# Commit staged changes
git commit -m "your message"

Branching & Merging

# List all branches (* = current branch)
git branch

# Create a new branch
git branch [branch-name]

# Switch to a branch
git checkout [branch-name]

# Create and switch in one step
git checkout -b [branch-name]

# Merge a branch into the current branch
git merge [branch]

# Delete a branch
git branch -d [branch-name]

Remote Operations

# Add a remote
git remote add origin https://github.com/user/repo.git

# List remotes
git remote -v

# Fetch all branches from remote (no merge)
git fetch origin

# Fetch and merge from tracking branch
git pull

# Push local branch to remote
git push origin [branch]

# Merge a remote branch into current branch
git merge origin/[branch]

Inspect & Compare

# Show commit history for current branch
git log

# Show commits on branchA not on branchB
git log branchB..branchA

# Show commits that changed a file (even across renames)
git log --follow [file]

# Show diff between two branches
git diff branchB...branchA

# Show any Git object in human-readable format
git show [SHA]

Switch and Restore (Modern Git)

Git 2.23 split the overloaded git checkout into two clearer commands. Both are safe to use in place of checkout for everyday work:

# Switch to an existing branch
git switch [branch-name]

# Create a branch and switch to it
git switch -c [branch-name]

# Discard unstaged changes to a file
git restore [file]

# Unstage a file but keep the changes
git restore --staged [file]

Undoing Mistakes

Pick the command by where the mistake is: in your working directory, in the last commit, or already pushed.

# Change the last commit's message or add a forgotten file (before pushing)
git add [forgotten-file]
git commit --amend

# Undo the last commit but keep its changes staged
git reset --soft HEAD~1

# Undo a commit that is already pushed, by adding a new "reverse" commit
git revert [SHA]

# Find commits that seem lost after a reset or rebase
git reflog

# Bring a lost commit back as a new branch
git branch recovered [SHA]

Use git revert on shared branches. git reset rewrites history and causes problems for anyone who has already pulled.

Tags and Releases

# Create an annotated tag for a release
git tag -a v1.4.0 -m "Release 1.4.0"

# List tags
git tag

# Push tags to the remote
git push origin --tags

Cherry-Pick a Commit

# Apply one commit from another branch onto the current branch
git cherry-pick [SHA]

Tracking Path Changes

# Delete a file and stage the removal
git rm [file]

# Rename/move a file and stage the change
git mv [old-path] [new-path]

# Show commit log with path changes indicated
git log --stat -M

Rewriting History

# Rebase current branch ahead of another
git rebase [branch]

# Reset staging area and working tree to a specific commit (destructive)
git reset --hard [commit]

Temporary Commits (Stash)

# Save modified and staged changes to stash
git stash

# List all stash entries
git stash list

# Apply the top stash entry and remove it from the stack
git stash pop

# Discard the top stash entry without applying
git stash drop

Ignoring Files

Create a .gitignore file in your repo root with patterns to exclude:

logs/
*.notes
temp-*/
# Set a global gitignore for all local repos
git config --global core.excludesfile ~/.gitignore_global

Handy Git Aliases

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.lg "log --oneline --graph --decorate --all"

With these set, git lg shows a compact graph of every branch, which is the fastest way to see how branches relate before a merge or rebase. For a full branching model across dev, staging and production, see our Git merge strategy, and the official Git reference documentation covers every option in detail.

For shell/terminal setup tips to pair with these git commands, see our Bashrc Cheat Sheet.