Sometimes I accidentally
git add files. Or more often, I do
git add . and get a huge changelist and then realize I want to move certain files to a different changelist or a different branch. I could do a
git reset which, absent
--hard, pulls all the changes out of the index (aka dircache aka staging area) but leaves them in the filesystem (aka working tree). But wouldn’t it be nice to leave all the files in the index except the few I want to keep out?
Yes. Yes, it would. And believe it or not, git has been telling you how, every time you do a
git status. Like me, you’ve probably seen this line often enough for it to be completely invisible:
# (use "git reset HEAD <file>..." to unstage)
“Oh,” I hear the whole Internet saying, “that’s what that means.
“But why,” you continue, “instead of
documenting an obscure feature at runtime during a tangentially related operation, wouldn’t they just provide a simple
git unadd command?”
Fortunately, you can just run
git config --global alias.unadd "reset HEAD"
and now you can do
git unadd foo.txt bar.txt
and only those files will be pulled out of your index. (Unfortunately, the silly overcommunication is not removed this time.)
About the Author