Git Command Finder & Cheat Sheet

Find the git command for what you want to do, such as undoing a commit or renaming a branch, with plain-English explanations.

Runs in your browser. Nothing is uploaded.

  1. Start a new repository in this folder

    git init

    Creates a hidden .git folder so this folder is tracked by git. Add -b main to name the first branch main.

  2. Download a repository

    git clone https://github.com/user/repo.git

    Copies the repository with its full history into a new folder and sets it up as the remote called origin.

  3. Download only the latest version of a big repository

    git clone --depth 1 https://github.com/user/repo.git

    Fetches just the latest commit, which is much faster for large projects. Run git fetch --unshallow later if you need the history.

  4. See what has changed

    git status

    Lists changed, staged and untracked files and the current branch. Add -s for a short view.

  5. See the exact changes not yet staged

    git diff

    Shows line-by-line changes in files you have edited but not added. Use git diff --staged to see what is about to be committed.

  6. Stage files for the next commit

    git add path/to/file

    Adds the current state of the file to the next commit. git add . stages everything in the current folder; git add -p lets you pick parts of files.

  7. Save staged changes as a commit

    git commit -m "Describe the change"

    Records the staged changes with a message. Write the message as what the commit does, such as “Fix login timeout”.

  8. Commit all changes to tracked files in one step

    git commit -am "Describe the change"

    Stages every modified tracked file and commits. New, untracked files still need git add.

  9. Change the last commit message or add a forgotten file

    Can lose work
    git commit --amend

    Replaces the last commit with a new one including anything you have staged. Use --no-edit to keep the message. Only do this before pushing, or you will need to force push.

  10. Discard changes to a file

    Can lose work
    git restore path/to/file

    Puts the file back to how it was at the last commit. Your uncommitted changes to it are lost for good.

  11. Discard all uncommitted changes

    Can lose work
    git restore .

    Throws away every unstaged change in tracked files. New, untracked files stay; remove them with git clean -fd.

  12. Delete untracked files and folders

    Can lose work
    git clean -fd

    Deletes files git isn’t tracking, such as build output. Run git clean -nd first to see what would be removed.

  13. Unstage a file but keep the changes

    git restore --staged path/to/file

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

  14. Undo the last commit but keep the changes

    git reset --soft HEAD~1

    Moves the branch back one commit and leaves the changes staged, so you can fix and commit again. Safe if the commit wasn’t pushed.

  15. Delete the last commit and its changes

    Can lose work
    git reset --hard HEAD~1

    Moves the branch back one commit and throws away its changes and any uncommitted work. Use git reflog to recover if you regret it.

  16. Undo a commit that was already pushed

    git revert <commit>

    Creates a new commit that reverses the changes of an old one. History stays intact, so it is safe on shared branches.

  17. Recover a lost commit or branch

    git reflog

    Lists every position HEAD has been at, including commits removed by reset or rebase. Find the one you want and run git branch rescue <commit>.

  18. List branches

    git branch -a

    Shows local branches and, with -a, remote-tracking branches too. The current branch is marked with *.

  19. Create a branch and switch to it

    git switch -c feature/login

    Starts a new branch from where you are and switches to it. Older guides use git checkout -b.

  20. Switch to another branch

    git switch main

    Changes your working files to the other branch. Commit or stash your changes first if git refuses.

  21. Rename the current branch

    git branch -m new-name

    Renames the branch locally. If it was pushed, push the new name and delete the old one on the remote.

  22. Delete a local branch

    git branch -d feature/login

    Deletes a branch that has been merged. Use -D to force-delete an unmerged branch, losing its commits.

  23. Delete a branch on the remote

    Can lose work
    git push origin --delete feature/login

    Removes the branch from GitHub, GitLab or another remote. Your local copy is not affected.

  24. Merge another branch into the current one

    git merge feature/login

    Brings the other branch’s commits into yours. If both changed the same lines, git asks you to resolve conflicts.

  25. Cancel a merge with conflicts

    git merge --abort

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

  26. Update my branch with the latest main

    Can lose work
    git rebase main

    Replays your commits on top of main for a straight history. Don’t rebase commits others have already pulled; merge main instead.

  27. Combine (squash) or reword recent commits

    Can lose work
    git rebase -i HEAD~3

    Opens a list of the last 3 commits where you can squash, reorder, reword or drop them. Rewrites history: only for commits that aren’t shared.

  28. Copy one commit to the current branch

    git cherry-pick <commit>

    Applies the changes from a single commit on top of your branch as a new commit.

  29. See which remotes are set up

    git remote -v

    Lists remote names and their URLs. origin is the usual name for the one you cloned from.

  30. Connect a local repository to GitHub

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

    Adds a remote called origin. Then push with git push -u origin main.

  31. Change the remote URL

    git remote set-url origin git@github.com:user/repo.git

    Points origin at a new address, for example when switching from HTTPS to SSH or after renaming a repository.

  32. Upload commits to the remote

    git push

    Sends your commits to the remote branch. The first time for a new branch, use git push -u origin branch-name.

  33. Push after rewriting history

    Can lose work
    git push --force-with-lease

    Overwrites the remote branch, but refuses if someone else pushed in the meantime. Safer than --force; still avoid on shared branches.

  34. Download and apply the latest changes

    git pull

    Fetches from the remote and merges into your branch. git pull --rebase replays your local commits on top instead.

  35. Check for new commits without changing my files

    git fetch --prune

    Downloads new commits and branches but doesn’t touch your working files. --prune removes branches deleted on the remote.

  36. See the commit history

    git log --oneline --graph --all

    Shows one line per commit with a branch graph. Press q to quit.

  37. See the history of one file

    git log -p -- path/to/file

    Lists commits that changed the file, with the changes. Add --follow to track it across renames.

  38. Find who changed each line

    git blame path/to/file

    Shows the commit, author and date for every line of the file.

  39. See what a commit changed

    git show <commit>

    Shows the commit message and its full diff.

  40. Find the commit that added or removed some text

    git log -S "search text"

    Lists commits where the number of occurrences of the text changed. Add -p to see the changes.

  41. Find the commit that introduced a bug

    git bisect start

    Binary search through history: mark a bad commit with git bisect bad and a good one with git bisect good, then test each commit git checks out. Finish with git bisect reset.

  42. Put changes aside for later

    git stash push -m "work in progress"

    Saves uncommitted changes and cleans your working files, so you can switch branches. Add -u to include untracked files.

  43. Bring back stashed changes

    git stash pop

    Applies the latest stash and removes it from the list. git stash list shows all stashes; git stash apply keeps the stash.

  44. Tag a release

    git tag -a v1.2.0 -m "Version 1.2.0"

    Creates an annotated tag at the current commit. Tags aren’t pushed by default: run git push origin v1.2.0.

  45. Delete a tag

    Can lose work
    git tag -d v1.2.0 && git push origin --delete v1.2.0

    Removes the tag locally and on the remote.

  46. Stop tracking a file now in .gitignore

    git rm --cached path/to/file

    Removes the file from git but keeps it on your disk. Commit the change; .gitignore keeps it out from then on. Use -r for a folder.

  47. Move my last commits to a new branch

    Can lose work
    git branch new-branch && git reset --hard HEAD~2

    Saves the last 2 commits on a new branch, then removes them from the current one. Switch to new-branch to continue. Only before pushing.

  48. Set my name and email

    git config --global user.name "Your Name" && git config --global user.email "you@example.com"

    Sets the author shown on your commits on this computer. Leave out --global to set it for one repository.

  49. Use main as the default branch name

    git config --global init.defaultBranch main

    New repositories created with git init start on main instead of master.

  50. Create a short alias for a command

    git config --global alias.st status

    Lets you type git st instead of git status. Any command and options can be aliased.

  51. Ignore files in every repository

    git config --global core.excludesFile ~/.gitignore_global

    Uses a personal ignore file for files like .DS_Store or editor folders in all projects.

How to use the Git Command Finder & Cheat Sheet

  1. Type what you want to do in your own words, such as “undo last commit” or “delete remote branch”.
  2. The best matches move to the top. Use the section buttons to browse, for example all Undo commands.
  3. Read the explanation under each command. Commands marked Can lose work delete changes or rewrite history.
  4. Copy the command, replace placeholders such as path/to/file or <commit>, and run it in your terminal.

The git command for what you mean

Git has hundreds of commands and options, and the one you need is often named differently from what you are thinking. You want to “undo a commit”, but the answer might be reset, revert or restore depending on whether the commit was pushed and whether you want to keep the changes. This page starts from the task, in plain words, and gives you the command with a short explanation of what it does.

Search by task

Type a few words and the list reorders itself, with the closest task at the top. The search understands related words: “uncommit”, “throw away” or “squash” find the right entries even when the command is called something else. Section buttons group the list into starting a repository, saving changes, undoing, branches, remotes, history, stashing, tags, fixing mistakes and setup.

Know when to be careful

Some commands throw away work for good (git restore, git clean, git reset --hard) or rewrite history that others may have pulled (git rebase, git commit --amend, force pushes). These are marked Can lose work. A good rule of thumb: rewriting is fine for commits only you have; for anything already shared, prefer commands that add new commits, such as git revert.

Modern commands

The commands follow current Git: git switch for branches, git restore for files and git push --force-with-lease instead of a plain force push, which refuses to overwrite commits you haven’t seen. Every entry still works with older habits; the explanations mention the classic git checkout form where people often search for it.

Works offline

The whole reference is part of the page, so it keeps working without a connection and nothing you type is sent anywhere. Keep unwanted files out of your repository with the .gitignore Generator.

Frequently asked questions

Why do some commands use git switch and git restore instead of git checkout?

Since Git 2.23, git switch changes branches and git restore restores files. They split the many jobs of git checkout into two clearer commands. Older guides still use checkout, and it still works.

What is the difference between reset and revert?

git reset moves your branch back and removes commits from its history, which is fine for local work. git revert adds a new commit that undoes an old one, which is the safe way to undo something that has already been pushed and pulled by others.

What does HEAD~1 mean?

HEAD is the commit you are on. HEAD~1 is the one before it, HEAD~2 the one before that, and so on. You can also use any commit ID from git log --oneline.

I lost commits after a reset. Can I get them back?

Usually yes. git reflog lists every commit HEAD has pointed to recently, including ones no branch points to any more. Create a branch at the one you want with git branch rescue <commit>.

Does this page run git or connect to my repository?

No. It is a searchable reference in your browser. You run the commands yourself in a terminal.

Tutorials