justdan0227
justdan0227

Reputation: 1362

Ruby on Rails - Git Branch Workflow

I know its a simple concept but just not grasping it after researching several sites.

I have a Ruby On Rails project and using git to manage the source. I have a production ready snapshot and initialize git such that it has a master (with git init, git add -A and git commit -m).

Now I want to try out a new feature so I create a branch called 'test' with git checkout -b test

Now while in test I try out a new scaffold with rails g scaffold UserToken username:string

Scaffold created all of the ROR files and I do a rake db:migrate to update the DB. I then go into the rails console and test out adding records to the db and then start working on updating the other generated scaffold model file.

After lunch I come back and decide I want to scrap all of this.

Questions - (after trying this out myself) is the only way to get back to master is to git add -A, git commit -m and then git checkout master ? Do I really have to add and commit to get back to master? (this does work; however I don't think I'm grasping something basic in git that I would do a commit on something that I am going to scrap)

Next, if I do the above (again I don't think that's right) when I get back to master I do see that my scaffold files are gone as are the migration file (which created the table) and the schema.rb reflects that the table generated while in the branch is not there.

So far so good:

HOWEVER, if I go into the actual database the table IS still there. What basic fundamental am I missing in ROR/Git of testing something out in a branch and then abandoning it?

UPDATE #1

So Stash does not appear to help: Stash does not help.

Steps:  
rails new test_app  
git init  
git add -A  
git commit -m 'initial commit'  

git checkout -b newfeatures  
rails g scaffold UserToken username:string coin:integer  

files get generated... 
sqlite3 db/development.sqlite3 show that there is now a table called user_tokens:  

git stash save  
git checkout master  

Now in Master however all of the scaffold files still exist (and shouldn't)

Upvotes: 1

Views: 811

Answers (2)

James B. Byrne
James B. Byrne

Reputation: 1066

You have two questions here:

First: How do I move between branches without commiting work in progress?

To move between branches without having to commit your work you can use git stash.

git stash help
Usage: git stash list [<options>]
   or: git stash show [<stash>]
   or: git stash drop [-q|--quiet] [<stash>]
   or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
   or: git stash branch <branchname> [<stash>]
   or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] [<message>]]
   or: git stash clear

Once you stash your work then you can switch between branches without problem. Note that files that you wish to stash must first be added to a git branch via git add. Changes to files, including creation, that have not been added to git are are not tracked. So they are just like any other part of the general file system and will remain visible across branches.

Second: Why do my database migration changes in one branch show up in another?

Because the Database manager is not part of git. Whatever DBMS you are modifying those changes are persisted through the DBMS. If you want to keep your branch migrations separate then you need to have a separate database instance for each.

It may help if you imagine a git branch as a file-system template. When you switch to another branch then that branch's template overwrites your existing file-system with whatever is under its control. Everything else is ignored. When you commit you are updating the template for that branch. However, all of your work is actually done in the one true file-system.

What this means that things that you do to the file-system that are outside of git's control remain visible from all branches.

Upvotes: 4

smallbutton
smallbutton

Reputation: 3437

git reset --hard HEAD deletes everything and goes back to head in your testbranch so you can easily switch back with git checkout master

rake db:reset should do the trick with your data in the DB


UPDATE:

If you really want to get rid of anything:

git reset --hard && git clean -dfx rake db:drop rake db:create && rake db:migrate

Upvotes: 1

Related Questions