git - update to new branch of rails - ruby-on-rails

I currently have a rails project running of a git tag v2.1.2, to get here id did
git checkout v2.1.2
However there are now new fix's that have been applied to the 2.1 branch, how do I move to this branch rather than the tag?

If I understand your question correctly, you have a tag and a branch named the same. Then, to checkout to the branch, you would provide the path to it.
For example:
git checkout refs/heads/2.1
This disambiguates 2.1 branch from a tag named 2.1 itself.

You can create a new branch that tracks the remote 2-1-stable like so:
git checkout -b 2-1-stable origin/2-1-stable
Then just cd back to rails root and commit the submodule change.
Later if you need to update it, you should just be able to cd back into vendor/rails and:
git remote update
git rebase origin/2-1-stable
And commit the changes again.

Related

Use Unused Repo for New Project

I would like to save my app in one of my unused repository in GIT. Instead of creating new repo and deleting old unused repo, i want to get knowledge about Git by use the unused repo for new project.
Ex:
old Proj repo name: app_old
new Proj repo name: app_new
Status of app_old:
Am deleted all files in app_old through command. Then check out the app, the downloading status gives 80 mb done. But, there is no files in downloaded folder.
Status of app_new:
Created a new iOS project without checking the “Create Git Repository” option. Then choose the option “Create Working Copy” in Source Control. Then add the remote of app_old in both Xcode Preferences and Configure in Source Control.
Please adjust my English!!!
There is not too much sense to reuse existing repository as you're going to delete it anyway.
However, in case of some constraint like reusing existing remote repo, there is a way to remove full history of git commits and start from scratch:
# create new branch named newProject
git checkout --orphan newProject
# do copy your new files here, set up project, set up .gitignore and things like that
...
# add all files to the project
git add -A
# commit the new branch
git commit
# delete the old master branch
git branch -D master
# rename current branch (newProject) to master
git branch -m master
# push back to origin
git push -f --set-upstream origin master
This way you'll get rid of old history and start from scratch with your new project.
As written in comments, you'll need to delete remote branches too if you got any.
OK, let's start from scratch. Create a new, empty directory and do:
git init .
git remote add origin <url of the existing repository>
git push origin :master
echo "Some text" > somefile.txt
git add .
git commit -m "Initial commit"
git push origin master -u
Note the colon in the third step. Also note that if you have any other directories pointing to the old repo, you will want to delete them and re-clone from the repo.

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)
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.
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

GIT - How to create a new branch to refactor an entire project

I have an ios project that i would like to restart from scratch. I want to do that into a new branch from the git repo. I don't really know how to proceed.
I've created the new branch and a new project in xcode. But now i would like to version the new project in my new branch.
Thanks!
If you use Xcode for that, simply go to Source Control/Check Out... choose your new branch and then Source Control/Commit... your changes and Source Control/Push... and you should be good to go!
Clone your repo. Then reset your HEAD to the commit you want to start from now create new branch and push it to remote..
e.g
git clone project
git reset --hard CommitID
git checkout -b newbranch
git push origin HEAD:refs/heads/newbranch

Can't push changes to GitHub

Update: I couldn't get either of the first two solutions provided to work, so I'm providing more detail and setting a bounty.
I have previously forked a github project called Enki (a Rails blogging platform) and then customized it and deployed it on Heroku.
Now I want to start a new Enki blog. I can't fork Enki again, because it's already forked and customized for the first blog. I now cloned it from the author's page, and wanted to add a new repository on my github page so that I could deploy it to Heroku. Here's step by step instructions that I followed
git clone https://github.com/xaviershay/enki.git valentines
cd valentines
git checkout -b myvalentines
bundle install
cp config/database.example.yml config/database.yml
git init
Message
Reinitialized existing Git repository in /Users/mm/Sites/valentines/.git/
1) Why the existing repository? Did I do something wrong? So it's going into the same repository (as the original fork?) even though I've named it something else?
Made code changes then did
git add .
git commit -m "made code changes"
2 files changed, 193 insertions(+), 157 deletions(-)
rewrite Gemfile.lock (70%)
2) Does the fact that it's writing Gemfile.lock mean that it's not changing the branch but rather the master?
Next I created a repository on GitHub
git remote add valentines git#github.com:Username/Valentines.git
git push valentines master
As you can see I, following Ksol's suggestion, used a different word than origin but it didn't work 3) Problem - the GitHub repository 'Valentines' did not show the code changes that I made, but was rather the original gem
Just use another name for your remote than origin?
Adding to #Katen's answer:
You can also use branches for this. For example, your forked repo could contain these branches:
master (pristine code from the author's repo. You may periodically update this)
blog1 (your first blog)
blog2 (your second blog)
This way, to begin working on new installation and customization of this blog engine, just run these commands.
// assuming that you completed and committed your work on a current branch
git checkout master // switch to original code
git checkout -b blog3 // "fork" it to a new branch and switch to that branch
1) git clone will point to the original repository, regardless of the local directory you place it into to. You created a new branch w/ git checkout -b, but it's not a new git repo
2) This looks normal, rewrite is just telling you that you made a lot of changes to that file.
3) I believe this is because you didn't push the new branch to your github page
git push valentines myvalentines
you can also update your .git/config file so origin is your new location. Similarly, there's no reason your master branch need be a copy of the enki master branch, but you should keep a reference to it so you can pull future updates.

how do I upgrade from Rails 2 to Rails 3 without freezing code?

I am upgrading from Rails 2 to Rails 3. What I did was did a clone of the original app and began the upgrade process.
Unfortunately, I needed to continue to use and make refinements to Rails 2, so there have been changes in the code.
I am not done with the Rails 3 upgrade from the original code: do I need to freeze my current Rails 2 and then start-over, or is there a way I can get my original up to Rails 3 and then take only the changes made in the original and push them into the new upgrade?
I would choose git for this kind of work, it is a beautiful tool for that.
First you can init your source tree as a git repository, if you did not have it in git repo. If you already have it in git you can skip these steps and jump to branch creating.
git init .
Add the source files with git add and commit it w/ git commit.
Now you have a working Rails 2 app in git, create your upgrade branch for your Rails 3 modifications:
git checkout -b rails-3
Here you can modify your code to work with Rails 3. If you ever need to modify the Rails 2 part, simply checkout to the master branch:
git checkout master
Do the work, commit the modifications and then go back to Rails 3 branch and rebase:
git checkout rails-3 && git rebase master
After you're done and have a working Rails 3 app, go back and merge the changes:
git checkout master && git merge rails-3

Resources