Overview
- Unstage changes with git reset
A plain reset moves staged changes back to the working directory:
git reset
The file edits remain. Only their staged state is removed.
To undo the latest commit while returning its changes to the working process, the demonstrated command is:
git reset HEAD^
This moves away from the latest commit and makes its changes available for staging or modification again.
- Discard working changes with git restore
Restore one file to its most recent committed state:
git restore 1.txt
Restore a directory:
git restore my-folder
Restore all working-directory changes in the repository:
git restore .
This is appropriate when an uncommitted approach has failed and the desired result is the exact last committed version rather than a manual reconstruction.
If a change has already been staged, remove it from staging while preserving the working-directory content with:
git restore --staged 1.txt
or:
git restore --staged .
- Reset the repository state forcefully
A normal reset changes staging state but does not necessarily recreate deleted files in the working directory. To restore both repository state and working files to the committed state in the demonstrated workflow, use:
git reset --hard
This can bring deleted files back and remove local modifications, but it also discards uncommitted work.
- Remove tracked content with git rm
Delete a tracked file and stage that deletion in one step:
git rm 4.txt
If the file contains local modifications, Git may refuse because deleting it would discard uncommitted changes. If deletion is intentional despite those changes, force it with:
git rm -f 4.txt
Remove the file from Git tracking while keeping the physical file in the working directory with:
git rm --cached 4.txt
Afterward, Git reports the retained file as untracked.
Remove a tracked directory and its contents recursively with:
git rm -r my-folder
The deletion is staged automatically.
- Reverse a committed change with git revert
To cancel the effect of a previous commit without deleting it from history, use:
git revert <commit-id>
Revert creates a new commit whose changes reverse the selected commit. The original commit remains visible, and the new revert commit records that a correction occurred.
This differs from reset. Reset can move the project back and remove later commits from the visible history. Revert preserves the existing history and adds a new corrective event.