Git Notes
Having a quick reference of essential Git commands at your fingertips accelerates your workflow and prevents the frustration of searching for syntax during...
Having a quick reference of essential Git commands at your fingertips accelerates your workflow and prevents the frustration of searching for syntax during critical moments. This cheatsheet covers the commands you will use daily along with their most common options, organized by workflow stage. Bookmark this page and refer to it until these commands become muscle memory.
Configuration Commands
Set up your identity and preferences before anything else:
# Set your identity (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Set default editor
git config --global core.editor "code --wait"
# Enable colored output
git config --global color.ui auto
# Set pull behavior
git config --global pull.rebase true
# View all configuration
git config --list
git config --global --listRepository Setup
# Initialize a new repository
git init
git init my-project # Create directory and init
# Clone an existing repository
git clone <url>
git clone <url> folder-name # Clone into specific folder
git clone --depth 1 <url> # Shallow clone (latest commit only)
git clone --branch dev <url> # Clone specific branchDaily Workflow Commands
# Check status (most-used command)
git status
git status -s # Short format
# Stage changes
git add file.txt # Stage specific file
git add . # Stage all changes
git add -p # Stage interactively (hunk by hunk)
git add *.js # Stage by pattern
# Commit
git commit -m "message" # Commit with message
git commit -am "message" # Stage tracked files and commit
git commit --amend # Modify last commit
git commit --amend --no-edit # Amend without changing message
# View differences
git diff # Unstaged changes
git diff --staged # Staged changes
git diff main..feature # Between branches
git diff HEAD~3 # Last 3 commitsBranch Operations
# List branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
git branch -v # With last commit info
# Create branches
git branch feature-name # Create branch
git checkout -b feature-name # Create and switch
git switch -c feature-name # Create and switch (modern)
# Switch branches
git checkout branch-name
git switch branch-name # Modern alternative
# Delete branches
git branch -d branch-name # Safe delete (merged only)
git branch -D branch-name # Force delete
# Rename branch
git branch -m new-name # Rename current branch
git branch -m old new # Rename other branchMerging and Rebasing
# Merge
git merge branch-name # Merge into current branch
git merge --no-ff branch # Force merge commit
git merge --squash branch # Squash all commits into one
git merge --abort # Cancel conflicted merge
# Rebase
git rebase main # Rebase onto main
git rebase -i HEAD~4 # Interactive rebase last 4 commits
git rebase --continue # After resolving conflicts
git rebase --abort # Cancel rebaseRemote Operations
# Manage remotes
git remote -v # List remotes
git remote add origin <url> # Add remote
git remote remove origin # Remove remote
git remote rename old new # Rename remote
# Fetch, Pull, Push
git fetch # Download remote data
git fetch --all # From all remotes
git fetch --prune # Remove deleted remote branches
git pull # Fetch + merge
git pull --rebase # Fetch + rebase
git pull --ff-only # Only fast-forward
git push origin main # Push to remote
git push -u origin branch # Push and set upstream
git push --force-with-lease # Safe force push
git push --tags # Push all tagsHistory and Inspection
# View log
git log # Full log
git log --oneline # Compact view
git log --graph # Visual branch graph
git log --oneline --graph --all # Full picture
git log -5 # Last 5 commits
git log --author="Name" # Filter by author
git log --since="2 weeks" # Time-based filter
git log -- path/to/file # File history
git log -p file.txt # Show diffs per commit
# Show commit details
git show abc1234 # Show specific commit
git show HEAD # Show latest commitUndoing Changes
# Discard working directory changes
git restore file.txt # Restore file (modern)
git checkout -- file.txt # Restore file (classic)
# Unstage files
git restore --staged file.txt
git reset HEAD file.txt # Classic way
# Reset commits
git reset --soft HEAD~1 # Undo commit, keep changes staged
git reset --mixed HEAD~1 # Undo commit, keep changes unstaged
git reset --hard HEAD~1 # Undo commit, discard changes
# Revert (safe for shared history)
git revert abc1234 # Create reverse commit
git revert HEAD # Revert latest commitStash Commands
git stash # Stash current changes
git stash push -m "message" # Stash with description
git stash list # List all stashes
git stash pop # Apply and remove latest stash
git stash apply # Apply without removing
git stash drop # Delete latest stash
git stash clear # Delete all stashesTags
git tag v1.0.0 # Lightweight tag
git tag -a v1.0.0 -m "msg" # Annotated tag
git tag -l "v1.*" # List matching tags
git push origin v1.0.0 # Push specific tag
git push origin --tags # Push all tags
git tag -d v1.0.0 # Delete local tagKey Takeaways
This cheatsheet covers the essential Git commands for daily development work. Start with the basics — status, add, commit, push, pull — and gradually incorporate more advanced commands as your workflow demands. The best way to learn Git is through daily practice, and this reference ensures you always have the correct syntax when you need it.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Command Cheatsheet.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, interview, preparation, command, cheatsheet
Related Git & GitHub Topics