Cheatsheets / Git Cheatsheet

Git Cheatsheet

The everyday Git commands a working developer actually reaches for - setup, branching, remotes, history, and undoing mistakes.

Last verified

Setup and config

git config --global user.name "Your Name"

Sets the name attached to your commits, machine-wide.

git config --global user.email "you@example.com"

Sets the email attached to your commits.

git config --global init.defaultBranch main

Sets the default branch name for new repositories.

git config --list

Shows all effective config values and where they came from.

git init

Creates a new, empty Git repository in the current directory.

git clone https://github.com/user/repo.git

Copies a remote repository, including its full history, to your machine.

Basic snapshotting

git status

Shows staged, unstaged, and untracked changes.

git add file.txt

Stages a specific file for the next commit.

git add -A

Stages all changes, including deletions, across the whole repo.

git add -p

Interactively stages changes hunk by hunk - useful when a file has unrelated edits.

git commit -m "Fix login redirect bug"

Records the staged changes as a new commit.

git commit --amend

Rewrites the most recent commit (message and/or contents) instead of creating a new one.

git diff

Shows unstaged changes against the last commit.

git diff --staged

Shows staged changes that will go into the next commit.

git restore file.txt

Discards unstaged changes to a file, reverting it to the last commit.

git restore --staged file.txt

Unstages a file without discarding its changes.

Branching and merging

git branch

Lists local branches, marking the current one.

git branch feature/login

Creates a new branch without switching to it.

git switch feature/login

Switches to an existing branch.

git switch -c feature/login

Creates and switches to a new branch in one step.

git branch -d feature/login

Deletes a branch that has already been merged.

git branch -D feature/login

Force-deletes a branch even if unmerged.

git merge feature/login

Merges another branch into the current one.

git merge --no-ff feature/login

Merges while always creating a merge commit, even if a fast-forward were possible - keeps feature history visible.

Remote operations

git remote -v

Lists configured remotes and their URLs.

git remote add origin https://github.com/user/repo.git

Adds a remote named origin.

git fetch origin

Downloads new commits and branches from a remote without merging them.

git pull

Fetches from the remote and merges into the current branch in one step.

git pull --rebase

Fetches and replays your local commits on top of the remote branch instead of merging.

git push

Uploads local commits on the current branch to its tracked remote branch.

git push -u origin feature/login

Pushes a new branch and sets it to track the remote branch for future plain git push/git pull.

git push --force-with-lease

Force-pushes, but only if no one else has pushed to the remote branch since you last fetched - safer than --force.

History and inspection

git log --oneline --graph --decorate

Shows a compact, graphical commit history with branch/tag labels.

git log -p -- file.txt

Shows the full commit history and diffs for a single file.

git blame file.txt

Shows which commit last changed each line of a file.

git show abc1234

Shows the full diff and metadata of a specific commit.

git log --author="Anupam"

Filters commit history by author.

git log --since="2 weeks ago"

Filters commit history by date.

Undoing changes

git reset --soft HEAD~1

Undoes the last commit but keeps its changes staged.

git reset --mixed HEAD~1

Undoes the last commit and unstages its changes (default mode of git reset).

git reset --hard HEAD~1

Undoes the last commit and discards its changes entirely. Destructive - make sure you mean it.

git revert abc1234

Creates a new commit that undoes a specific past commit, without rewriting history - safe on shared branches.

git clean -fd

Deletes untracked files and directories not covered by .gitignore.

Stashing

git stash

Shelves uncommitted changes so you can switch branches cleanly.

git stash push -u -m "wip: login form"

Stashes changes (including untracked files) with a descriptive message.

git stash list

Lists all stashed changesets.

git stash pop

Reapplies the most recent stash and removes it from the stash list.

git stash apply stash@{2}

Reapplies a specific stash without removing it from the list.

Tags

git tag v1.2.0

Creates a lightweight tag on the current commit.

git tag -a v1.2.0 -m "Release 1.2.0"

Creates an annotated tag with a message (preferred for releases).

git push origin v1.2.0

Pushes a single tag to the remote (tags are not pushed by plain git push).

git push --tags

Pushes all local tags to the remote.

Rebase and cherry-pick

git rebase main

Replays the current branch’s commits on top of the latest main, producing linear history.

git rebase -i HEAD~3

Opens an interactive rebase to reorder, squash, or edit the last 3 commits.

git rebase --abort

Cancels an in-progress rebase and returns to the state before it started.

git cherry-pick abc1234

Applies a single commit from another branch onto the current one.