Web Development

Git Cheatsheet: Essential Commands for Everyday Use

A practical Git cheatsheet with real-world examples: configuration, staging, branching, merging, stashing, undoing changes, and collaboration tips.

Branded git logo

Git Cheatsheet: Essential Commands for Everyday Use

This quick-reference guide collects the most useful Git commands with short explanations and copy-paste examples. Whether you’re new to Git or need a refresher, this cheatsheet covers the daily essentials plus some advanced tricks.

Basics

Use these to get started and check your Git setup:

  • git init → Create a new repository in the current folder
  • git clone [url] → Copy a repository from a remote URL
  • git status → Show the working tree status
  • git help [command] → Get help for a specific Git command

Configuration

Manage your identity and editor settings:

  • git config --global user.name "Your Name" → Set global author name
  • git config --global user.email "you@example.com" → Set global author email
  • git config --list → Show all configuration

Staging & Committing

Track file changes in your project:

  • git add [file] → Stage a specific file
  • git add . → Stage all changes in the directory
  • git commit -m "message" → Commit staged changes with a message
  • git commit -am "message" → Stage and commit tracked files in one step

Branching & Merging

Work with feature branches and combine changes:

  • git branch → List all branches
  • git branch [name] → Create a new branch
  • git checkout [name] → Switch to another branch
  • git switch -c [name] → Create and switch to a new branch (modern syntax)
  • git merge [branch] → Merge another branch into the current one
  • git branch -d [name] → Delete a branch

Remote Repositories

Work with GitHub, GitLab, or Bitbucket remotes:

  • git remote -v → Show remote URLs
  • git remote add origin [url] → Add a new remote
  • git push -u origin main → Push branch to remote and set upstream
  • git pull → Fetch and merge changes from remote
  • git fetch → Download changes without merging

Inspecting History

Review commits and logs:

  • git log → Show commit history
  • git log --oneline --graph --decorate → Pretty, compact log view
  • git show [commit] → Show details of a commit
  • git diff → Show unstaged changes
  • git diff --staged → Show staged changes

Undoing Changes

Fix mistakes and roll back:

  • git restore [file] → Discard changes in a file
  • git restore --staged [file] → Unstage a file
  • git reset --hard [commit] → Reset repository to a commit (dangerous)
  • git revert [commit] → Create a new commit that undoes a previous one

Stashing

Temporarily save uncommitted changes:

  • git stash → Save dirty changes for later
  • git stash list → Show stashed changes
  • git stash apply → Reapply latest stash
  • git stash drop → Delete latest stash

Collaboration Tips

  • Use feature branches for each task.
  • Keep your main branch stable.
  • Always pull before pushing to avoid conflicts.
  • Use pull requests for review and collaboration.

Advanced Tricks

Go beyond the basics with these powerful commands:

  • git cherry-pick [commit] → Apply a specific commit onto the current branch
  • git rebase [branch] → Replay commits onto another branch
  • git tag [name] → Create a lightweight tag for a commit
  • git reflog → Show history of HEAD (great for recovering lost commits)
Tip: Use Git frequently with small commits and clear messages. It’s easier to understand history and revert changes later.