変更の取り消し
ミスから復旧する - reset、revert、コミットを安全に取り消します。
git undo commitUndo any commit - an older one, several at once, or one that is already pushed.git resetMove HEAD and choose what happens to the index and working tree: --soft, --mixed, --hard.git revertUndo a commit safely by adding a new commit that reverses it - the right choice for pushed history.git undo last commitUndo just the most recent commit - keep the work, discard it, or reverse it if it was pushed.git reset --hardForce your branch and working tree back to a commit, discarding all changes since it.git stashShelve your uncommitted changes so you can switch context, then reapply them later.git reflogSee everywhere HEAD has been - and recover commits you thought were lost.
How to undo things in Git
Almost nothing in Git is truly lost, but the right undo depends on where the mistake lives. Uncommitted changes can be stashed or discarded; a local commit can be reset away as if it never happened; a pushed commit should be reverted - a new commit that reverses it - so shared history is never rewritten.
The pages in this category walk through each case: git reset and its --soft/--mixed/--hard modes, git revert for published history, git stash for work you want back later, and git reflog - the safety net that can recover commits even after a hard reset.
よくある質問
How do I undo my last commit but keep the changes?
git reset --soft HEAD~1 removes the commit and leaves everything staged, ready to re-commit. Use git reset HEAD~1 (mixed) to also unstage, or git reset --hard HEAD~1 to discard the changes entirely.What is the difference between git reset and git revert?
git reset moves your branch pointer back, erasing commits from the branch - right for local, unpushed mistakes. git revert adds a new commit that undoes an earlier one, keeping history intact - the safe choice for anything already pushed.Can I recover commits after git reset --hard?
Usually yes.
git reflog lists every commit HEAD has pointed at, including the ones you just reset away. Find the lost commit hash in the list and restore it with git reset --hard <hash> or git cherry-pick <hash>.