ZapKit

Git cheat sheet

Find the git command you need, especially the ones for undoing mistakes, explained in a sentence and ready to copy.

What is a git cheat sheet?

A git cheat sheet is a quick reference of git commands for the tasks developers do most: committing, branching, syncing with a remote and recovering from mistakes. Git has hundreds of commands and options, and even experienced developers regularly look up the less common ones.

This one lists over 40 commands grouped by topic, each with a one-line explanation. Search by command or by what you want to do, filter by topic, and copy any command with one click, then fill in its placeholders.

How to use the Git cheat sheet

  1. 1.Search

    By command or task, like undo, rename or stash.

  2. 2.Filter by topic

    Set up, everyday, branches, remotes, undo, releases and more.

  3. 3.Copy

    Replace the <placeholders> with your values.

Example: "I committed to the wrong branch"

Searching "undo" and combining three commands:

1
git reset --soft HEAD~1 (undo the commit, keep the changes)
2
git switch -c <branch> (move to the right branch)
3
git commit -m "message" (commit again there)

The work ends up on the right branch with nothing lost.

Undoing safely

Most git fears are about undoing. git restore discards uncommitted edits, git reset --soft HEAD~1 undoes a commit but keeps its changes, and git revert undoes a commit that's already shared by adding a new one. git reflog lists everywhere HEAD has been, so even commits lost to a hard reset can usually be recovered.

Commands that rewrite history, such as reset --hard, rebase and force pushes, are safe on your own branches but disruptive on shared ones. Prefer --force-with-lease to a plain --force.

Frequently asked questions

switch or checkout?

git switch and git restore split checkout's two jobs and are easier to get right.

merge or rebase?

Merge keeps history as it happened; rebase makes it linear. Never rebase commits others have pulled.

How do I rename the default branch?

git branch -m main, then push it and update the default on your host.

What does <url> mean?

Angle brackets mark something to replace with your own value.

More Developer tools