Skip to content

eon01/GitCheatSheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

The Everyday Git Cheat Sheet

This cheatsheet covers the most common Git commands for everyday use. It was originally created for my book Learn Git in a Day - The Visual Guide

Setup

Command What It Does
git --version Check which version of Git is installed
git config --global user.name "Your Name" Set your name (attached to every commit)
git config --global user.email "you@example.com" Set your email
git config --global init.defaultBranch main Use main as the default branch name
git config --list Show all current settings

Starting a Project

Command What It Does
git init Turn the current folder into a Git repository
git clone <url> Download a full copy of a remote repository

Day-to-Day Workflow

Command What It Does
git status See what's changed, staged, or untracked
git add <file> Stage a specific file for the next commit
git add . Stage all changed files at once
git commit -m "message" Save a snapshot with a one-line message
git commit Save a snapshot and open an editor for a longer message
git diff Show unstaged changes line by line
git diff --staged Show staged changes (what the next commit will contain)

Viewing History

Command What It Does
git log Full commit history (hash, author, date, message)
git log --oneline Compact history (hash and message only)
git log --oneline --graph --all Visual branch history with graph lines
git log --oneline -<n> Show only the last n commits
git show <hash> View a specific commit's message and diff
git blame <file> Show who last changed each line and when
git blame -L 10,20 <file> Blame a specific range of lines
git log --author="Name" Filter history by author
git log --grep="keyword" Search commit messages for a keyword
git log -p <file> Show the full change history of a single file
git reflog List everywhere HEAD has pointed - your safety net for "lost" commits

Undoing Things

Command What It Does
git restore <file> Discard uncommitted changes (back to last commit)
git restore --staged <file> Unstage a file (keep the changes, just remove from staging)
git revert HEAD Create a new commit that reverses the most recent one
git revert <hash> Create a new commit that reverses a specific older commit
git commit --amend -m "new message" Replace the last commit's message
git commit --amend --no-edit Add forgotten staged files to the last commit
git reset --soft HEAD~1 Undo the last commit, keep changes staged
git reset HEAD~1 Undo the last commit, keep changes unstaged (default --mixed)
git reset --hard HEAD~1 Undo the last commit and discard its changes entirely
git clean -n Preview which untracked files would be deleted
git clean -fd Delete untracked files and directories

Warning: git restore, git reset --hard, and git clean -fd permanently discard changes. git commit --amend and git reset rewrite history - only use them before pushing.

Stashing

Command What It Does
git stash Save uncommitted work aside and clean the working directory
git stash pop Bring stashed changes back and remove them from the stash
git stash apply Bring stashed changes back but keep them in the stash

Branches

Command What It Does
git branch List all local branches
git branch <name> Create a new branch (stay on current one)
git switch <name> Switch to an existing branch
git switch -c <name> Create a new branch and switch to it
git merge <name> Merge a branch into the current one
git branch -d <name> Delete a branch (only if fully merged)
git branch -D <name> Force-delete a branch (even if unmerged)
git branch --merged List branches already merged into the current one

Note: git switch and git restore were introduced in Git 2.23 (2019) to split the old git checkout into clearer commands. On older versions, use git checkout <name> / git checkout -b <name> instead.

Remotes

Command What It Does
git remote add origin <url> Connect your repo to a remote server
git remote -v Show configured remotes
git push -u origin main Push and set the upstream (first time only)
git push Push commits to the upstream remote
git push origin <branch> Push a specific branch
git pull Download and merge remote changes
git pull --rebase Download and rebase local commits on top
git fetch Download remote changes without merging
git fetch --prune Fetch and clean up deleted remote branches

Rebase

Command What It Does
git rebase main Replay your branch's commits on top of main
git rebase -i HEAD~<n> Interactively reword, squash, drop, or reorder the last n commits
git rebase --continue Continue after resolving a conflict
git rebase --abort Cancel the rebase and restore the original state
git config --global pull.rebase true Make rebase the default for every pull

The rule: rebase before you push, merge after you push. Never rebase commits others have already pulled.

Cherry-Pick

Command What It Does
git cherry-pick <hash> Apply a single commit from another branch onto the current one
git cherry-pick <hash1> <hash2> Apply several specific commits
git cherry-pick <hash1>..<hash2> Apply a range of commits
git cherry-pick --continue Continue after resolving a conflict
git cherry-pick --abort Cancel and restore the original state

Tags

Command What It Does
git tag <name> Create a lightweight tag on the current commit
git tag -a <name> -m "message" Create an annotated tag with a description
git tag -a <name> <hash> -m "msg" Tag an older commit
git tag List all tags
git tag -l "v1.*" List tags matching a pattern
git show <tag> View tag details and the commit it points to
git push origin <tag> Push a specific tag to the remote
git push origin --tags Push all tags
git tag -d <tag> Delete a tag locally
git push origin --delete <tag> Delete a tag from the remote
git checkout <tag> View the project at a tagged version (detached HEAD)

Ignoring Files

A .gitignore file tells Git which files to skip. Common entries for Python:

# example .gitignore for Python projects
# Note: comments must be on their own line; Git does not support inline comments.

# Compiled bytecode
__pycache__/
*.pyc

# Virtual environment
venv/

# Secrets and API keys
.env

# Editor settings
.vscode/
.idea/
Pattern Matches
*.pyc Any file ending in .pyc
__pycache__/ The folder and everything inside it
.env That exact filename
!important.log Exception - do not ignore this file
Command What It Does
git rm --cached <file> Stop tracking a file already committed (keeps it on disk)
git config --global core.excludesfile ~/.gitignore_global Set a personal ignore file for all repos

Use github/gitignore for ready-made templates for different languages and frameworks.

SSH Authentication

Command What It Does
ssh-keygen -t ed25519 -C "you@example.com" Generate a new SSH key pair
cat ~/.ssh/id_ed25519.pub Display your public key (add it to your GitHub/GitLab account under Settings → SSH keys)
ssh -T git@github.com Test the SSH connection

Aliases

Setup Command Shortcut
git config --global alias.st "status" git st
git config --global alias.co "switch" git co
git config --global alias.br "branch" git br
git config --global alias.cm "commit -m" git cm "message"
git config --global alias.lg "log --oneline --graph --all" git lg

To list all aliases: git config --global --get-regexp alias

Merge Conflicts

When two branches change the same lines, Git marks the conflict in the file:

<<<<<<< HEAD
your version of the code
=======
the other branch's version
>>>>>>> feature/branch

To resolve: edit the file to keep what you want, remove the conflict markers, then stage and commit:

git add <file>
git commit -m "Resolve merge conflict"

When Things Go Wrong

Situation Command
I want to undo my uncommitted edits git restore <file>
I staged a file by mistake git restore --staged <file>
I want to throw away all uncommitted changes git restore . (add git clean -fd for untracked files)
I deleted a file and committed, and want it back git restore --source=<hash> <file>
I committed something wrong git revert HEAD
I want to undo my last commit but keep the changes git reset --soft HEAD~1
I think I lost a commit git reflog → find the hash → git checkout <hash>
I want one commit from another branch git cherry-pick <hash>
I have messy untracked files to clear out git clean -n (preview) → git clean -fd
I want to clean up my commits before pushing git rebase -i HEAD~<n>
I need to pause my work git stash → do other work → git stash pop
I made a typo in my last commit message git commit --amend -m "fixed message"
I forgot to add a file to my last commit git add <file>git commit --amend --no-edit
My branch is behind main git fetch then git rebase origin/main (before pushing) or git merge origin/main (after pushing)
I want to abandon a rebase git rebase --abort
I resolved conflicts mid-rebase or cherry-pick git add <file>git rebase --continue (or git cherry-pick --continue)
I'm in detached HEAD git switch main
I made commits in detached HEAD and want to keep them git switch -c <new-branch> (before switching away)
I want to nuke my last commit entirely git reset --hard HEAD~1
I committed on the wrong branch git switch <right-branch>git cherry-pick <hash> → remove it from the wrong branch
I deleted a branch and need it back git reflog → find the hash → git branch <name> <hash>
I hard-reset and lost commits git reflog → find the hash → git reset --hard <hash>
A merge went sideways git merge --abort
My pull created a mess git reset --hard ORIG_HEAD
I rewrote pushed history and need to push git push --force-with-lease (never bare --force)
Git keeps tracking a file that's in .gitignore git rm --cached <file> → commit
I committed a secret Rotate the secret first → rewrite history with git filter-repo
I want to undo a merge commit git revert -m 1 <merge-hash>
I stashed something and lost it git stash listgit stash apply stash@{n}
I need to find which commit broke things git bisect startgit bisect badgit bisect good <hash>
Who wrote this line and why git blame <file>git show <hash>
My branch name is wrong git branch -m <new-name>
Phantom diffs from line endings git config --global core.autocrlf input (Linux/macOS) or true (Windows)

--

More Resources

About

This cheatsheet covers the most common Git commands for everyday use. It was originally created for my book "Learn Git in a Day - The Visual Guide"

Topics

Resources

Stars

Watchers

Forks

Contributors