ShareYourGroupShareYourGroupGit Help

Git command cheat sheet from tested examples

Each command here appears in a situation page where its output is shown.

CommandWhat it was used for
git reset --soft HEAD~1Undo the last commit but keep my changes
git reset --hard HEAD~1Undo the last commit and throw the changes away
git commit --amend -m "Add the second line of notes"Change the last commit message
git add readme.txtAdd a forgotten file to the last commit
git commit --amend --no-editAdd a forgotten file to the last commit
git restore --staged secret.txtUnstage a file I added by mistake
git restore notes.txtThrow away my changes to one file
git revert --no-edit HEADUndo a commit that is already pushed
git reset --hard HEAD@{1}Get back a commit after reset --hard
git branch feature HEAD@{1}Recover a branch I deleted
git stashSave unfinished work and switch branch
git stash listSave unfinished work and switch branch
git stash popSave unfinished work and switch branch
git branch featureMove a commit I made on the wrong branch
git reset --soft HEAD~3Combine the last three commits into one
git commit -m "Add numbered lines"Combine the last three commits into one
git cherry-pick feature~1Copy one commit to another branch
git rebase mainPut my branch on top of the latest main
git merge featureFix a merge conflict
git add greet.txtFix a merge conflict
git commit --no-editFix a merge conflict
git reset --hard ORIG_HEADUndo a merge I just made
git rm --cached config.localStop tracking a file but keep it on disk
git add .gitignoreStop tracking a file but keep it on disk
git commit -m "Stop tracking local config"Stop tracking a file but keep it on disk
git blame app.txtFind who changed a line and why
git log -S"rate = 7" --onelineFind who changed a line and why
git diffSee what I changed before I commit
git diff --stagedSee what I changed before I commit
git branch -m featureRename a branch

Before you run anything destructive

  1. Run git status and read it.
  2. Save work you might want: git branch backup costs nothing.
  3. Remember that git reflog can recover many mistakes for a while. The defaults, from the Git documentation, are 90 days for entries that can still be reached and 30 days for those that cannot.

Sources

  • Git documentation: git-reset (git-scm.com/docs/git-reset)
  • Git documentation: gitrevisions (git-scm.com/docs/gitrevisions)
  • Git documentation: git-config, settings gc.reflogExpire and gc.reflogExpireUnreachable (git-scm.com/docs/git-config)
  • Git documentation: git-revert (git-scm.com/docs/git-revert)

Read on 14 November 2026.

This is an independent guide. It is not connected with the Git project, GitHub, GitLab or any company named here. Every command below was run on 14 November 2026 with the real program (git version 2.56.0.windows.4) in a new empty practice repository; the output shown is what it printed. Facts about options were read in the official Git documentation on 14 November 2026.