23

Consider I've staged for committing a file (e.g. db/schema.rb) that I didn't intended to change. I need to do:

git reset db/schema.rb
git checkout db/schema.rb

Can I do it by single command?

Alexey
  • 9,197
  • 5
  • 64
  • 76

2 Answers2

30

I tried this one and works well for me:

git checkout HEAD -- path
Ethan Zhang
  • 4,189
  • 3
  • 22
  • 17
  • 2
    Wow, that really works. I always used shorter `git checkout path`, but it doesn't checkout files that are already staged. – KL-7 Apr 28 '12 at 11:31
  • why `git checkout HEAD -- path` works well, whereas `git checkout -- path` doesn't work ? – Olivier Boissé May 11 '18 at 15:43
1

I just added this to my .zshrc / .bashrc

checkout() {
  git reset "*$1*"
  git checkout "*$1*"
} 

And then you can just do checkout <file> and you're all set.

habitats
  • 2,203
  • 2
  • 23
  • 31