brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: unstage a file with git restore --staged

Git
~/tips/tip-git-restore-staged

git restore --staged notes.txt

Takes a file out of the staging area while keeping your edits on disk.

You staged the wrong file. Git even tells you how to fix it, if you read the hint under git status.

git restore --staged notes.txt

This takes the file out of the staging area and puts it back to unstaged. It does not touch the file on disk, so your edits are still there. The docs say that --staged restores only the index, using HEAD as the source by default.

Compare that with the command without the flag. git restore notes.txt restores the working tree file from the index, which means it throws your edits away. One flag is the difference between "oops, wrong file" and "oops, where did my work go".

If you want both at once, git restore --staged --worktree notes.txt resets the file to how it is in HEAD, in the index and on disk.

The caveat: the old way of doing this, git reset HEAD notes.txt, still works, but restore says what it does. I would use that one and leave the reset for moving branches around.

Also, a new file you staged with git add becomes untracked again. It is not deleted.

Further reading: git restore documentation, On this site: git diff –staged shows what your commit will contain, On this site: stage only part of a file with git add -p

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link