Web Development

Git Commands Cheat Sheet: The Commands You’ll Actually Use

Git Commands Cheat Sheet: The Commands You’ll Actually Use
Table of contents
  1. Before You Start: Install and Check Git
  2. Git Setup and Configuration Commands
  3. Basic Git Commands for Everyday Work
  4. Start or Copy a Repository
  5. Stage and Commit Changes
  6. Git Branch Commands
  7. Working With GitHub and Remote Repositories
  8. Inspect History: git log, git show, and git blame
  9. Git Undo Commands: Fix Mistakes Safely
  10. Stash, Tag, and Other Handy Commands
  11. Git Commands List: The 10 to Memorize First
  12. Where to Go From Here
  13. Frequently Asked Questions
  14. What are the basic Git commands?
  15. What is the difference between git fetch and git pull?
  16. How do I undo my last commit in Git?
  17. What is the difference between git switch and git checkout?
  18. How do I delete a branch in Git locally and remotely?
  19. Is Git the same as GitHub?

This Git commands cheat sheet covers the commands that handle nearly all day-to-day work: setting up a repository, saving changes, branching, syncing with GitHub, and undoing mistakes. If you only memorize five, make them git status, git add, git commit, git pull, and git push. Everything below is grouped by task, with a plain-English note on what each command does and when it can bite you.

Key Takeaways

  • Run git status before and after almost everything. It tells you exactly where your changes are.
  • Use git switch and git restore (added in Git 2.23) instead of the overloaded git checkout.
  • Already pushed? Undo with git revert, not git reset.
  • Pick git push --force-with-lease over --force so you never wipe out a teammate’s work.
  • git reflog can bring back “lost” commits, usually for 30 days or more.

Before You Start: Install and Check Git

Open a terminal and type git --version. If you see a version number, you’re set. If not, here’s the quickest install path for each system:

  • Mac: Running git --version on a Mac without Git prompts you to install Apple’s Command Line Developer Tools, which include Git. Homebrew users can run brew install git for a newer version.
  • Windows: Download Git for Windows from the official Git downloads page. It comes with Git Bash, a terminal where every command in this guide works as written.
  • Linux: Use sudo apt install git on Ubuntu or Debian, or sudo dnf install git on Fedora.

New to the command line itself? Our terminal commands cheat sheet for Mac and Linux covers cd, ls, and the other basics you’ll use alongside Git.

Git Setup and Configuration Commands

Do this once per computer. Git stamps your name and email on every commit, so get it right before your first one.

CommandWhat it does
git config --global user.name "Jane Doe"Sets the name attached to your commits
git config --global user.email "jane@example.com"Sets the email attached to your commits
git config --global init.defaultBranch mainMakes new repos start on a branch called main (Git 2.28 or later)
git config --global core.editor "code --wait"Opens VS Code instead of Vim for commit messages
git config --listShows every setting currently in effect

One small thing that saves headaches later: use the same email your GitHub account uses (or GitHub’s private noreply address). Otherwise your commits won’t link to your profile and won’t count on your contribution graph.

Basic Git Commands for Everyday Work

Git tracks your code in three local places: the working directory (the files you edit), the staging area (changes you’ve picked for the next commit), and the local repository (your saved history). Most basic Git commands just move changes from one place to the next.

Diagram of basic git commands moving changes from working directory to staging area, local repo and GitHub

Start or Copy a Repository

CommandWhat it does
git initTurns the current folder into a new Git repository
git clone <url>Downloads a full copy of a remote repository, history included
git clone <url> my-folderClones into a folder name you choose

Stage and Commit Changes

CommandWhat it does
git statusShows changed, staged, and untracked files
git status -sSame thing, in a compact one-line-per-file view
git add app.jsStages one file
git add .Stages everything in the current folder and below
git add -pWalks you through each change so you stage only what you want
git diffShows edits you haven’t staged yet
git diff --stagedShows what’s staged and about to be committed
git commit -m "Fix login bug"Saves staged changes with a message
git commit -am "Update styles"Stages and commits changes to tracked files in one step
git rm old-file.jsDeletes a file and stages the deletion
git mv old.js new.jsRenames a file and stages the rename

Two habits from years of code review. First, use git add -p instead of git add . when you can. Reviewing each chunk catches the stray debug line or leftover API key before it lands in history. Second, know that git commit -am skips brand-new files. If you created a file, you still need git add for it.

Git Branch Commands

Branches let you build a feature or fix a bug without touching the main line of code. Create one for every piece of work, even small ones. It costs nothing, and it keeps main deployable.

Git branch commands diagram showing git switch -c, commits on a feature branch, and git merge into main
CommandWhat it does
git branchLists local branches (the current one has an asterisk)
git branch -aLists local and remote branches
git switch feature/searchMoves to an existing branch
git switch -c feature/searchCreates a new branch and moves to it
git switch -Jumps back to the branch you were just on
git branch -m new-nameRenames the current branch
git branch -d feature/searchDeletes a branch that’s already merged
git branch -D feature/searchForce-deletes a branch, merged or not
git merge feature/searchMerges that branch into the one you’re on
git rebase mainReplays your branch’s commits on top of the latest main

Merge or rebase? My rule is simple. Rebase your own feature branch onto main before opening a pull request, because it gives reviewers a clean, straight history. Never rebase commits that other people have already pulled, because rebasing rewrites them and your teammates’ copies will no longer match.

Working With GitHub and Remote Repositories

A remote is a copy of your repository hosted somewhere else, usually GitHub, GitLab, or Bitbucket. By convention, the main remote is named origin.

CommandWhat it does
git remote -vLists remotes and their URLs
git remote add origin <url>Connects a local repo to a remote
git fetchDownloads new commits without changing your files
git pullFetches and merges remote changes into your branch
git pull --rebaseFetches and replays your local commits on top, with no merge commit
git pushUploads your commits to the remote
git push -u origin feature/searchPushes a new branch and sets it to track the remote
git push --force-with-leaseOverwrites the remote branch only if nobody else pushed since your last fetch
git push origin --delete feature/searchDeletes a branch on the remote

If git push rejects your GitHub password, that’s expected. GitHub stopped accepting account passwords for Git operations on August 13, 2021. Use a personal access token, an SSH key, or the GitHub CLI (gh auth login), which sets up credentials for you in about a minute.

Inspect History: git log, git show, and git blame

CommandWhat it does
git logFull commit history for the current branch
git log --oneline --graph --allCompact history of every branch, drawn as a tree
git log -p app.jsEvery change ever made to one file
git log --author="Jane"Commits by one person
git show <hash>Details and changes for a single commit
git diff main feature/searchCompares two branches
git blame app.jsShows who last changed each line, and in which commit

git log --oneline --graph --all is long enough that most developers alias it. Run git config --global alias.lg "log --oneline --graph --all" and from then on, git lg does the job.

Git Undo Commands: Fix Mistakes Safely

Almost everything in Git can be undone. The question that decides which command to use is whether you’ve already pushed. If the commit is only on your machine, you can rewrite history freely. If it’s on GitHub and others may have pulled it, use git revert.

Git undo commands table comparing git restore, git commit --amend, git revert and git reset with risk levels
SituationCommandOK after pushing?
Throw away unsaved edits to a filegit restore app.jsLocal only (edits are gone for good)
Unstage a file but keep the editsgit restore --staged app.jsYes
Fix the last commit message or add a forgotten file (stage it first)git commit --amendNo
Undo the last commit, keep changes stagedgit reset --soft HEAD~1No
Undo the last commit, keep changes unstagedgit reset HEAD~1No
Delete the last commit and its changesgit reset --hard HEAD~1No
Reverse a commit that’s already on GitHubgit revert <hash>Yes
Find a commit you “lost” after a resetgit reflogYes
Preview, then delete untracked filesgit clean -n, then git clean -fdLocal only

Treat git reset --hard and git clean -fd with respect. They delete uncommitted work, and Git can’t bring that back. Committed work is different. Even after a hard reset, git reflog lists where your branch pointed before, and Git keeps those entries for 30 days or more by default. Find the hash, then run git reset --hard <hash> to get back to it.

Stash, Tag, and Other Handy Commands

CommandWhat it does
git stashShelves uncommitted changes so you can switch tasks
git stash -uStashes untracked files too
git stash listShows saved stashes
git stash popRestores the latest stash and removes it from the list
git stash apply stash@{1}Restores a specific stash and keeps it saved
git tag -a v1.0.0 -m "First release"Creates an annotated tag for a release
git push origin --tagsPushes your tags to the remote
git cherry-pick <hash>Copies one commit onto your current branch
git rm --cached .envStops tracking a file without deleting it from your disk

Set up a .gitignore file on day one. At minimum, list .env, node_modules/, and, on a Mac, .DS_Store. If a secrets file already slipped into a commit, git rm --cached stops tracking it, but the old commit still holds the value, so rotate that password or API key right away.

Git Commands List: The 10 to Memorize First

If this Git commands cheat sheet feels like a lot, start here. These ten cover a typical workday:

  • git status to see where things stand
  • git add -p to stage changes carefully
  • git commit -m "message" to save a snapshot
  • git switch -c branch-name to start new work
  • git pull --rebase to get your team’s latest changes
  • git push to share your commits
  • git log --oneline --graph to see recent history
  • git stash to park work in progress
  • git restore to discard or unstage changes
  • git revert to safely undo a pushed commit

Where to Go From Here

The fastest way to make these stick is to break something on purpose. Make a throwaway folder, run git init, commit a few files, create a branch, merge it, then try every undo command in the table above. Ten minutes of that beats an hour of reading. When you want the full details on any command, the official Git reference documentation and the free Pro Git book are the most reliable sources. And if you’re still choosing where to write your code, our guide to the best free code editors for developers covers editors with Git built right in.

Frequently Asked Questions

What are the basic Git commands?

The basic Git commands are git init or git clone to start, git status to check changes, git add to stage files, git commit to save a snapshot, and git push and git pull to sync with a remote like GitHub. Add git switch for branches and git log for history, and you can handle most everyday work.

What is the difference between git fetch and git pull?

git fetch downloads new commits from the remote but leaves your files and current branch untouched, so you can review changes first. git pull runs a fetch and then immediately merges (or rebases, with –rebase) those changes into your branch. Use fetch when you want to look before you combine anything.

How do I undo my last commit in Git?

If you haven’t pushed, run git reset –soft HEAD~1 to undo the commit and keep your changes staged, or git reset HEAD~1 to keep them unstaged. If the commit is already on GitHub, run git revert followed by the commit hash. That creates a new commit reversing it without rewriting shared history.

What is the difference between git switch and git checkout?

git checkout does several jobs, including changing branches and restoring files, which makes mistakes easy. Git 2.23 split those jobs into two clearer commands: git switch changes branches and git restore restores files. Both checkout styles still work, but switch and restore are easier to read and harder to misuse, especially for beginners.

How do I delete a branch in Git locally and remotely?

To delete a local branch that’s already merged, run git branch -d branch-name. Use git branch -D to force-delete one that isn’t merged. To delete the same branch on GitHub or another remote, run git push origin –delete branch-name. You can’t delete the branch you’re on, so switch away first.

Is Git the same as GitHub?

No. Git is free, open-source version control software that runs on your own computer and tracks changes to files. GitHub is a website, owned by Microsoft, that hosts Git repositories online and adds pull requests, issues, and team tools. You can use Git without GitHub, and GitLab and Bitbucket offer similar hosting.

Written by Trust Jaguar

Trust Jaguar is an independent editorial team producing researched, practical guides on personal finance, technology, gaming, and everyday processes. Each article is verified against official sources and reviewed regularly to keep it accurate and current.