Visitar URL original
Animated Git cheat sheet | Learn Git

Free · Customize it · Download it · Print it

The Animated Git Cheat Sheet

We used git-sim to build an animated Git cheat sheet you can customize with just the commands you need.

(because only you know which commands you always forget...)

Filter
Safe nothing is lostCaution rewrites history, but you can get it backDestructive can lose work for good

Start a project4 commands

A repository is a project folder with its history in a hidden .git folder. Make a new one, or clone one that already exists, history and all.

Create a new repository

Safe
git init

Creates an empty Git repository in the current folder, stored in a hidden .git folder. No files are tracked until you add and commit them.

Loading the graph…
Shown: git init

Clone a repository

Safe
git clone <url>

Downloads the repository at <url> with its full history into a new folder, checks out its default branch, and adds the source as the remote origin.

Loading the graph…
Shown: git clone https://example.com/your_project.git

Clone only the N latest commits

Safe
git clone --depth <N> <url>

Clones only the <N> latest commits of the default branch, leaving out the older history.

Loading the graph…
Shown: git clone --depth 3 https://example.com/your_project.git

Clone a repository and check out a specific branch

Safe
git clone -b <branch> <url>

Clones the repository and checks out <branch> instead of the remote's default branch.

Loading the graph…
Shown: git clone -b feature https://example.com/your_project.git

Configure Git11 commands

Settings made with --global apply to all your repositories on this machine and are saved in ~/.gitconfig. Leave out --global to set something for the current repository only.

Set your author name

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

Sets the name Git records as the author and committer of each new commit.

Loading the graph…
Shown: git config user.name "Jacob Stopak"

Set your email address

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

Sets the email address Git records for the author and committer of each new commit.

Loading the graph…
Shown: git config --global user.email "jacob@initialcommit.io"
  • Undogit config --global --unset user.email

Set your default branch name

Safe
git config --global init.defaultBranch <name>

Sets the name git init gives the first branch of a new repository.

Loading the graph…
Shown: git config --global init.defaultBranch main

Set your text editor

Safe
git config --global core.editor "<editor>"

Sets the editor Git opens for commit messages and interactive rebases. For VS Code, use code --wait so Git waits until the file is closed.

Loading the graph…
Shown: git config --global core.editor "code --wait"

List all config settings

Safe
git config --list

Prints every setting from the system, global, and repository config files. When a setting is in more than one file, the repository value overrides the global value, and the global value overrides the system value.

Loading the graph…
Shown: git config --list

Set a setting for every user on the machine

Safe
git config --system <setting> <value>

Sets <setting> to <value> in the system config file, which applies to every user and repository on the machine. A global or repository value for the same setting overrides it.

Loading the graph…
Shown: git config --system core.autocrlf true
  • WatchWriting the system config file needs admin rights.

Create a shortcut for a command

Safe
git config --global alias.<name> <command>

Creates a Git command named <name> that runs <command>. For example, alias.st status makes git st run git status.

Loading the graph…
Shown: git config --global alias.st status

Rebase instead of merging when you pull

Safe
git config --global pull.rebase true

Makes git pull rebase your local commits onto the fetched commits instead of creating a merge commit.

Loading the graph…
Shown: git config --global pull.rebase true

Prune deleted remote branches on every fetch

Safe
git config --global fetch.prune true

Makes git fetch delete remote-tracking branches, like origin/<branch>, whose branches no longer exist on the remote.

Loading the graph…
Shown: git config --global fetch.prune true

Convert line endings on Windows

Safe
git config --global core.autocrlf true

Converts LF line endings to CRLF when files are checked out, and back to LF when they're committed.

Loading the graph…
Shown: git config --global core.autocrlf true

Save your Git credentials

Safe
git config --global credential.helper <helper>

Stores your username and password or token with <helper>, like manager on Windows or osxkeychain on macOS, so Git doesn't ask for them each time.

Loading the graph…
Shown: git config --global credential.helper cache

Stage and commit6 commands

Committing takes two steps: stage the changes you want, then commit them as one snapshot.

Check the state of your repository

Safe
git status

Shows the active branch, staged changes, unstaged changes, and untracked files. During a merge, rebase, or cherry-pick, it also shows that operation's state and the next step.

Loading the graph…
Shown: git status

Stage your file changes

Safe
git add <file>

Stages the current changes to <file>, so they're included in the next commit.

Loading the graph…
Shown: git add app.py notes.txt

Stage all changes in the current folder

Safe
git add .

Stages every new, modified, and deleted file in the current folder and its subfolders.

Loading the graph…
Shown: git add .

Stage all changes in the repository

Safe
git add -A

Stages every new, modified, and deleted file in the repository, whichever folder it's run from.

Loading the graph…
Shown: git add -A

Commit your staged changes

Safe
git commit -m "<message>"

Creates a new commit from the staged changes with the message <message>, and moves the active branch to it.

Loading the graph…
Shown: git commit -m "Describe the project in the README"
  • Undogit reset --soft HEAD~1

Stage and commit tracked file changes in one command

Safe
git commit -am "<message>"

Stages the changes to all tracked files and commits them with the message <message>. Untracked files aren't included.

Loading the graph…
Shown: git commit -a -m "Tweak the app and the header"
  • Undogit reset --soft HEAD~1

Manage files6 commands

Delete, rename, untrack, and ignore files, and clear out the ones Git doesn't track.

Delete a file and stage the deletion

Caution
git rm <file>

Deletes <file> from the working directory and stages the deletion.

Loading the graph…
Shown: git rm login.html
  • Undogit restore --staged --worktree <file>

Stop tracking a file but keep it on disk

Safe
git rm --cached <file>

Removes <file> from the staging area, so the next commit stops tracking it. The file stays in the working directory.

Loading the graph…
Shown: git rm --cached .env
  • Undogit add <file>

Rename or move a file

Safe
git mv <old> <new>

Renames or moves <old> to <new> and stages the change.

Loading the graph…
Shown: git mv app.py main.py
  • Undogit mv <new> <old>

Find the rule that ignores a file

Safe
git check-ignore -v <file>

Prints the ignore file, line number, and pattern that ignore <file>. Prints nothing if the file isn't ignored.

Loading the graph…
Shown: git check-ignore -v debug.log build/app.js app.py

Delete all untracked files

Destructive
git clean -f

Deletes the untracked files in the working directory. Add -d to delete untracked folders too.

Loading the graph…
Shown: git clean -f
  • UndoNo way back
  • WatchUntracked files aren't in Git's history, so they can't be recovered. Run git clean -n first to list what would be deleted.

Delete all untracked and ignored files

Destructive
git clean -fdx

Deletes all untracked files and folders, including ignored ones like build output and .env files.

Loading the graph…
Shown: git clean -fdx
  • UndoNo way back
  • WatchIgnored local files, like settings and secrets, are deleted too. Run git clean -ndx first to list what would be deleted.

History and search14 commands

Every commit keeps its author, date, and changes. These commands list them, filter them, and search through them. None of them change anything.

View the active branch's commit history

Safe
git log

Lists the commits in the active branch's history, newest first, with each commit's hash, author, date, and message.

Loading the graph…
Shown: git log

View the commit history of all branches

Safe
git log --all

Lists the commits in the history of every branch and tag, not only the active branch.

Loading the graph…
Shown: git log --all

View all branches as a text graph

Safe
git log --oneline --graph --all

Prints the history of all branches as a text graph, one line per commit, with branch and tag names next to their commits.

Loading the graph…
Shown: git log --oneline --graph --all

View every change to a file, commit by commit

Safe
git log -p <file>

Lists the commits that changed <file>, each with the lines it added and removed.

Loading the graph…
Shown: git log -p app.py

Follow a file's history through renames

Safe
git log --follow <file>

Lists the commits that changed <file>, including the ones from before it was renamed.

Loading the graph…
Shown: git log --follow main.py

Find the commits that added or removed a string

Safe
git log -S "<text>"

Lists the commits that added or removed <text>, found by checking where the number of times <text> appears changes.

Loading the graph…
Shown: git log -S "logging"

List one author's commits

Safe
git log --author "<name>"

Lists the commits whose author name or email matches <name>.

Loading the graph…
Shown: git log --author "Ada"

List the commits from a date range

Safe
git log --since "<date>" --until "<date>"

Lists the commits made between the two dates. Exact dates like 2024-05-01 and relative ones like 2 weeks ago both work.

Loading the graph…
Shown: git log --since "2024-05-01 11:00 +0000" --until "2024-05-01 13:00 +0000"

View a commit's data

Safe
git show <commit>

Shows a commit's hash, author, date, and message, followed by the changes it made.

Loading the graph…
Shown: git show HEAD~1

See who last changed each line of a file

Safe
git blame <file>

Prints each line of <file> with the commit, author, and date that last changed it.

Loading the graph…
Shown: git blame app.py

Count commits by author

Safe
git shortlog -sn

Lists each author who made commits on the active branch, with their number of commits, highest first.

Loading the graph…
Shown: git shortlog -sn

Describe a commit by its nearest tag

Safe
git describe

Names the current commit after the most recent annotated tag in its history, like v1.0-2-g1a2b3c4: the tag v1.0, 2 commits since it, and g followed by the commit's short hash.

Loading the graph…
Shown: git describe

Search all tracked files for text

Safe
git grep -n "<text>"

Searches every tracked file for <text> and prints each matching line with its file name and line number.

Loading the graph…
Shown: git grep -n logging

Find the commit that introduced a bug

Safe
git bisect start <bad> <good>

Starts a binary search between the <bad> and <good> commits by checking out the commit halfway between them. Test each commit Git checks out and mark it with git bisect good or git bisect bad until Git reports the first bad commit.

Loading the graph…
Shown: git bisect start HEAD 4114b2c

Compare changes5 commands

See exactly which lines changed between your files, the staging area, commits, and branches. Nothing is changed.

Show unstaged changes

Safe
git diff

Shows the changes in the working directory that aren't staged yet, line by line.

Loading the graph…
Shown: git diff

Show staged changes

Safe
git diff --staged

Shows the staged changes line by line, which is what the next commit will contain.

Loading the graph…
Shown: git diff --staged

Compare two commits

Safe
git diff <commit> <commit>

Shows the changes between two commits. Branch and tag names work in place of commit hashes.

Loading the graph…
Shown: git diff HEAD~2 HEAD

Compare two branches

Safe
git diff <branch>..<branch>

Shows the changes between the latest commits of the two branches.

Loading the graph…
Shown: git diff main..feature

Summarize the changes per file

Safe
git diff --stat

Lists each file with unstaged changes and the number of lines added and removed in it.

Loading the graph…
Shown: git diff --stat

Branches14 commands

A branch is a movable pointer to a commit, and HEAD points to the active branch.

Create a new branch without switching to it

Safe
git branch <name>

Creates a branch named <name> that points to the current commit. HEAD stays on the active branch.

Loading the graph…
Shown: git branch bugfix-header

List all local and remote-tracking branches

Safe
git branch -a

Lists local branches, then remote-tracking branches like remotes/origin/main. A * marks the active branch.

Loading the graph…
Shown: git branch -a

List branches with their upstream and ahead/behind counts

Safe
git branch -vv

Lists each local branch with its latest commit, its upstream branch, and how many commits it's ahead or behind, like [origin/main: ahead 1, behind 2].

Loading the graph…
Shown: git branch -vv

List branches already merged into the active branch

Safe
git branch --merged

Lists the branches whose latest commit is already in the active branch's history.

Loading the graph…
Shown: git branch --merged

List branches not yet merged into the active branch

Safe
git branch --no-merged

Lists the branches that have commits not in the active branch's history.

Loading the graph…
Shown: git branch --no-merged

Switch to another branch

Safe
git switch <branch>

Points HEAD at <branch> and updates the staging area and working directory to match its latest commit.

Loading the graph…
Shown: git switch feature

Switch back to the previous branch

Safe
git switch -

Switches to the branch that was active before the current one.

Loading the graph…
Shown: git switch -

Check out a branch from the remote

Safe
git switch <branch>

When no local branch named <branch> exists but origin/<branch> does, creates the local branch from it, sets it to track origin/<branch>, and switches to it.

Loading the graph…
Shown: git switch feature

Create a new branch and switch to it

Safe
git switch -c <name>

Creates a branch named <name> at the current commit and switches to it.

Loading the graph…
Shown: git switch -c hotfix

Create a new branch at a specific commit and switch to it

Safe
git switch -c <name> <start>

Creates a branch named <name> at <start>, which can be a commit hash, a tag, or a branch like origin/main, and switches to it.

Loading the graph…
Shown: git switch -c search origin/feature

Rename a branch

Safe
git branch -m <old> <new>

Renames the branch <old> to <new>. Its commits don't change.

Loading the graph…
Shown: git branch -m feature search
  • Undogit branch -m <new> <old>

Delete a merged branch

Safe
git branch -d <name>

Deletes the branch <name>. Git refuses if the branch has commits that aren't merged into its upstream branch, or into the active branch if it has no upstream.

Loading the graph…
Shown: git branch -d docs
  • Undogit branch <name> <hash>

Force-delete an unmerged branch

Destructive
git branch -D <name>

Deletes the branch <name> even if it has unmerged commits. Afterward, those commits are reachable only through the reflog.

Loading the graph…
Shown: git branch -D feature
  • Undogit branch <name> <hash>
  • WatchNote the hash Git prints. Git eventually deletes commits that nothing points to.

Check out a non-branch commit (detached HEAD)

Safe
git switch --detach <commit>

Points HEAD directly at <commit> instead of at a branch, and updates the working directory to match. Create a branch before committing there.

Loading the graph…
Shown: git switch --detach 96c4fc2

Merge6 commands

A merge brings another branch's work into yours and keeps the history as it happened.

Merge a branch into the active branch

Safe
git merge <branch>

Merges <branch> into the active branch. When both have new commits, Git creates a merge commit with two parents.

Loading the graph…
Shown: git merge feature
  • Undogit reset --hard ORIG_HEAD

Fast-forward the active branch

Safe
git merge <branch>

When the active branch has no commits that <branch> lacks, Git moves the active branch forward to <branch>'s latest commit. No merge commit is created.

Loading the graph…
Shown: git merge main

Force a merge commit even if a fast-forward merge is possible

Safe
git merge --no-ff <branch>

Creates a merge commit even when a fast-forward is possible, so the merged branch stays visible in the history.

Loading the graph…
Shown: git merge --no-ff main
  • Undogit reset --hard ORIG_HEAD

Squash a branch's changes into one commit

Safe
git merge --squash <branch>

Stages the combined changes from <branch> without committing or recording a merge. The next commit contains them as one commit.

Loading the graph…
Shown: git merge --squash feature

Finish a merge after fixing conflicts

Safe
git merge --continue

Creates the merge commit once the conflicts are resolved and the files are staged with git add.

Loading the graph…
Shown: git merge --continue

Abort a merge with conflicts

Caution
git merge --abort

Stops the merge and returns the branch, staging area, and working directory to their state before it started.

Loading the graph…
Shown: git merge --abort
  • UndoRun the merge again

Rebase and cherry-pick8 commands

A rebase replays your commits on top of another branch, so the history reads as one line. A cherry-pick replays single commits the same way.

Rebase the active branch onto another

Caution
git rebase <branch>

Replays the active branch's commits on top of <branch>, one at a time, as new commits with new hashes.

Loading the graph…
Shown: git rebase main
  • Undogit reset --hard ORIG_HEAD
  • WatchDon't rebase commits that others have already pulled.

Run an interactive rebase to edit, squash, or reorder commits

Caution
git rebase -i <base>

Opens an editable list of the commits after <base>. Change pick to reword, squash, or drop, or reorder the lines, and Git replays the commits as listed when you save and close the file.

Loading the graph…
Shown: git rebase -i main
  • Undogit reset --hard ORIG_HEAD

Move part of a branch onto a new base commit

Caution
git rebase --onto <new-base> <old-base>

Replays only the commits after <old-base> onto <new-base>. The commits before <old-base> stay where they are.

Loading the graph…
Shown: git rebase --onto main feature
  • Undogit reset --hard ORIG_HEAD

Continue a rebase after fixing conflicts

Safe
git rebase --continue

After the conflicts are resolved and staged, commits the stopped commit and continues replaying the remaining commits.

Loading the graph…
Shown: git rebase --continue

Skip the conflicting commit in a rebase

Caution
git rebase --skip

Skips the commit that caused the conflict and continues replaying the remaining commits.

Loading the graph…
Shown: git rebase --skip
  • WatchThe skipped commit's changes aren't applied at all.

Abort a rebase in progress

Caution
git rebase --abort

Stops the rebase and returns the branch to the commit it pointed to before the rebase started.

Loading the graph…
Shown: git rebase --abort
  • UndoRun the rebase again

Apply a commit from another branch to the active branch

Safe
git cherry-pick <commit>

Applies the changes from <commit> to the active branch as a new commit.

Loading the graph…
Shown: git cherry-pick fc19889
  • Undogit reset --hard HEAD~1

Apply a range of commits from another branch to the active branch

Safe
git cherry-pick <from>..<to>

Applies each commit after <from> up to and including <to>, in order, as new commits on the active branch.

Loading the graph…
Shown: git cherry-pick 96c4fc2..feature

Remotes8 commands

A remote is a name for the URL of another repository of the same project. A clone starts with one named origin.

Add a new remote

Safe
git remote add <name> <url>

Adds a remote named <name> for the repository at <url>, so you can fetch from it and push to it.

Loading the graph…
Shown: git remote add upstream ../your_project.git

List remotes with their URLs

Safe
git remote -v

Lists each remote with its fetch URL and push URL.

Loading the graph…
Shown: git remote -v

Show the details of a remote

Safe
git remote show <name>

Shows the remote's URLs, its branches, which of them are tracked, and what git pull and git push do for each local branch.

Loading the graph…
Shown: git remote show origin

Change a remote's URL

Safe
git remote set-url <name> <url>

Changes the URL of the remote <name> to <url>.

Loading the graph…
Shown: git remote set-url origin https://example.com/your_project.git
  • Undogit remote set-url <name> <old-url>

Rename a remote

Safe
git remote rename <old> <new>

Renames the remote <old> to <new>, along with its remote-tracking branches and settings.

Loading the graph…
Shown: git remote rename origin upstream
  • Undogit remote rename <new> <old>

Remove a remote

Caution
git remote remove <name>

Removes the remote <name> with its remote-tracking branches and settings. The remote repository itself isn't affected.

Loading the graph…
Shown: git remote remove origin
  • Undogit remote add <name> <url>

List a remote's branches and tags without fetching

Safe
git ls-remote

Lists the remote's branches and tags and the commit each points to, without downloading anything.

Loading the graph…
Shown: git ls-remote

Set the upstream branch of the active branch

Safe
git branch -u origin/<branch>

Sets origin/<branch> as the active branch's upstream, so plain git pull and git push use it.

Loading the graph…
Shown: git branch -u origin/feature

Fetch, pull, and push12 commands

Fetch and pull bring a remote's new commits down, and push sends yours up.

Fetch new commits from the remote, without merging

Safe
git fetch

Downloads new commits from the remote and updates its remote-tracking branches, like origin/main. Local branches and the working directory don't change.

Loading the graph…
Shown: git fetch

Fetch new commits on one branch from a remote

Safe
git fetch <remote> <branch>

Downloads only <branch>'s new commits from <remote> and updates <remote>/<branch>. Local branches and the working directory don't change.

Loading the graph…
Shown: git fetch origin main

Fetch new commits on all branches from all remotes

Safe
git fetch --all

Fetches from every remote, like origin and upstream, and updates all their remote-tracking branches.

Loading the graph…
Shown: git fetch --all

Fetch and prune deleted remote branches

Safe
git fetch --prune

Fetches, then deletes remote-tracking branches, like origin/<branch>, whose branches no longer exist on the remote.

Loading the graph…
Shown: git fetch --prune

Fetch and merge remote commits

Safe
git pull

Fetches the active branch's upstream and merges it into the active branch. When both have new commits, Git creates a merge commit.

Loading the graph…
Shown: git pull
  • Undogit reset --hard ORIG_HEAD

Pull a specific branch from the remote

Safe
git pull origin <branch>

Fetches <branch> from origin and merges it into the active branch, even if the active branch tracks a different branch.

Loading the graph…
Shown: git pull origin main
  • Undogit reset --hard ORIG_HEAD

Pull and rebase instead of merging

Caution
git pull --rebase

Fetches the active branch's upstream, then replays your local commits on top of it instead of merging.

Loading the graph…
Shown: git pull --rebase

Push the active branch's commits

Safe
git push

Sends the active branch's new commits to its upstream branch on the remote.

Loading the graph…
Shown: git push

Push a new branch and set its upstream

Safe
git push -u origin <branch>

Creates <branch> on origin, pushes its commits, and sets it as the local branch's upstream.

Loading the graph…
Shown: git push -u origin hotfix

Force-push only if the remote branch hasn't changed

Caution
git push --force-with-lease

Overwrites the remote branch with your local branch, but only if the remote branch hasn't changed since your last fetch.

Loading the graph…
Shown: git push --force-with-lease
  • WatchIt still rewrites history that others may have pulled.

Force-push and overwrite the remote branch

Destructive
git push --force

Overwrites the remote branch with your local branch, even if the remote has commits you don't have.

Loading the graph…
Shown: git push --force
  • UndoNo way back
  • WatchCommits that others pushed are removed from the remote. Use --force-with-lease instead.

Delete a remote branch

Destructive
git push origin --delete <branch>

Deletes <branch> on the remote. The local branch isn't affected.

Loading the graph…
Shown: git push origin --delete feature
  • Undogit push origin <branch>

Tags7 commands

A tag is a fixed pointer to a commit, usually for a release like v1.0. Unlike a branch it doesn't move, and it isn't sent to a remote until you push it.

Create a lightweight tag

Safe
git tag <name>

Creates a lightweight tag named <name> that points to the current commit.

Loading the graph…
Shown: git tag v1.0

Create an annotated tag with a message

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

Creates an annotated tag named <name> on the current commit, storing the tagger, the date, and <message>.

Loading the graph…
Shown: git tag -a v1.0 -m "First release"
  • Undogit tag -d <name>

List all tags

Safe
git tag -l

Lists all tags in alphabetical order.

Loading the graph…
Shown: git tag -l

Delete a local tag

Caution
git tag -d <name>

Deletes the local tag <name>. The commit it pointed to isn't affected.

Loading the graph…
Shown: git tag -d v1.0

Push a single tag

Safe
git push origin <tag>

Pushes the tag <tag>, and any commits it needs, to the remote.

Loading the graph…
Shown: git push origin v1.1
  • Undogit push origin --delete <tag>

Push all tags

Safe
git push --tags

Pushes every local tag the remote doesn't have yet.

Loading the graph…
Shown: git push --tags

Delete a remote tag

Caution
git push origin --delete <tag>

Deletes the tag <tag> on the remote. The local tag isn't affected.

Loading the graph…
Shown: git push origin --delete v1.0
  • Undogit push origin <tag>
  • WatchAnyone who already fetched the tag still has it.

Stash11 commands

The stash saves changes you're not ready to commit and cleans the working directory, so you can switch branches and reapply the changes later.

Stash your uncommitted changes

Safe
git stash

Saves the staged and unstaged changes to tracked files as a new stash entry, and resets the staging area and working directory to the last commit.

Loading the graph…
Shown: git stash
  • Undogit stash pop

Stash changes including untracked files

Safe
git stash -u

Stashes untracked files as well as the changes to tracked files.

Loading the graph…
Shown: git stash -u
  • Undogit stash pop

Stash changes with a message

Safe
git stash push -m "<message>"

Stashes the changes as an entry with the message <message>, which git stash list shows.

Loading the graph…
Shown: git stash push -m "Try a grid header"
  • Undogit stash pop

Stash changes to a specific file or file(s)

Safe
git stash push <file>

Stashes only the changes to <file>, leaving all other changes in place.

Loading the graph…
Shown: git stash push styles.css
  • Undogit stash pop

List all stash entries

Safe
git stash list

Lists every stash entry, newest first, as stash@{0}, stash@{1}, and so on.

Loading the graph…
Shown: git stash list

Apply and drop the latest stash entry

Safe
git stash pop

Applies the latest stash entry to the working directory and removes it from the stash. If applying it conflicts, the entry is kept.

Loading the graph…
Shown: git stash pop

Apply the latest stash entry and keep it

Safe
git stash apply

Applies the latest stash entry to the working directory and keeps it on the stash.

Loading the graph…
Shown: git stash apply

Drop a stash entry

Caution
git stash drop

Deletes the latest stash entry, or the one named, like stash@{1}.

Loading the graph…
Shown: git stash drop
  • WatchGit prints the dropped entry's hash. git stash apply <hash> restores it until Git deletes it.

Show the changes in a stash entry

Safe
git stash show -p

Shows the changes in the latest stash entry, line by line. Name an entry to see another, like stash@{1}.

Loading the graph…
Shown: git stash show -p

Create a new branch from a stash entry

Safe
git stash branch <name>

Creates the branch <name> at the commit the latest stash entry was created on, applies the entry there, and drops it if it applied cleanly.

Loading the graph…
Shown: git stash branch try-hello
  • Undogit stash

Delete all stash entries

Destructive
git stash clear

Deletes every stash entry.

Loading the graph…
Shown: git stash clear
  • UndoNo way back
  • WatchGit doesn't print the entries' hashes, so recovering them is difficult. Check git stash list first.

Undo and recover16 commands

Most mistakes in Git can be undone, as long as you know which command undoes what. The badge on each card says what's at stake.

Unstage a file's changes

Safe
git restore --staged <file>

Unstages the changes to <file>, so they're not in the next commit. The file in the working directory doesn't change.

Loading the graph…
Shown: git restore --staged README.md

Discard unstaged changes to a file

Destructive
git restore <file>

Discards the unstaged changes to <file>, restoring it from the staging area.

Loading the graph…
Shown: git restore app.py
  • UndoNo way back
  • WatchUnstaged edits aren't saved in Git, so they can't be recovered. Run git stash first if you're not sure.

Discard all unstaged changes

Destructive
git restore .

Discards the unstaged changes to every file in the current folder and its subfolders. Staged changes are kept.

Loading the graph…
Shown: git restore .
  • UndoNo way back
  • WatchDiscarded edits can't be recovered. Untracked files aren't affected, so use git clean for those.

Discard all unstaged changes (older Git)

Destructive
git checkout -- .

Discards the unstaged changes in the current folder, like git restore . does. The -- marks what follows as file paths, not a branch.

Loading the graph…
Shown: git checkout -- .
  • UndoNo way back
  • WatchDiscarded edits can't be recovered.

Restore a file from an older commit

Destructive
git restore --source <commit> <file>

Replaces <file> in the working directory with its version from <commit>. Commit the file to keep that version.

Loading the graph…
Shown: git restore --source HEAD~1 app.py
  • Undogit restore <file>
  • WatchUncommitted changes to <file> can't be recovered.

Amend the last commit

Caution
git commit --amend -m "<message>"

Replaces the last commit with a new commit that includes any staged changes and has the message <message>. Leave out -m to edit the existing message.

Loading the graph…
Shown: git commit --amend -m "Update dependencies and README"
  • Undogit reset --soft HEAD@{1}
  • WatchDon't amend a commit that others have already pulled.

Undo the N latest commits, keep their changes staged

Caution
git reset --soft HEAD~<N>

Moves the active branch back <N> commits (HEAD~1 undoes only the last one). Their changes stay staged.

Loading the graph…
Shown: git reset --soft HEAD~1
  • Undogit reset --soft ORIG_HEAD

Undo the N latest commits, keep their changes unstaged

Caution
git reset HEAD~<N>

Moves the active branch back <N> commits (HEAD~1 undoes only the last one) and unstages their changes. The working directory doesn't change.

Loading the graph…
Shown: git reset HEAD~1
  • Undogit reset ORIG_HEAD

Reset the branch and discard all changes

Destructive
git reset --hard <commit>

Moves the active branch to <commit> and resets the staging area and working directory to match it.

Loading the graph…
Shown: git reset --hard HEAD~2
  • Undogit reset --hard ORIG_HEAD
  • WatchUncommitted changes can't be recovered. The commits can be recovered until Git deletes them.

Reset your branch to match the remote

Destructive
git reset --hard origin/<branch>

Moves the active branch to origin/<branch> and resets the staging area and working directory to match, discarding local commits and changes. Run git fetch first.

Loading the graph…
Shown: git reset --hard origin/main
  • Undogit reset --hard ORIG_HEAD
  • WatchUncommitted changes can't be recovered, and unpushed commits remain only in the reflog.

Revert a commit with a new commit

Safe
git revert <commit>

Creates a new commit that reverses the changes from <commit>. Existing commits aren't rewritten, so it's safe on shared branches.

Loading the graph…
Shown: git revert HEAD
  • Undogit revert <the new commit>

Revert a merge commit

Safe
git revert -m 1 <merge>

Creates a new commit that reverses the changes a merge brought in. -m 1 makes Git reverse them relative to the merge's first parent, the branch the merge was made on.

Loading the graph…
Shown: git revert -m 1 HEAD
  • Undogit revert <the revert commit>

Revert several commits in one commit

Safe
git revert --no-commit <from>..<to>

Reverses each commit after <from> up to and including <to> and stages the result without committing, so one commit can undo them all.

Loading the graph…
Shown: git revert --no-commit HEAD~2..HEAD
  • Undogit revert --abort

Find a lost commit in the reflog

Safe
git reflog

Lists every commit HEAD has pointed to, newest first, including commits that no branch points to anymore.

Loading the graph…
Shown: git reflog

Undo a hard reset

Destructive
git reset --hard HEAD@{1}

HEAD@{1} is the commit HEAD pointed to before its last move, so this resets the branch to where it was before the reset.

Loading the graph…
Shown: git reset --hard HEAD@{1}
  • WatchLike any reset --hard, it discards uncommitted changes.

Restore a deleted branch

Safe
git branch <name> <hash>

Creates the branch <name> at <hash>, its last commit. Git prints the hash when it deletes a branch, and git reflog shows it too.

Loading the graph…
Shown: git branch feature 1117a34

Nothing matches that search.

More Git cheat sheets

All cheat sheets