GIT & VERSION CONTROL · REFERENCE
Fixing Mistakes
Choose a recovery operation based on whether your change is unstaged, staged, committed, or shared.
At a glance
| Term or command | What it means | When to use it |
|---|---|---|
| git restore --staged FILE | Removes the file’s staged change while preserving working-file content. | Use after staging the wrong file; needs an existing commit for this default form. |
| git restore FILE | Replaces a tracked working file with its staged version by default. | Use only when you intend to discard that file’s unstaged edits. |
| git revert COMMIT | Creates a new commit reversing an earlier commit’s changes. | Prefer for undoing a simple commit already shared with others. |
| git stash push -u | Temporarily stores tracked changes and untracked files. | Pause unfinished work; ignored files are not included by -u. |
| git merge --abort | Attempts to cancel an in-progress merge. | Use when you want to return from a conflicted merge to the pre-merge state. |
| Conflict markers | Text showing competing versions Git could not combine. | Edit deliberately, test the result, then stage and complete the operation. |
Commands are examples, not an interactive terminal. Run them in your own learning repository after checking the prerequisites. Output, paths, commit IDs, and wording can vary. Host-specific instructions are labeled.
Unstage without losing the edit
Prerequisite: this repository has at least one commit. You accidentally staged index.html.
Commands
git restore --staged index.html
git status --short
Example output
M index.html
The edit remains in the working tree and is no longer selected for the next commit. This is different from discarding the edit.
Discard an unstaged edit deliberately
The next command discards unstaged edits to this tracked file. Review git diff first and copy out anything you want to keep. Git generally cannot recover content it never recorded.
git diff -- index.html
git restore index.html
By default, restore copies from the staging area. If the file was staged earlier, it returns to that staged content, not necessarily the last commit. It does not delete a separate untracked file.
Undo a simple shared commit
Prerequisite: clean working tree, and HEAD is the ordinary non-merge commit you intend to reverse.
Commands
git revert --no-edit HEAD
git log --oneline -2
Example output
e5f6a7b Revert "Clarify homepage heading"
d4e5f6a Clarify homepage heading
Revert adds a new history entry instead of deleting the old entry. Inspect and test it before pushing. Reverting older changes can conflict; merge commits require additional understanding.
Pause unfinished work
git stash push -u -m "Pause heading edit"
git stash list
git stash apply
Use these inside the repository when you need a clean working tree temporarily. Apply restores the latest stash’s changes but retains the stash entry. Review status and resolve any conflicts. A stash lives locally; an ordinary push does not share it. -u includes untracked files but not ignored ones.
Resolve a merge conflict step by step
Assume main and a feature branch both edited the same heading. You merge and Git reports a conflict. Use status to identify the affected files, then open them in your editor. A simplified conflicted file might contain:
<<<<<<< HEAD
<h1>Trail Notes</h1>
=======
<h1>Walking Notes</h1>
>>>>>>> update-heading
The upper side is the current branch; the lower side is the incoming branch in this merge. Decide the final text, remove all marker lines, and save. You may keep one side or write a combined result. Then:
git status
git diff
git add index.html
git diff --staged
git commit -m "Merge heading updates"
Test before committing the resolved result. To cancel an in-progress merge, use git merge --abort instead. Start merges with a clean working tree: unrelated edits made before or during a merge can complicate abort recovery.
If a pull or push is rejected, inspect the message and branch history. Force-pushing or a hard reset is not a routine synchronization step.
Sources and further reading
Study questions
Explain it in your own words, then open the answer.
Does unstage erase my file changes?
No. Restore --staged preserves working-tree edits.
Can restore discard work Git never recorded?
Yes. Restoring a tracked file can overwrite unstaged edits, which Git may not be able to recover.
Why use revert for shared history?
It records a new reversing commit rather than rewriting the published sequence.
Is a stash uploaded by an ordinary push?
No. It is local temporary storage.
What should I do with conflict markers?
Read both versions, choose the intended final content, remove markers, save, test, stage, and complete the merge.