Git Unstage Before First Commit


Here's how to unstage files before the first commit.

You've created a new repo. It's clean and fresh, then you go for the first commit. You add your current directory, then look at your status... A deflated sigh. So soon, too soon, you have sullied your pristine repo by preparing to commit a bunch of generated files. Of course, you don't want these in your repo. You only want source code. Well, you try the usually trick to start the file staging over:

git restore --staged .

It doesn't work.

You ask for help and get this alternate:

git rm --cached -r .

It works! Yay!

Then you learn of this little number, which looks familiar from interactive staging. We have an interactive unstage, or revert. We start the interactive session:

git add -i

We select 3: Revert. This lists all the files. There are a lot of files that we want to unstage. Each file has a file number before it. We enter the file number range at the Revert> prompt:

Revert> 2-253

Then we press enter, and get the reassuring:

reverted 251 paths

Ahh, now to promptly add those paths to .gitignore so we never have to worry about them again.

Cleanliness restored.