Skip to content

Git: Undo

7 Git commands: Undo. Click a command to see what each part does.

Index
7 items
Click a command to see what each part does.

Undo7

  • git restore --staged <file>Unstage a file (keep the changes)

    What each part does

    1. restorerestore files
    2. --stagedonly the index (staging area): the change goes back to "not staged"
  • git restore <file>Discard local changes to a fileDestructive

    What each part does

    1. restore <file>replace the working copy with the last committed/staged version
  • git reset --soft HEAD~1Undo the last commit, keep the changes stagedCaution

    What each part does

    1. resetmove the current branch pointer
    2. --softonly move HEAD; index and files are untouched
    3. HEAD~1one commit before HEAD
  • git fetch origin && git reset --hard origin/<branch>Throw away local commits and changes to match the remoteDestructive

    What each part does

    1. git fetch origindownload the latest remote state
    2. &&only if that worked
    3. reset --hardmove the branch AND overwrite index + working tree
    4. origin/<branch>the remote version
  • git revert <commit>Undo a pushed commit safely

    What each part does

    1. revert <commit>create a new commit that undoes <commit>; history is not rewritten
  • git reflog -n 20Find a "lost" commit

    What each part does

    1. reflogevery place HEAD has been (commits, resets, rebases)
    2. -n 20last 20 entries; then git branch rescue <sha>
  • git stash push -u -m "<message>"; git stash popPark work in progress

    What each part does

    1. stash pushsave changes and clean the working tree
    2. -uinclude untracked files
    3. -m "<message>"label it
    4. stash popapply the latest stash and drop it

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…