“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey (contact@robl.me)
Senior Software Engineer, Brighton, UK

Recover committed (deleted) file in Git

What if, just suppose, you deleted a file in error, made the commit to Git, then several commits later you realise you needed the file all along. How do you get it back?

Well, you find the last commit for that file and then checkout the file with the revision number. In just a few lines then…

Robs-iMac:repo rl$ file='app/views/previously_deleted.html.haml'
Robs-iMac:repo rl$ echo $file
app/views/previously_deleted.html.haml
Robs-iMac:repo rl$ git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"