GIT · Foundations · 5 min

Keep the scratch edit out

Remove a file from the next commit without losing its edits.

Not completed

Your task

Both files are staged. Unstage scratch.txt, keep its edited text, and leave app.txt staged. Do not commit.

What you’re aiming for

  • Only app.txt remains staged.
  • scratch.txt still contains the local reminder.
  • The existing commit stays unchanged.

1. Prepare your practice folder

Use Git 2.28 or newer. The setup commands below use a Bash-compatible shell: Git Bash on Windows, or Bash on Linux/macOS.

Create a new, empty folder for this exercise. If that name already exists, choose another new name. Continue only after you’re inside the new folder.

NEW PRACTICE FOLDER
mkdir opsrung-git-unstage-file && cd opsrung-git-unstage-file

Run these setup commands in that folder:

SETUP — RUN ONCE
git init --initial-branch=main
git config --local user.name 'Practice Learner'
git config --local user.email 'practice@example.invalid'
printf 'Ready\n' > app.txt
printf 'Scratch\n' > scratch.txt
git add .
git commit -m 'Initial app and scratch files'
printf 'Ready for release\n' > app.txt
printf 'Local-only reminder.\n' > scratch.txt
git add .
Only if your personal Git settings interfere

You can normally skip this. If a commit asks for a signing key, runs a custom hook, or changes line endings unexpectedly, these repository-local overrides keep this disposable exercise independent of those settings.

OPTIONAL REPOSITORY OVERRIDES
git config --local commit.gpgsign false
git config --local core.autocrlf false
git config --local core.hooksPath .git/practice-no-hooks

Retry the failed command afterward. These settings apply only inside this practice repository.

2. Try the task

Work toward the goal above. Start by inspecting the files or resources, then use a hint if you need one.

Hint 1

You need to change the staging area, not throw away the working file.

Hint 2

Check what the --staged option on git restore does.

Hint 3

git restore --staged -- scratch.txt keeps the working-tree edit.

3. Review your result

Use these commands to look at your result and compare it with the goals. The website does not run or verify them.

REVIEW IN YOUR TERMINAL
git diff --staged --name-only
git diff -- scratch.txt
git log --oneline

What you should see

  • git diff --staged --name-only prints only app.txt.
  • git diff -- scratch.txt still shows the added line: Local-only reminder.
  • The original commit remains the latest commit.

If your result differs: Use git restore --staged for the staging area. Restoring scratch.txt without --staged would discard the working-file edit you need to keep.

Compare with one possible solution
ONE POSSIBLE SOLUTION
git restore --staged -- scratch.txt

Other approaches are fine if you reach the same goal. Opening this solution does not mark the exercise complete.

Finished this exercise?

You decide when you’re done. Your choice is saved in this browser, and you can change it later.