What are the steps I can do to re-start cleanly from the previous perfectly working Git checkout tree with all old files (new files deleted), correct migrations (new tables/migrations dropped), etc?
Here’s the situation. I’m learning Rails and a couple of times I messed up and hence, I did git checkout -f (without committing anything) to start cleanly from that perfectly working code. However, I found that newly created files remained and I had to delete them manually and rake db:migrate had errors saying the tables were already existing.
Here are some of my errors:
Sayanee:depot sweska$ rake db:migrate
(in /Applications/XAMPP/xamppfiles/htdocs/rails_projects/TUTORIALS/depot)
== CreateUsers: migrating ====================================================
-- create_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "hashed_password" varchar(255), "salt" varchar(255), "created_at" datetime, "updated_at" datetime)
(See full trace by running task with --trace)
Sayanee:depot sweska$ rake db:reset
(in /Applications/XAMPP/xamppfiles/htdocs/rails_projects/TUTORIALS/depot)
db/test.sqlite3 already exists
db/test.sqlite3 already exists
db/development.sqlite3 already exists
-- create_table("carts", {:force=>true})
-> 0.0033s
-- create_table("line_items", {:force=>true})
-> 0.0172s
-- create_table("orders", {:force=>true})
-> 0.0032s
-- create_table("products", {:force=>true})
-> 0.0030s
-- create_table("users", {:force=>true})
-> 0.0049s
-- initialize_schema_migrations_table()
-> 0.0095s
-- assume_migrated_upto_version(20110522103119, "db/migrate")
-> 0.0153s
You have 1 pending migrations:
20110523004950 CreateUsers
Run "rake db:migrate" to update your database then try again.
So my questions are:
- Which git command to use to make a clean shift back to the previous commit?
- How can I know what are the new files created so that I can delete them? Or should I commit the current not-working codes and then shift back?
- How can I make a clean shift to the previous migrations when I do not know how many steps to go back using the
rake db:rollback STEP=3? Or is there any command likegit statuswhich can list all my previous migrations and their names? - Am i missing anything else that I should consider when moving back to my previous working git commit and start fresh from there?
Many thanks! And with the answers, I will list the steps here!
Steps to create a clean start from the previous git checkout:
- git reset –hard HEAD^ #hard reset
- git clean -f #delete all new files
- rake db:migrate
You could always reset your commit to the previous.
Remember resetting hard resets the index and the working tree, so any tracked files will be discarded. If you don’t want to discard anything, you can do a –soft reset instead.
For more information, the git reset docs has some well written examples:
http://git-scm.com/docs/git-reset
Since your rails app creates the sqlite database file, resetting the current tree to a previous state will not remove untracked files. To remove those untracked files (your sqlite database), you can remove that sqlite database file. If you want to clean the entire tree, you can run:
By resetting and cleaning your git working tree, it will go back to the state where you initially checked out at that particular commit.
Hope that helps!