Git: Undo
7 Git commands: Undo. Click a command to see what each part does.
Git: UndoIndex
Undo7
git restore --staged <file>Unstage a file (keep the changes)What each part does
restorerestore files--stagedonly the index (staging area): the change goes back to "not staged"
git restore <file>Discard local changes to a fileDestructiveWhat each part does
restore <file>replace the working copy with the last committed/staged version
git reset --soft HEAD~1Undo the last commit, keep the changes stagedCautionWhat each part does
resetmove the current branch pointer--softonly move HEAD; index and files are untouchedHEAD~1one commit before HEAD
git fetch origin && git reset --hard origin/<branch>Throw away local commits and changes to match the remoteDestructiveWhat each part does
git fetch origindownload the latest remote state&&only if that workedreset --hardmove the branch AND overwrite index + working treeorigin/<branch>the remote version
git revert <commit>Undo a pushed commit safelyWhat each part does
revert <commit>create a new commit that undoes <commit>; history is not rewritten
git reflog -n 20Find a "lost" commitWhat each part does
reflogevery place HEAD has been (commits, resets, rebases)-n 20last 20 entries; then git branch rescue <sha>
git stash push -u -m "<message>"; git stash popPark work in progressWhat each part does
stash pushsave changes and clean the working tree-uinclude untracked files-m "<message>"label itstash popapply the latest stash and drop it
Nothing matches.
My DevOps Playbook by Angelo John Calleja