Free · customizable · printable

The Animated Git Cheat Sheet

Stuck on a Git command or need a quick refresher? We used git-sim to build an animated cheat sheet with every Git command you could possibly 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 copy one that already exists.

Create a repository

Safe
git init

Turns the current folder into a Git repository with an empty history. Nothing is tracked until you add and commit it.

Loading the graph…
Shown: git initArticle

Copy a repository

Safe
git clone <url>

Downloads a repository with its whole history into a new folder, and remembers where it came from as origin.

Loading the graph…
Shown: git clone ../your_project.git your_project_copyArticle

Copy only the latest commits

Safe
git clone --depth 1 <url>

Makes a shallow clone with just the newest commit, so a big repository downloads fast. Any number of commits works.

Loading the graph…
Shown: git clone --depth 3 ../your_project.git your_project_copy

Clone and check out a 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 ../your_project.git your_project_copy

Configure Git10 commands

Settings made with --global apply to every repository on your computer and live in ~/.gitconfig. Leave out --global to set something for the current repository only.

Set your name for commits

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

Every commit records who made it. Set your name and email once per computer.

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

Set your email for commits

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

Records your email in every commit you make. Use the one your Git host knows, so it links the commits to your account.

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

Name the first branch main

Safe
git config --global init.defaultBranch main

Makes git init start every new repository on a branch called main instead of master.

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

Choose Git's text editor

Safe
git config --global core.editor "code --wait"

Sets the editor Git opens for commit messages and interactive rebases. This one is VS Code.

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

See all your settings

Safe
git config --list

Prints every setting Git is using here, from the system, global and repository config files.

Loading the graph…
Shown: git config --list

Make a shortcut for a command

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

Creates your own Git command. After alias.st status, typing git st runs git status.

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

Rebase instead of merge on pull

Safe
git config --global pull.rebase true

Makes every git pull rebase your local commits on top of what came down, with no merge commits.

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

Clean up deleted branches on every fetch

Safe
git config --global fetch.prune true

Makes every git fetch remove origin/<branch> labels for branches deleted on the remote.

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

Handle line endings across Windows and Mac

Safe
git config --global core.autocrlf true

On Windows, Git converts line endings to CRLF when checking files out and back to LF when committing.

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

Remember your login

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

Saves your username and password or token, so Git stops asking on every push and pull.

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

Stage and commit4 commands

Git saves your work in two moves: you stage the changes you want, then commit them as one snapshot.

See what's changed

Safe
git status

Lists what's staged for the next commit, what's changed but not staged, and files Git isn't tracking yet.

Loading the graph…
Shown: git statusArticle

Stage files

Safe
git add <file>

Puts the file's current changes in the staging area, ready for the next commit.

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

Commit your staged changes

Safe
git commit -m "<message>"

Saves everything in the staging area as a new commit, and moves the current branch to it.

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

Stage and commit tracked files at once

Safe
git commit -am "<message>"

Stages every change to files Git already tracks, then commits. New files still need git add first.

Loading the graph…
Shown: git commit -a -m "Tweak the app and the header"Article
  • 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 from the repository

Caution
git rm <file>

Deletes the file and stages the deletion, so the next commit removes it from the project.

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

Stop tracking a file but keep it

Safe
git rm --cached <file>

Takes the file out of Git but leaves it on disk. Add it to .gitignore so it stays out.

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

Rename or move a file

Safe
git mv <old> <new>

Renames the file and stages the rename in one step.

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

Find out why a file is ignored

Safe
git check-ignore -v <file>

Prints the .gitignore file, line number and rule that ignores the file. Prints nothing if it isn't ignored.

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

Delete untracked files

Destructive
git clean -f

Deletes every file Git isn't tracking. Add -d to delete untracked folders too.

Loading the graph…
Shown: git clean -fArticle
  • UndoNo way back
  • WatchUntracked files were never in Git, so they're gone for good. Run git clean -n first to preview.

Delete untracked and ignored files

Destructive
git clean -fdx

Deletes every untracked file and folder, including the ones .gitignore hides, like build output and .env files.

Loading the graph…
Shown: git clean -fdx
  • UndoNo way back
  • WatchThis deletes your local settings and secrets too. Run git clean -ndx first to preview.

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 commit history

Safe
git log

Lists the commits on the current branch, newest first, with their hash, author, date and message.

Loading the graph…
Shown: git logArticle

View the history of every branch

Safe
git log --all

Includes the commits of every branch, not just the one you're on.

Loading the graph…
Shown: git log --all

See every branch as a graph

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

Draws the history of every branch as a text graph, one line per commit, with the branch labels on it.

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

See how a file changed, commit by commit

Safe
git log -p <file>

Lists the commits that changed the 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>

Keeps listing the file's commits past the point where it was renamed. Without --follow the history stops there.

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

Find the commit that added or removed some code

Safe
git log -S "<text>"

Lists the commits that changed how many times <text> appears, which is where it was added or removed.

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

See one person's commits

Safe
git log --author "<name>"

Lists only the commits whose author's name or email contains <name>.

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

See the commits from a time range

Safe
git log --since "2 weeks ago" --until "yesterday"

Lists only the commits made in that window. Dates like 2024-05-01 work too.

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 message and the changes it made.

Loading the graph…
Shown: git show HEAD~1Article

See who changed each line

Safe
git blame <file>

Prints the file with the commit, author and date that last changed each line.

Loading the graph…
Shown: git blame app.pyArticle

Count commits by author

Safe
git shortlog -sn

Lists everyone who made commits on the current branch, with how many each, most first.

Loading the graph…
Shown: git shortlog -sn

Name a commit after the nearest tag

Safe
git describe

Prints a name like v1.0-2-g1a2b3c4: the latest tag, how many commits since it, and the commit's short hash.

Loading the graph…
Shown: git describe

Search the code in your repository

Safe
git grep -n "<text>"

Finds <text> in every tracked file and prints each matching line with its file and line number.

Loading the graph…
Shown: git grep -n logging

Find the commit that broke something

Safe
git bisect start <bad> <good>

Checks out the commit halfway between a bad one and a good one. Mark each test with git bisect good or git bisect bad until Git names the culprit.

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

Compare changes5 commands

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

See unstaged changes

Safe
git diff

Shows the lines you've changed but haven't staged yet.

Loading the graph…
Shown: git diffArticle

See staged changes

Safe
git diff --staged

Shows the lines you've staged, which is exactly what the next commit will contain.

Loading the graph…
Shown: git diff --staged

Compare two commits

Safe
git diff <commit> <commit>

Shows everything that changed between two commits, branches or tags.

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

Compare two branches

Safe
git diff <branch>..<branch>

Shows every difference between the tips of the two branches.

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

Summarize what changed

Safe
git diff --stat

Lists each changed file with how many lines were added and removed, instead of the lines themselves.

Loading the graph…
Shown: git diff --stat

Branches14 commands

A branch is a movable label on a commit, and HEAD marks the one you're on. Branches are cheap, so make one for every piece of work.

Create a branch

Safe
git branch <name>

Puts a new branch label on the current commit. You stay on the branch you're on.

Loading the graph…
Shown: git branch bugfix-headerArticle

List all branches, remote ones too

Safe
git branch -a

Lists your local branches, then the remote-tracking ones like remotes/origin/main. The * marks the one you're on.

Loading the graph…
Shown: git branch -a

See which branches are ahead or behind

Safe
git branch -vv

Lists each branch with its latest commit and the remote branch it tracks, like [origin/main: ahead 1, behind 2].

Loading the graph…
Shown: git branch -vv

List branches that are safe to delete

Safe
git branch --merged

Lists the branches whose commits are all in the current branch already.

Loading the graph…
Shown: git branch --merged

List branches with unmerged work

Safe
git branch --no-merged

Lists the branches that have commits the current branch doesn't. Deleting one needs -D.

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

Switch branches

Safe
git switch <branch>

Moves HEAD to the branch and updates your files to match its latest commit.

Loading the graph…
Shown: git switch featureArticle

Switch back to the previous branch

Safe
git switch -

Goes back to whichever branch you were on before this one. Run it again to flip back.

Loading the graph…
Shown: git switch -

Check out a teammate's branch

Safe
git switch <branch>

When only origin/<branch> exists, Git creates a local branch from it that tracks it, and switches to it.

Loading the graph…
Shown: git switch feature

Create a branch and switch to it

Safe
git switch -c <name>

Creates the branch on the current commit and switches to it in one step.

Loading the graph…
Shown: git switch -c hotfixArticle

Create a branch from another commit

Safe
git switch -c <name> <start>

Creates the branch at <start>, which can be a commit, 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>

Moves the label to a new name. The commits don't change.

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

Delete a merged branch

Safe
git branch -d <name>

Removes the label. Git refuses if the branch has commits that aren't merged anywhere.

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

Force-delete an unmerged branch

Destructive
git branch -D <name>

Removes the label even when its commits aren't merged. Only the reflog still knows them.

Loading the graph…
Shown: git branch -D featureArticle
  • Undogit branch <name> <hash>
  • WatchNote the hash Git prints. Unreachable commits are cleaned up for good eventually.

Check out an old commit

Safe
git switch --detach <commit>

Moves HEAD onto the commit itself, not a branch (a detached HEAD). Good for looking around.

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

Merge6 commands

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

Merge a branch into yours

Safe
git merge <branch>

Brings the branch's commits into yours with a merge commit that has two parents.

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

Fast-forward to a branch

Safe
git merge <branch>

When your branch has nothing of its own, Git just slides its label forward (a fast-forward). No merge commit.

Loading the graph…
Shown: git merge mainArticle

Merge with a merge commit, always

Safe
git merge --no-ff <branch>

Makes a merge commit even when a fast-forward would do, so the branch still shows in history.

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

Squash a branch into one commit

Safe
git merge --squash <branch>

Stages all of the branch's changes without committing. Your next commit holds them as one.

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

Finish a merge after fixing conflicts

Safe
git merge --continue

Once you've fixed the conflicted files and staged them with git add, this makes the merge commit.

Loading the graph…
Shown: git merge --continueArticle

Abort a conflicted merge

Caution
git merge --abort

Stops the merge and puts everything back the way it was before it started.

Loading the graph…
Shown: git merge --abortArticle
  • 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 copies single commits the same way.

Rebase your branch onto another

Caution
git rebase <branch>

Copies your branch's commits onto the tip of <branch>, one by one, as new commits with new hashes.

Loading the graph…
Shown: git rebase mainArticle
  • Undogit reset --hard ORIG_HEAD
  • WatchDon't rebase commits you've already pushed and others use.

Edit, squash or reorder your commits

Caution
git rebase -i <base>

Opens a list of your commits after <base>. Change pick to squash, reword or drop, or reorder the lines.

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

Move part of a branch to a new base

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

Moves only the commits after <old-base> onto <new-base>, leaving the rest behind.

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

Continue a rebase after fixing conflicts

Safe
git rebase --continue

Once the conflict is fixed and staged, this commits it and replays the rest.

Loading the graph…
Shown: git rebase --continueArticle

Skip the conflicting commit in a rebase

Caution
git rebase --skip

Leaves out the commit that stopped the rebase and carries on with the rest.

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

Abort a rebase

Caution
git rebase --abort

Stops the rebase and returns the branch to where it was before it started.

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

Copy a commit onto your branch

Safe
git cherry-pick <commit>

Applies the changes of one commit from anywhere as a new commit on your branch.

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

Copy a range of commits

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

Copies every commit after <from> up to <to>, in order.

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

Remotes8 commands

A remote is a named link to another copy of the repository, usually on GitHub or a server. A clone starts with one called origin.

Add a remote repository

Safe
git remote add <name> <url>

Gives a remote repository a short name, so you can fetch from it and push to it.

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

List your remotes and their URLs

Safe
git remote -v

Lists each remote with the URL Git fetches from and the one it pushes to.

Loading the graph…
Shown: git remote -v

See the details of a remote

Safe
git remote show origin

Asks the remote for its branches and shows which ones you track, which are new, and what git pull and git push do for each of yours.

Loading the graph…
Shown: git remote show origin

Change a remote's URL

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

Points the remote at a new address, like after a repository moves or you switch from HTTPS to SSH.

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

Rename a remote

Safe
git remote rename <old> <new>

Renames the remote, and its remote-tracking branches and branch settings with it.

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

Remove a remote

Caution
git remote remove <name>

Deletes the link to the remote and its remote-tracking branches. The remote repository itself isn't touched.

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

See a remote's branches and tags without fetching

Safe
git ls-remote

Asks the remote for every branch and tag it has and the commit each points to. Nothing is downloaded.

Loading the graph…
Shown: git ls-remote

Link a branch to a remote branch

Safe
git branch -u origin/<branch>

Sets the remote branch your current branch tracks, so plain git pull and git push know where to go.

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

Fetch, pull and push11 commands

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

Download new commits without merging

Safe
git fetch

Downloads what's new on the remote and updates origin/<branch>. Your own branches and files don't change.

Loading the graph…
Shown: git fetchArticle

Fetch from every remote

Safe
git fetch --all

Fetches from each remote in turn, like origin and upstream in a fork, and updates all their remote-tracking branches.

Loading the graph…
Shown: git fetch --all

Clean up deleted remote branches

Safe
git fetch --prune

Fetches, then removes origin/<branch> labels for branches that were deleted on the remote.

Loading the graph…
Shown: git fetch --pruneArticle

Pull and merge remote changes

Safe
git pull

Fetches, then merges the remote branch into yours. Makes a merge commit when both sides have new commits.

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

Pull a specific remote branch

Safe
git pull origin <branch>

Fetches <branch> from origin and merges it into the branch you're on, whatever that branch tracks.

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

Pull and rebase, without a merge commit

Caution
git pull --rebase

Fetches, then replays your local commits on top of what came down, so history stays in one line.

Loading the graph…
Shown: git pull --rebaseArticle

Push your commits

Safe
git push

Sends your branch's new commits to the remote branch it tracks.

Loading the graph…
Shown: git pushArticle

Publish a new branch

Safe
git push -u origin <branch>

Creates the branch on the remote and links yours to it, so plain git push and git pull work from then on.

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

Force-push safely

Caution
git push --force-with-lease

Replaces the remote branch with yours after a rebase or amend, but refuses if someone pushed since you last fetched.

Loading the graph…
Shown: git push --force-with-leaseArticle
  • WatchIt still rewrites shared history. Tell whoever else uses the branch.

Force-push over the remote

Caution
git push --force

Replaces the remote branch with yours, whatever is there.

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

Delete a remote branch

Destructive
git push origin --delete <branch>

Removes the branch from the remote. Your local branch stays.

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

Tags7 commands

A tag is a permanent label on a commit, usually a release like v1.0. Unlike a branch it never moves, and it stays in your repository until you push it.

Tag a commit as a release

Safe
git tag <name>

Puts a permanent label, like v1.0, on the current commit. Unlike a branch, a tag never moves.

Loading the graph…
Shown: git tag v1.0Article

Tag a release with a message

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

Makes an annotated tag, which stores who tagged it, when, and a message. Use these for releases.

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

List your tags

Safe
git tag -l

Lists every tag in alphabetical order.

Loading the graph…
Shown: git tag -l

Delete a tag

Caution
git tag -d <name>

Removes the tag from your repository. The commit stays.

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

Push one tag

Safe
git push origin <tag>

Sends a single tag, and any commits it needs, to the remote.

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

Push your tags

Safe
git push --tags

Tags stay local until you push them. This sends every tag the remote doesn't have.

Loading the graph…
Shown: git push --tagsArticle

Delete a remote tag

Destructive
git push origin --delete <tag>

Removes the tag from the remote. Your local tag stays until you run git tag -d <tag>.

Loading the graph…
Shown: git push origin --delete v1.0
  • Undogit push origin <tag>
  • WatchAnyone who already fetched the tag keeps their copy.

Stash11 commands

The stash is a shelf for changes you're not ready to commit, so you can switch branches with a clean working directory.

Stash your changes

Safe
git stash

Saves your staged and unstaged changes on the stash and resets the working directory to the last commit.

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

Stash untracked files too

Safe
git stash -u

Stashes untracked files along with your changes. Plain git stash leaves them where they are.

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

Stash with a description

Safe
git stash push -m "<message>"

Stashes your changes under a message of your own, so git stash list tells you what each entry is.

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

Stash only some files

Safe
git stash push <file>

Stashes the changes to the files you name and leaves every other change where it is.

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

List your stashes

Safe
git stash list

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

Loading the graph…
Shown: git stash listArticle

Apply and remove the latest stash

Safe
git stash pop

Applies the newest entry to your files and removes it from the stash.

Loading the graph…
Shown: git stash popArticle

Apply a stash but keep it

Safe
git stash apply

Applies the newest entry but leaves it on the stash, so you can apply it again elsewhere.

Loading the graph…
Shown: git stash applyArticle

Delete a stash

Caution
git stash drop

Deletes the newest entry. Name one to drop, like stash@{1}.

Loading the graph…
Shown: git stash dropArticle
  • WatchGit prints the dropped entry's hash. git stash apply <hash> brings it back until Git cleans it up.

See what's in a stash

Safe
git stash show -p

Shows the changes the newest entry holds, line by line. Name an entry to look at another, like stash@{1}.

Loading the graph…
Shown: git stash show -p

Turn a stash into a branch

Safe
git stash branch <name>

Creates a branch at the commit you stashed from, applies the entry there and drops it. Good when the stash no longer applies cleanly.

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

Delete every stash

Destructive
git stash clear

Empties the stash, deleting every entry at once.

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

Worktrees3 commands

A worktree is a second folder checked out from the same repository, so you can work on two branches at once without switching back and forth.

Work on two branches at once

Safe
git worktree add <folder> <branch>

Checks the branch out in a second folder that shares this repository, so you don't have to switch back and forth.

Loading the graph…
Shown: git worktree add ../hotfix-tree featureArticle

List your worktrees

Safe
git worktree list

Lists every folder checked out from this repository, with its commit and branch.

Loading the graph…
Shown: git worktree list

Forget worktrees you deleted by hand

Safe
git worktree prune

Clears Git's record of worktrees whose folders no longer exist, so their branches can be checked out again.

Loading the graph…
Shown: git worktree prune

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

Safe
git restore --staged <file>

Takes the file out of the next commit. Your edits stay in the file.

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

Discard changes to a file

Destructive
git restore <file>

Puts the file back the way it was at the last commit. git restore . does every file.

Loading the graph…
Shown: git restore app.pyArticle
  • UndoNo way back
  • WatchThe edits were never committed, so Git can't bring them back. Run git stash first if you're not sure.

Discard all your changes

Destructive
git restore .

Puts every file in the current folder back the way it was at the last commit. Staged changes stay staged.

Loading the graph…
Shown: git restore .
  • UndoNo way back
  • WatchEvery unstaged edit is gone for good. New untracked files stay, so use git clean for those.

Discard all your changes, older Git

Destructive
git checkout -- .

The command Git had before restore. The -- means what follows is a file or folder, not a branch.

Loading the graph…
Shown: git checkout -- .
  • UndoNo way back
  • WatchEvery unstaged edit is gone for good.

Restore a file from an older commit

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

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

Loading the graph…
Shown: git restore --source HEAD~1 app.py
  • Undogit restore <file>

Fix the last commit

Caution
git commit --amend

Replaces the last commit with a new one. Stage a forgotten file first, or change the message with -m.

Loading the graph…
Shown: git commit --amend -m "Update dependencies and README"Article
  • Undogit reset --soft HEAD@{1}
  • WatchDon't amend a commit you've already pushed.

Undo a commit, keep its changes staged

Caution
git reset --soft HEAD~1

Moves the branch back one commit. The commit's changes wait in the staging area, ready to commit again.

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

Undo a commit, keep its changes unstaged

Caution
git reset HEAD~1

Moves the branch back one commit and unstages its changes. The files keep every edit.

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

Throw away commits and changes

Destructive
git reset --hard <commit>

Moves the branch to <commit> and makes the staging area and every file match it.

Loading the graph…
Shown: git reset --hard HEAD~2Article
  • Undogit reset --hard ORIG_HEAD
  • WatchUncommitted edits are gone for good. The commits can come back until Git cleans them up.

Make your branch match the remote

Caution
git reset --hard origin/<branch>

Throws away your local commits and changes on the branch and moves it to where the remote's is. Run git fetch first.

Loading the graph…
Shown: git reset --hard origin/main
  • Undogit reset --hard ORIG_HEAD
  • WatchUncommitted edits are gone for good, and unpushed commits only stay in the reflog.

Undo a pushed commit

Safe
git revert <commit>

Adds a new commit that reverses <commit>. History stays intact, so it's safe on a shared branch.

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

Undo a pushed merge

Safe
git revert -m 1 <merge>

Reverses everything a merge brought in. -m 1 keeps the side you merged into.

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

Undo several commits as one

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

Reverses every commit after <from> up to <to> and stages the result without committing, so one commit undoes them all.

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

Find a lost commit

Safe
git reflog

Lists every commit HEAD has pointed to, including ones no branch reaches anymore. Copy the hash you need.

Loading the graph…
Shown: git reflogArticle

Undo a reset

Caution
git reset --hard HEAD@{1}

HEAD@{1} is wherever HEAD was one move ago, so this jumps back past the reset that lost your commits.

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

Restore a deleted branch

Safe
git branch <name> <hash>

Git prints the hash when it deletes a branch. Point a new branch at it and nothing is lost.

Loading the graph…
Shown: git branch feature 1117a34

Nothing matches that search.

More Git cheat sheets

All cheat sheets