You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 environmentvenv/
# 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)