Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

  • Blog Navigation
git unadd

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 --soft or --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.)

  • Alex Chaffee

    See also GitFaq: [Why is “git rm” not the inverse of “git add”?](http://git.or.cz/gitwiki/GitFaq#Whyis.22gitrm.22nottheinverseof.22gitadd.22.3F)

  • Sebastian

    Any particular reason you didn’t go for “remove”? “unadd” seems almost as obscure as “reset HEAD”.

  • Alex Chaffee

    “remove” is an expansion of “rm” and means something else altogether in Gitistan — see that FAQ link in my earlier comment. I agree that git terminology is hazy at best but I’m not in a position to fix it, and after [browsing Google](http://www.google.com/search?q=git+unadd) it seems many others share my intuition that you’d call undoing an add an “[unadd](http://kerneltrap.org/mailarchive/git/2006/2/1/200270)”.

  • Nice little trick, now added to my aliases. Thanks for sharing.

  • Thanks, this is useful… Now I’m worried my custom git configs might end up as long ad my vim config!

  • I got this aliased to git unstage and more @ http://github.com/grosser/dotfiles/blob/master/gitconfig ;)

  • Cool stuff Alex. For those who may not know, HEAD is the default:


    Commit to make the current HEAD. If not given defaults to HEAD.”

    So, its really just typing unadd instead of reset. I can see how that will work nicely and all, but sometimes I find myself in a land without personal aliases and wishing I hadn’t used so many. ymmv.

  • Mark Wilden

    Even when I’m on a system without all my pretty aliases, it’s useful to know that git is case-insensitive by default, so you can type ‘head’ instead of ‘HEAD’.

  • Pieter

    added alias. thanks :)

  • yolanda

    Awesome I learned 3 things :

    – How to unadd something

    – How to alias stuff

    – To read the messages git tells me!

    Note that if this is the initial commit, there is no HEAD. So git reset HEAD doesnt work. Instead, you have to do : git rm –cached …

    But again… git told me that… and I couldnt see it….

Share This