Choose an undo operation by identifying its target. Restoring an uncommitted file, reorganizing local commits and counteracting a shared commit have different effects on files and history. Establish that distinction before choosing restore, reset or revert.
The exercise takes place in a repository that can be disposed of. In particular, if you discard uncommitted changes with restore or run reset --hard, Git may not have data to restore. Check status, diff, and diff --staged before execution.
Git · GitOps All Series · 07 / 24
On this page
To cancel just add, restore –staged
git status --short
git diff --staged -- README.md
git restore --staged -- README.md
git diff -- README.md
Run on a repository that already has at least one commit. --staged basically restores HEAD contents to the index. Edits in the working tree remain so you can re-stage the file.
Conversely, the command below overwrites the working file with the contents of the index. Use only when it is a crystal that can be discarded. The difference that --staged is missing is important.
# Discards unstaged edits in this tracked file.
git diff -- README.md
git restore -- README.md
If there is other content in the index, the content of that index is returned, not HEAD. To explicitly base it on HEAD, specify --source=HEAD. It’s easier to memorize options if you think of them separately as source and destination.
reset to split my local commits again
You can use reset --soft HEAD~1 when rewriting the last commit that you haven’t shared yet. Moves the current branch location to its parent, but maintains the index and working tree. This assumes you have more than one commit and that no one else is working on those commits.
git status --short
git branch safety/before-reset
git reset --soft HEAD~1
git diff --staged
git commit -m "Rewrite the local change description"
A safe branch is a name that refers to an existing commit. It doesn’t keep untracked files or uncommitted changes. Commit required uncommitted work first or back it up separately.
| mode | index | work tree |
|---|---|---|
--soft | maintain | maintain |
--mixed·Default | Change to target commit | maintain |
--hard | Change to target commit | Change to target commit |
This table covers resetting to a commit without specifying a path. --hard discards uncommitted changes to tracked files and may also remove untracked files or directories that obstruct writing tracked files. Do not use it as a routine cleanup command.
Shared changes are offset with revert
If you want to revert a regular commit that has already been pushed by merging it into main, first look at revert, which adds a new commit that offsets that change. Since existing records are not erased, others can follow along with normal pushes and fetches.
git status --porcelain
git log --oneline -5
# Replace BAD_COMMIT with the non-merge commit to undo.
git revert BAD_COMMIT
# Run the project tests, then push the new revert commit.
revert may also conflict if other modifications overlap. After solving, use git add and git revert --continue or cancel with git revert --abort. Because the revert of a merge commit involves -m, which determines the main parent, and has implications for subsequent remerges, the general commit example is not applied as is.
reflog when branch location is lost
git reflog --date=iso -15
# After inspecting the desired commit ID:
git branch rescue/work RECOVER_COMMIT
Replace RECOVER_COMMIT with the actual confirmed commit hash. The reflog is a record of local reference movements and is not a remote server-wide audit log. Some records may not be found due to expiration or object cleanup, and recovery of edits that were never committed or saved is not guaranteed.
For the exact behavior, consult git restore, git reset, git revert and git reflog. After undoing a change, check the test results as well as the resulting files.
Previous · [Git and GitOps 06] Read History with HEAD, Graphs, diff and bisect · Next · [Git and GitOps 08] Create and Merge Branches: switch and Fast-Forward · Series list and example files



