Git Command Cheatsheet

58 commands

git init

Initialize the current directory as a Git repository

git clone <url>

Clone a remote repository to your local machine

git clone --depth 1 <url>

Clone only the latest snapshot without history (fast)

git add <file>

Stage a specific file for the next commit

git add .

Stage all modified and new files

git add -p

Interactively stage changes hunk by hunk

git restore --staged <file>

Remove a file from the staging area (keeps changes)

git status

Show the current state of the working tree and staging area

git diff

Show unstaged changes since the last commit

git diff --staged

Show staged changes ready to be committed

git commit -m "<message>"

Commit staged changes with a message

git commit --amend

Edit the most recent commit message or content

git commit --amend --no-edit

Add staged changes to the last commit without changing its message

git commit -am "<message>"

Stage all tracked files and commit in one step

git branch

List all local branches

git branch -a

List all local and remote branches

git branch <name>

Create a new branch (does not switch to it)

git switch <branch>

Switch to an existing branch

git switch -c <branch>

Create a new branch and switch to it immediately

git branch -d <branch>

Delete a merged branch (safe delete)

git branch -m <old> <new>

Rename a branch

git merge <branch>

Merge the specified branch into the current branch

git merge --no-ff <branch>

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

git rebase <branch>

Rebase the current branch onto another

git rebase -i HEAD~<n>

Interactively rewrite the last n commits (squash, reorder, etc.)

git cherry-pick <commit>

Apply the changes from a specific commit to the current branch

git remote -v

List remote connections with their URLs

git remote add origin <url>

Add a remote repository named origin

git fetch origin

Download changes from the remote without merging

git pull origin <branch>

Fetch and merge changes from the remote branch

git pull --rebase origin <branch>

Fetch and rebase instead of merge (cleaner history)

git push origin <branch>

Push the local branch to the remote

git push -u origin <branch>

Push and set the upstream tracking branch at the same time

git push --force-with-lease

Force push safely without overwriting others' changes

git log

Show the commit history

git log --oneline --graph

Show a compact, visual graph of commit history

git log -p <file>

Show commit history and diffs for a specific file

git show <commit>

Show details and changes of a specific commit

git blame <file>

Show who last modified each line of a file

git stash

Temporarily save uncommitted changes

git stash pop

Restore the most recent stash and remove it from the stash list

git stash list

List all saved stashes

git stash drop stash@{<n>}

Delete a specific stash entry

git tag

List all tags

git tag <name>

Create a lightweight tag at the current HEAD

git tag -a <name> -m "<msg>"

Create an annotated tag with a message (recommended for releases)

git push origin <tag>

Push a specific tag to the remote

git tag -d <tag>

Delete a local tag

git restore <file>

Discard changes in a file and restore it to HEAD

git restore .

Discard all working tree changes (irreversible)

git revert <commit>

Create a new commit that undoes a specific commit (history-safe)

git reset --soft HEAD~1

Undo the last commit, keeping changes staged

git reset --hard HEAD~1

Permanently delete the last commit and its changes (irreversible)

git reflog

Show the history of HEAD movements (useful for recovering lost commits)

git config --global user.name "<name>"

Set your Git username globally

git config --global user.email "<email>"

Set your Git email address globally

git config --list

List all current Git configuration settings

git config --global alias.<name> "<cmd>"

Create a Git command shortcut (alias)

How to Use

  1. STEP 1
    Click a category button to filter commands by topic (commit, branch, remote, etc.).
  2. STEP 2
    Type a keyword in the search box to narrow results in real-time.
  3. STEP 3
    Click the copy button on any command card to copy it to your clipboard.
  • Replace placeholders like '<'url'>', '<'branch'>', and '<'file'>' with actual values before running commands.
  • Commands like git reset --hard and git restore . are destructive and irreversible—double-check before running.

Tips

1

Use git log --oneline --graph --all to see a compact view of all branches and their history.

2

git reflog is invaluable for recovering commits lost after a reset or accidental branch deletion.

3

Prefer git push --force-with-lease over --force to avoid accidentally overwriting teammates' work.

FAQ

  • Q1

    What's the difference between git checkout and git switch?

    git switch (added in Git 2.23) is dedicated to switching branches, while git checkout handles both branch switching and file restoration. Using switch and restore separately makes intent clearer and is now recommended.

  • Q2

    Where should I start when I'm confused about a command?

    Run git help '<'command'>' or git '<'command'>' --help to open the manual. Starting with git status to see the current repository state is always a good first step.