Git Cheatsheet

95+ Git commands with descriptions — searchable by keyword or category. Click Copy to grab any command.

Showing 95 of 95 commands
git initSetup

Initialize a new local repository.

git init <directory>Setup

Create a new repository in the specified directory.

git clone <url>Setup

Clone a remote repository into a new local directory.

git clone <url> <directory>Setup

Clone into a specific local folder name.

git clone --depth 1 <url>Setup

Shallow clone — only the latest commit (faster).

git clone --branch <branch> <url>Setup

Clone and check out a specific branch.

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

Set the global author name for all commits.

git config --global user.email "email"Config

Set the global author email for all commits.

git config --global core.editor vimConfig

Set the default text editor for commit messages.

git config --listConfig

List all config settings (global + local).

git config --global alias.st statusConfig

Create a shortcut alias (e.g. `git st` → `git status`).

git config --global pull.rebase trueConfig

Rebase instead of merge when pulling.

git config --global color.ui autoConfig

Enable colorized output in the terminal.

git statusBasics

Show the working tree status (staged, unstaged, untracked).

git add .Basics

Stage all changes in the current directory.

git add <file>Basics

Stage a specific file.

git add -pBasics

Interactively stage hunks within files.

git commit -m "message"Basics

Commit staged changes with a message.

git commit --amendBasics

Modify the last commit (message or staged content).

git commit --amend --no-editBasics

Amend the last commit without changing the message.

git diffBasics

Show unstaged changes relative to the index.

git diff --stagedBasics

Show staged changes ready for the next commit.

git diff <branch1>..<branch2>Basics

Compare two branches.

git rm <file>Basics

Remove a file from the working tree and index.

git mv <old> <new>Basics

Move or rename a file and stage the change.

git branchBranching

List all local branches.

git branch -aBranching

List local and remote branches.

git branch <branch>Branching

Create a new branch at the current HEAD.

git branch -d <branch>Branching

Delete a fully merged branch.

git branch -D <branch>Branching

Force-delete a branch regardless of merge status.

git branch -m <old> <new>Branching

Rename a branch.

git checkout -b <branch>Branching

Create and check out a new branch.

git switch <branch>Branching

Switch to an existing branch (modern syntax).

git switch -c <branch>Branching

Create and switch to a new branch (modern syntax).

git checkout -Branching

Switch back to the previously checked-out branch.

git merge <branch>Merging

Merge the specified branch into the current branch.

git merge --no-ff <branch>Merging

Merge with a merge commit, even if fast-forward is possible.

git merge --squash <branch>Merging

Squash branch commits into a single staged change.

git merge --abortMerging

Abort a merge in progress and return to pre-merge state.

git mergetoolMerging

Open a merge conflict resolution tool.

git remote -vRemote

List remote connections with URLs.

git remote add origin <url>Remote

Add a new remote named "origin".

git remote remove <name>Remote

Remove a remote connection.

git remote rename <old> <new>Remote

Rename a remote.

git push origin <branch>Remote

Push a branch to the remote.

git push -u origin <branch>Remote

Push and set upstream tracking for the branch.

git push --force-with-leaseRemote

Safe force-push: fails if remote has new commits.

git push origin --delete <branch>Remote

Delete a remote branch.

git pullRemote

Fetch and merge changes from the tracking remote branch.

git pull --rebaseRemote

Fetch and rebase instead of merging.

git fetchRemote

Download remote changes without merging.

git fetch --pruneRemote

Fetch and remove stale remote-tracking branches.

git stashStash

Save dirty working directory to the stash stack.

git stash push -m "message"Stash

Stash with a descriptive label.

git stash popStash

Apply the latest stash and remove it from the stack.

git stash apply stash@{n}Stash

Apply a specific stash without removing it.

git stash listStash

List all stashed changesets.

git stash drop stash@{n}Stash

Delete a specific stash entry.

git stash clearStash

Remove all stash entries.

git stash branch <branch>Stash

Create a new branch from the latest stash.

git logHistory

Show the full commit history.

git log --onelineHistory

Compact one-line-per-commit log.

git log --graph --oneline --allHistory

Visual branch graph with all refs.

git log -p <file>History

Show patch diff for each commit touching a file.

git log --author="Name"History

Filter commits by author.

git log --since="2 weeks ago"History

Show commits from the last two weeks.

git log --grep="keyword"History

Search commit messages for a keyword.

git blame <file>History

Show who last modified each line of a file.

git show <commit>History

Show metadata and diff for a specific commit.

git shortlog -snHistory

Summarize commits by author (sorted by count).

git restore <file>Undoing

Discard unstaged changes in a file.

git restore --staged <file>Undoing

Unstage a file (keep working copy changes).

git reset HEAD~1Undoing

Undo the last commit, keeping changes unstaged.

git reset --soft HEAD~1Undoing

Undo the last commit, keeping changes staged.

git reset --hard HEAD~1Undoing

Undo the last commit and discard all changes.

git revert <commit>Undoing

Create a new commit that undoes a prior commit.

git clean -fdUndoing

Remove untracked files and directories.

git clean -nUndoing

Dry-run clean — show what would be deleted.

git reflogUndoing

Show a log of all reference updates (recover lost commits).

git rebase <branch>Advanced

Rebase current branch onto another branch.

git rebase -i HEAD~3Advanced

Interactive rebase for the last 3 commits (squash, reword, etc.).

git rebase --abortAdvanced

Abort an in-progress rebase.

git rebase --continueAdvanced

Continue a rebase after resolving conflicts.

git cherry-pick <commit>Advanced

Apply a specific commit onto the current branch.

git cherry-pick <hash1>..<hash2>Advanced

Apply a range of commits onto the current branch.

git bisect startAdvanced

Start a binary search to find the commit that introduced a bug.

git bisect good <commit>Advanced

Mark a commit as good during bisect.

git bisect bad <commit>Advanced

Mark a commit as bad during bisect.

git bisect resetAdvanced

End the bisect session and return to original HEAD.

git worktree add <path> <branch>Advanced

Check out a branch in a separate directory (worktree).

git worktree listAdvanced

List all linked worktrees.

git tag <name>Advanced

Create a lightweight tag at the current commit.

git tag -a <name> -m "message"Advanced

Create an annotated tag with a message.

git push origin --tagsAdvanced

Push all local tags to the remote.

git submodule add <url>Advanced

Add an external repository as a submodule.