9kit.org

Git Cheatsheet

A comprehensive Git commands quick reference guide. Find common Git commands organized by scenario including getting started, branching, merging, remote operations, undo, stash, and logs. Click any command to copy it to your clipboard.

Quick Scenarios:
git init

Initialize a new Git repository in the current directory

Example:

git init
git clone <repository>

Clone a remote repository to your local machine

Common Flags:

--depth- Shallow clone with limited history-b <branch>- Clone specific branch

Example:

git clone https://github.com/user/repo.git
git config --global user.name "<name>"

Set your global username for commits

Example:

git config --global user.name "John Doe"
git config --global user.email "<email>"

Set your global email for commits

Example:

git config --global user.email "john@example.com"
git add <file>

Stage changes for the next commit

Common Flags:

-A- Stage all changes (new, modified, deleted)-p- Interactively select hunks to stage.- Stage all changes in current directory

Example:

git add index.html
git commit -m "<message>"

Create a commit with the staged changes

Common Flags:

-m- Provide commit message-a- Automatically stage modified files--amend- Modify the last commit

Example:

git commit -m "Add new feature"
git status

Show the current state of your working directory

Common Flags:

-s- Short format output-b- Show branch info in short format

Example:

git status
git diff

Show unstaged changes between working directory and staging area

Common Flags:

--staged- Show staged changes--stat- Show diffstat summary

Example:

git diff
git rm <file>

Remove a file from Git tracking and delete from disk

Common Flags:

-f- Force removal (required if file is modified)--cached- Remove only from Git, keep file on disk

Example:

git rm temp.txt
git mv <source> <destination>

Rename or move a file

Example:

git mv old-name.txt new-name.txt
git branch

List all local branches

Common Flags:

-a- List all branches (local and remote)-d <branch>- Delete a merged branch-D <branch>- Force delete a branch-m <old> <new>- Rename a branch

Example:

git branch
git checkout <branch>

Switch to a different branch

Common Flags:

-b <branch>- Create and switch to new branch

Example:

git checkout main
git switch <branch>

Switch to a different branch (modern Git)

Common Flags:

-c <branch>- Create and switch to new branch-- Switch to previous branch

Example:

git switch develop
git checkout -b <branch>

Create a new branch and switch to it

Example:

git checkout -b feature/login
git branch <name>

Create a new branch without switching

Example:

git branch hotfix/bug-123
git merge <branch>

Merge a branch into your current branch

Common Flags:

--no-ff- Create merge commit even for fast-forward--squash- Squash all commits into one--abort- Abort the current merge

Example:

git merge feature/login
git rebase <branch>

Reapply commits on top of another branch

Common Flags:

-i- Interactive rebase--onto <new>- Rebase onto a different branch--abort- Abort the current rebase--continue- Continue after resolving conflicts

Example:

git rebase main
git mergetool

Open merge conflict resolution tool

Example:

git mergetool
git remote -v

List all configured remote repositories

Example:

git remote -v
git remote add <name> <url>

Add a new remote repository

Example:

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

Download objects and refs from a remote

Common Flags:

--all- Fetch all remotes--prune- Remove stale remote-tracking branches

Example:

git fetch origin
git pull <remote> <branch>

Fetch and merge changes from a remote branch

Common Flags:

--rebase- Rebase instead of merge--no-commit- Fetch and merge without committing

Example:

git pull origin main
git push <remote> <branch>

Upload local branch commits to remote repository

Common Flags:

-u- Set upstream for the branch--force- Force push (overwrite remote)--tags- Push all tags--delete- Delete a remote branch

Example:

git push -u origin feature/login
git reset <file>

Unstage a file while keeping changes

Common Flags:

--soft- Keep changes staged--hard- Discard all changes (dangerous)--mixed- Unstage changes (default)

Example:

git reset index.html
git checkout -- <file>

Discard local changes to a file

Example:

git checkout -- index.html
git restore <file>

Restore a file to last committed state (modern Git)

Common Flags:

-S- Restore and unstage--staged- Restore from staging area

Example:

git restore index.html
git revert <commit>

Create a new commit that undoes a previous commit

Example:

git revert abc123
git reset --hard <commit>

Reset to a specific commit and discard all changes

Example:

git reset --hard abc123
git stash

Temporarily store uncommitted changes

Common Flags:

push- Push changes to stash stack (default)-u- Include untracked files-a- Include all files (including ignored)

Example:

git stash
git stash list

List all stashed changes

Example:

git stash list
git stash pop

Apply and remove the latest stash

Example:

git stash pop
git stash apply

Apply changes from stash without removing it

Common Flags:

stash@{n}- Apply specific stash by name

Example:

git stash apply stash@{0}
git stash drop

Remove a stash without applying it

Example:

git stash drop stash@{0}
git stash clear

Remove all stashes

Example:

git stash clear
git log

Show commit history

Common Flags:

--oneline- Show one line per commit--graph- Show ASCII graph of branch structure-n <num>- Limit number of commits shown--author=<name>- Filter by author-p- Show patches (diff) for each commit

Example:

git log --oneline -10
git show <commit>

Show details of a specific commit

Common Flags:

--stat- Show file change statistics--name-only- Show only changed file names

Example:

git show abc123
git blame <file>

Show who changed each line of a file

Common Flags:

-L <start>,<end>- Show only specific line range

Example:

git blame index.html
git reflog

Show a log of all reference updates

Example:

git reflog
git shortlog

Show commits grouped by author

Common Flags:

-sn- Show number of commits per author--all- Show all branches

Example:

git shortlog -sn

Frequently Asked Questions

Tags

git
commands
reference
cheat sheet
version control