Undoing Things in Rails


Collapse Content

If you don't succeed at first, just press ctrl-Z

You'll often make mistakes when coding so it's important to know how to undo things quickly. Of course you can press ctrl-Z to undo your last code edit, but what happens when you want to undo something more significant?

rails generate

To undo a rails generate command, run a rails destroy command.

For example, let's say you ran:

rails generate model Post title:string content:text

You can undo it with:

rails destroy model Post title:string content:text

rake db:migrate

Let's say you ran a migration to apply changes to the database, but then realized you wanted to edit the migration file first. To undo the last migration, simply run:

rake db:rollback

You can then edit the file and run rake db:migrate again.

(See how to roll back a Migration file to rollback a specific migration or multiple migrations.)

git

Let's say you messed up your code (and saved it) and want to throw it all away and go back to a previous commit.

If you didn't yet commit your code to git, you can go back to your previous commit with:

git checkout .

Let's say you committed your changes with git, but you didn't share this code with anyone. You can undo this last commit so there's no trace of it with:

git reset --hard HEAD^

Learneroo

You can press ctrl-Z in the Learneroo code editor, but what happens if you accidentally close a tab with code in it? You can restore previous code by clicking on the lifesaver above the editor. You can throw away all your code by clicking on "Reset". And you can always view your previous submissions for every coding challenge.

Wrong Major

Let's say you majored in art history and now realize you want to become a programmer. Don't worry! You can start learning programming on this site and attend a programming bootcamp for further learning. Go through our full web development course to get started!

Contact Us
Sign in or email us at [email protected]