History Rewriting and Failure Recovery
This command group moves commit history or modifies existing commits. It suits cleaning local-branch history, but before changing commits that are pushed and used by others, confirm collaboration impact.
git reset HEAD~1
git reset --mixed HEAD~1Purpose: undo the most recent commit while retaining file modifications in the working tree.
Appropriate when:
- you just committed but the commit scope is wrong and you want to split/recommit;
- the message or file combination is poor, while the changes must remain.
Notes:
HEAD~1means one first-parent step backward.--mixedmoves the branch pointer and resets the index while retaining working-tree modifications. It is the default if omitted, but is explicit here for reviewability.
Common follow-up:
git status
git add -p
git commit -m "new message"git rebase -i HEAD~N
git rebase -i HEAD~5Purpose: interactively edit the most recent N commits—reorder, combine, reword, or remove them.
The official cheat sheet uses:
git rebase -i HEAD~6for a squash example. Choose N based on the commits you intend to edit. To process the newest five commits, HEAD~5 is common. To include an earlier baseline commit in the editor list, expand the range.
Common verbs:
| Verb | Meaning |
|---|---|
pick | Keep the commit |
reword | Keep content but edit the message |
edit | Stop at the commit and allow content changes |
squash | Merge into the preceding commit and allow message editing |
fixup | Merge into the preceding commit and discard current message |
drop | Delete the commit |
Commit IDs will be rewritten
Rebase generates new commit IDs. If the commits were pushed to a shared branch, confirm the team process before rewriting.
Conflict loop:
git status
git add -- resolved-file.md
git rebase --continueIf the base, order, or target was wrong, abort instead of layering more fixes:
git rebase --abort
git status --short --branchSquash several recent commits
After interactive rebase opens, normally leave the first record as pick and change later commits that belong with it to fixup or squash.
pick 1111111 first change
fixup 2222222 small fix
fixup 3333333 typo fixWhen finished, Git combines these commits into fewer commits.
git reflog BRANCHNAME
git reflog BRANCHNAMEPurpose: view commits a branch reference previously targeted. It is commonly used to recover a position before rebase, reset, or accidental branch deletion.
Appropriate when:
- a rebase failed or its result is wrong and you need an earlier commit;
- a branch was accidentally deleted and you need its last commit ID.
Examples:
git reflog main
git reflog- Without a branch name,
git reflognormally shows moves of currentHEAD. - Reflog is local, not remote-repository history.
- Reflog entries expire and may be pruned. Typical defaults expire reachable entries after about 90 days and unreachable entries after about 30 days, though configuration can change.
- Deleting a branch also deletes that branch’s own reflog. The
HEADreflog may still supply a clue, but cannot guarantee permanent recovery.
Create a recovery reference before deciding whether to move the current branch:
git reflog
git branch recovery/before-rewrite 'HEAD@{1}'
git show recovery/before-rewriteThis is easier to review than immediately hard-resetting and preserves the current-branch scene.
Three common reset modes
| Mode | Moves current branch | Resets index | Overwrites working tree | Typical boundary |
|---|---|---|---|---|
--soft | Yes | No | No | Reorganize recent commits that are not shared |
--mixed | Yes | Yes | No | Uncommit and stage again |
--hard | Yes | Yes | Yes | Disposable isolated repository only; loses uncommitted content |
reset behavior also depends on target commit and path form. Do not memorize only “undo”; first determine whether you need to move a reference, index, or working tree.
git reset --hard <commit>
git reset --hard 'HEAD@{1}'Purpose: force the current branch, staging area, and working tree to a specified commit.
Typical recovery flow:
git reflog
git branch recovery/before-hard-reset 'HEAD@{1}'
git reset --hard 'HEAD@{1}'High-risk operation
This discards current working-tree and staging-area modifications and may delete untracked paths blocking the target tree. Use only in a disposable isolated repository after creating a recovery reference and confirming current local changes are unneeded.
git commit --amend
git commit --amendPurpose: change the most recent commit. It can correct its message or add newly staged files to it.
Appropriate when:
- a message is wrong immediately after committing;
- one file was missed and the commit is not pushed or can be safely rewritten.
Add a missed file:
git add forgotten-file.md
git commit --amendTo change only the message:
git commit --amend -m "new message"Amend replaces the most recent commit, changing its commit ID.
References
Checked against Git 2.55.0 official documentation, obtained 2026-07-14.