Git Topics (9)Tap to view
    Git·Version Control·Topic 6 of 9

    Git Undoing Changes & Recovery

    Undo unsaved modifications, reset branches (soft, mixed, hard), safely undo commits, and restore lost ones via reflog.

    1. Undoing Changes

    1. git restore <file>

      What it doesCancels unstaged modifications in the file and returns it to the last commit

      When to use itto undo an unsuccessful attempt on a specific file

      Examplegit restore src/app.js

    2. git restore --staged <file>

      What it doesRemoves the file from the Staging Area while keeping modifications to the file locally

      When to use itWhen you added a file with git add by mistake and want to unprepare it

      Examplegit restore --staged src/secret.env

    3. git reset --soft HEAD~1

      What it doesGoes one commit back while keeping the changes stored in Staging

      When to use itTo edit a message or add a file to commit that was just saved locally

      Examplegit reset --soft HEAD~1

    4. git reset --mixed HEAD~1

      What it doesOne commit goes back and returns files to the unequipped Working Tree (default)

      When to use itto dismantle commit and reconfigure it another way

      Examplegit reset --mixed HEAD~1

    5. git reset --hard HEAD~1

      What it doesMoves the branch back and clears all changes from Index and Working Tree (dangerous)

      When to use itto completely delete the last commit and its changes forever

      Examplegit reset --hard HEAD~1

    6. git revert <commit>

      What it doesCreates a new commit that safely reverses and undoes the effect of a previous commit

      When to use itto undo an existing change on a shared public branch without rewriting history

      Examplegit revert a1b2c3d

    7. git commit --amend -m "new msg"

      What it doesReplaces the last local commit with a new message or merges newly prepared files

      When to use itTo correct a typo in the last commit message or add a file you forgot

      Examplegit commit --amend -m "docs: clarify recovery steps"

    8. git reflog -10

      What it doesDisplays the history of the HEAD cursor movement for recovering deleted commits or branches

      When to use itA safety net for recovering a lost code after a wrong reset or failed rebase

      Examplegit reflog -10

    Offline · Reading cached copy