
Git reset and revert commands: Choose between restore, reset and revert with a clear explanation of what each changes. Destructive reset examples are separated from safer shared-history practices. This guide explains what each command does, its syntax, practical examples, useful options, and common mistakes. Commands are shown for typical installations; flags can vary across operating-system and tool versions.
Before you start
Open a terminal appropriate for the operating system shown in the examples. Check that the required tool is installed and that you are running it with the permissions needed for your task. Replace sample names, paths, hostnames and IDs with values from your own environment.
Syntax at a glance
Square brackets usually indicate optional arguments, while uppercase words such as FILE, HOST and PACKAGE are placeholders that you replace. Keep quotes around paths or text that contains spaces. Commands should be tested on a safe sample before being used in production.
Quick command reference
| Command | What it does | Example | Useful options |
|---|---|---|---|
git restore | Discard or unstage selected working-tree changes | git restore --staged app.js | --staged unstages; omitting it restores working-tree content |
git reset --soft | Move HEAD while keeping changes staged | git reset --soft HEAD~1 | --mixed is the default reset mode; --hard also overwrites working tree |
git reset --hard | Reset index and working tree to a commit | git reset --hard HEAD~1 | No normal flag makes this safe for uncommitted work |
git revert | Create a new commit that reverses another commit | git revert HEAD | --no-edit accepts the generated message |
git reflog | Inspect recent local HEAD movements | git reflog --date=local -n 10 | -n limits displayed entries |
Command syntax and examples explained
1. git restore
Purpose: Discard or unstage selected working-tree changes.
Syntax
git restore PATHExample
git restore --staged app.jsOptions and notes: --staged unstages; omitting it restores working-tree content.
Restoring can discard uncommitted edits; make a backup if unsure.
2. git reset --soft
Purpose: Move HEAD while keeping changes staged.
Syntax
git reset --soft COMMITExample
git reset --soft HEAD~1Options and notes: --mixed is the default reset mode; --hard also overwrites working tree.
Soft reset is useful for combining local commits before sharing.
3. git reset --hard
Purpose: Reset index and working tree to a commit.
Syntax
git reset --hard COMMITExample
git reset --hard HEAD~1Options and notes: No normal flag makes this safe for uncommitted work.
High impact: uncommitted changes can be lost. Check status and back up first.
4. git revert
Purpose: Create a new commit that reverses another commit.
Syntax
git revert COMMITExample
git revert HEADOptions and notes: --no-edit accepts the generated message.
Revert is usually safer for already-shared history than reset.
5. git reflog
Purpose: Inspect recent local HEAD movements.
Syntax
git reflogExample
git reflog --date=local -n 10Options and notes: -n limits displayed entries.
Reflog can help find commits after a reset, but entries can eventually expire.
Common mistakes and troubleshooting
- Command not found: confirm the tool is installed and available on your PATH; open a new terminal after changing PATH.
- Permission denied: verify the current account, file permissions and target path. Use administrator/root privileges only when genuinely required.
- Unexpected output: check the command version, quoting, current directory and exact arguments. Consult the local help or manual for options supported by your build.
- Destructive operations: preview the target and keep a backup before deleting data, rewriting history, flashing a partition or changing system configuration.
Safety tips
Review every command before running it, especially commands that delete files, change permissions, flash firmware, alter a database or expose a network service. For network scanning and remote administration, work only on devices and systems you own or are authorized to test. Keep credentials, private keys, access tokens and log files private.
Frequently asked questions
Do these commands work on every system?
Not always. Some commands are specific to Windows, Linux, Android, a particular package manager or an installed tool version. Use the syntax shown for your platform and check local help when a flag is unavailable.
Can I copy and run every example exactly?
Use the examples as templates. Replace placeholders such as filenames, package IDs, hostnames and paths with your own values, and verify the effect before executing a command that changes data or system state.
Conclusion
Use this reference as a starting point, then confirm flags in the tool’s official help or manual for your installed version. A clear understanding of syntax, options and side effects makes command-line work more reliable.