Best FreeOnline Tools

🚀 Git Cheat Sheet

Quick reference guide for Git commands and workflows. All your notes are saved locally in your browser.

🚀 Git Basics

Check Git Version

Display Git version information

git --version

Configure Git

Set up your Git identity

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --list  # View all settings

Initialize Repository

Create a new Git repository

git init
git init <directory-name>

Clone Repository

Copy a repository to your local machine

git clone <repository-url>
git clone <repository-url> <directory-name>
git clone --depth 1 <repository-url>  # Shallow clone

Get Help

Access Git command documentation

git help
git help <command>
git <command> --help

📝 Staging & Committing

Check Status

View the status of your working directory

git status
git status -s  # Short format

Add Files to Staging

Stage files for commit

git add <file>
git add .  # Add all files
git add *.js  # Add all JS files
git add -p  # Interactive staging

Commit Changes

Save staged changes to repository

git commit -m "Commit message"
git commit -am "Commit message"  # Add and commit
git commit --amend  # Modify last commit
git commit --amend --no-edit  # Amend without changing message

Unstage Files

Remove files from staging area

git reset <file>
git reset  # Unstage all files
git restore --staged <file>

Discard Changes

Revert working directory changes

git checkout -- <file>
git restore <file>
git checkout .  # Discard all changes

🌿 Branching & Merging

List Branches

Show all branches

git branch
git branch -a  # Include remote branches
git branch -r  # Remote branches only

Create Branch

Create a new branch

git branch <branch-name>
git checkout -b <branch-name>  # Create and switch
git switch -c <branch-name>  # Create and switch (newer)

Switch Branch

Change to a different branch

git checkout <branch-name>
git switch <branch-name>  # Newer command

Delete Branch

Remove a branch

git branch -d <branch-name>  # Safe delete
git branch -D <branch-name>  # Force delete
git push origin --delete <branch-name>  # Delete remote branch

Merge Branch

Combine branches

git merge <branch-name>
git merge --no-ff <branch-name>  # No fast-forward
git merge --squash <branch-name>  # Squash commits

Rename Branch

Change branch name

git branch -m <old-name> <new-name>
git branch -m <new-name>  # Rename current branch

🌐 Remote Repositories

List Remotes

Show remote repositories

git remote
git remote -v  # Show URLs

Add Remote

Add a new remote repository

git remote add <name> <url>
git remote add origin https://github.com/user/repo.git

Fetch Changes

Download remote changes without merging

git fetch
git fetch <remote>
git fetch --all  # All remotes

Pull Changes

Fetch and merge remote changes

git pull
git pull <remote> <branch>
git pull --rebase  # Rebase instead of merge

Push Changes

Upload local commits to remote

git push
git push <remote> <branch>
git push -u origin <branch>  # Set upstream
git push --force  # Force push (use carefully!)
git push --force-with-lease  # Safer force push

Remove Remote

Delete a remote connection

git remote remove <name>
git remote rm <name>

📜 History & Inspection

View Commit History

Display commit logs

git log
git log --oneline  # Compact view
git log --graph --all --oneline  # Visual graph
git log -n 5  # Last 5 commits
git log --since="2 weeks ago"  # Time-based filter

View Commit Details

Show specific commit information

git show <commit-hash>
git show HEAD  # Latest commit
git show <commit-hash>:<file>  # File in specific commit

View Differences

Compare changes between commits/branches

git diff
git diff <file>
git diff --staged  # Staged changes
git diff <commit1> <commit2>
git diff <branch1>..<branch2>

Show File History

View commits affecting a specific file

git log <file>
git log -p <file>  # With diffs
git blame <file>  # Line-by-line attribution

Search Commits

Find commits by message or content

git log --grep="keyword"
git log -S "code string"  # Search code changes
git log --author="name"

Undoing Changes

Undo Last Commit (Keep Changes)

Remove commit but keep modifications

git reset --soft HEAD~1
git reset --soft <commit-hash>

Undo Last Commit (Discard Changes)

Remove commit and discard changes

git reset --hard HEAD~1
git reset --hard <commit-hash>

Revert Commit

Create new commit that undoes changes

git revert <commit-hash>
git revert HEAD  # Revert last commit

Clean Untracked Files

Remove untracked files and directories

git clean -n  # Dry run
git clean -f  # Remove files
git clean -fd  # Remove files and directories
git clean -fX  # Remove only ignored files

Recover Deleted Commit

Find and restore lost commits

git reflog
git reflog show
git checkout <commit-hash>  # Restore specific commit

📦 Stashing

Stash Changes

Temporarily save changes

git stash
git stash save "message"
git stash -u  # Include untracked files

List Stashes

Show all stashed changes

git stash list

Apply Stash

Restore stashed changes

git stash apply
git stash apply stash@{n}  # Specific stash
git stash pop  # Apply and remove stash

Show Stash Contents

View what's in a stash

git stash show
git stash show -p  # With diff
git stash show stash@{n}

Delete Stash

Remove stashed changes

git stash drop stash@{n}
git stash clear  # Remove all stashes

🔄 Rebasing

Rebase Branch

Reapply commits on top of another branch

git rebase <branch>
git rebase main
git rebase --continue  # After resolving conflicts
git rebase --abort  # Cancel rebase

Interactive Rebase

Edit commit history interactively

git rebase -i HEAD~n  # Last n commits
git rebase -i <commit-hash>
# Options: pick, reword, edit, squash, fixup, drop

Squash Commits

Combine multiple commits into one

git rebase -i HEAD~3
# Change 'pick' to 'squash' or 's' for commits to merge

🏷️ Tagging

List Tags

Show all tags

git tag
git tag -l "v1.*"  # Filter tags

Create Tag

Mark a specific commit

git tag <tag-name>
git tag -a v1.0.0 -m "Version 1.0.0"  # Annotated tag
git tag v1.0.0 <commit-hash>  # Tag specific commit

Push Tags

Upload tags to remote

git push origin <tag-name>
git push origin --tags  # Push all tags

Delete Tag

Remove a tag

git tag -d <tag-name>  # Delete local tag
git push origin --delete <tag-name>  # Delete remote tag

Checkout Tag

Switch to a tagged commit

git checkout <tag-name>
git checkout tags/<tag-name> -b <branch-name>

Advanced Commands

Cherry Pick

Apply specific commits to current branch

git cherry-pick <commit-hash>
git cherry-pick <commit1> <commit2>
git cherry-pick --continue
git cherry-pick --abort

Bisect (Find Bug)

Binary search to find commit that introduced bug

git bisect start
git bisect bad  # Current commit is bad
git bisect good <commit-hash>  # Known good commit
git bisect reset  # End bisect

Submodules

Manage repositories within repositories

git submodule add <repository-url> <path>
git submodule init
git submodule update
git submodule update --remote

Worktree

Manage multiple working trees

git worktree add <path> <branch>
git worktree list
git worktree remove <path>

Archive Repository

Create archive of repository

git archive --format=zip HEAD > archive.zip
git archive --format=tar main | gzip > archive.tar.gz

🚫 .gitignore Patterns

Common .gitignore

Example .gitignore file

# Dependencies
node_modules/
vendor/

# Build outputs
dist/
build/
*.log

# Environment files
.env
.env.local

# IDE files
.vscode/
.idea/
*.swp

# OS files
.DS_Store
Thumbs.db

Ignore Pattern Rules

Common .gitignore patterns

*.txt          # All .txt files
!important.txt  # Exception
folder/        # Directory
**/logs        # logs in any directory
file?.txt      # Single character wildcard
file[0-9].txt  # Character range

Remove Tracked File

Stop tracking a file that's already committed

git rm --cached <file>
git rm -r --cached <directory>
# Then add to .gitignore and commit

💡 Git Best Practices

  • Write clear, descriptive commit messages that explain the "why" not just the "what"
  • Commit often with small, logical changes rather than large batches
  • Use branches for new features and keep main/master stable
  • Pull before you push to avoid conflicts and keep history clean
  • Use .gitignore to exclude build artifacts, dependencies, and sensitive files
  • Never commit secrets, API keys, or passwords to version control
  • Review changes with git diff before committing
  • Use git stash when switching branches with uncommitted changes

About This Git Cheat Sheet

This Git Cheat Sheet is designed for developers of all skill levels to quickly reference essential version control commands. Whether you are initializing a new repository, resolving merge conflicts, or cleaning up your commit history, having these commands readily available can drastically improve your development workflow.

Who is it for?

  • Software Developers: Easily recall branching, merging, and rebasing commands.
  • Open Source Contributors: Learn how to properly fork, clone, and submit pull requests via the command line.
  • Beginners: Understand the fundamentals of staging, committing, and pushing code.

Common Use Cases

  • Undoing Mistakes: Quick access to git reset and git revert to fix accidental commits.
  • Branch Management: Switching context safely with git checkout and git stash.
  • Collaboration: Synchronizing local environments with remote branches using git fetch and git pull.

Frequently Asked Questions (FAQ)

What is the difference between git fetch and git pull?

git fetch downloads updates from the remote repository but does not apply them to your working directory. git pull is a combination of git fetch followed by a git merge, updating your local code immediately.

How do I discard local changes?

Use git restore <file> to discard changes in a specific file, or git checkout . to discard all uncommitted local changes.

What does git rebase do?

Rebasing rewrites your commit history by moving your feature branch commits to the tip of the main branch, creating a cleaner, linear history instead of a tangled merge commit.

Related Developer Tools

Explore more tools to boost your productivity: