How to undo ‘git add’ before commit?

To undo a git add before committing, you can unstage the changes using git reset or git restore. Here’s how:

1. Unstage All Files

git reset           # Unstage all changes (keeps files modified)
# OR
git restore --staged .  # Newer Git versions (same effect)

2. Unstage a Specific File

git reset <file>    # Unstage a specific file (keeps its changes)
# OR
git restore --staged <file>

Example:

git reset app.js    # Unstages app.js

3. Check Staged Files

Use git status to see which files are staged (prefixed with Changes to be committed).

Key Notes

  • No Data Loss: These commands only unstage files; your changes remain in the working directory.
  • Undo Multiple Files: Use git reset multiple times or specify multiple files.

Example Workflow

# Accidentally staged all files
git add .

# Unstage all
git reset

# Check status (files are now unstaged)
git status

# Stage only the correct file
git add index.html

# Commit
git commit -m "Add index.html"

By using git reset or git restore, you can easily undo git add and refine your staged changes!

Leave a Reply

Your email address will not be published. Required fields are marked *