Git Destructive Operations and Recovering Lost Work
Posted on September 1, 2020 in Git, GitHub by Matt Jennings

Local Destructive Operations

  • git checkout -- <file>
    If the file is present in the staging area, it’ll be overwritten.
  • git reset --hard
    Will overwrite changes that re staged and in the working area.
  • Unless changes are stashed, there’s no way of getting them back!
  • Tip: use git stash --include-untracked to include working area changes in your stash.

Remote Destructive Operations

  • There are many operations that can rewrite history:
    • rebase
    • amend
    • reset
  • If you code is hosted or shared, never run:
    git push -f

ORIG_HEAD to Undo a Merge

  • Use ORIG_HEAD to undo mergers
  • git reset --merge ORIG_HEAD
  • Use --merge flag to preserver any uncommited changes

Leave a Reply

To Top ↑