Rails - Quick switch from a branch to another

When I work with several branches on the same project, I use a script to switch from a branch to another. The script stashes in a wip-commit all the changes of the current branch, pull new changes of the destination branch, install dependancies and run migrations.

In details, all the steps are:

  1. cd to your dev directory ($DEV_ROOT_PATH)
  • git fetch is -f option is provided
  • reset db/structure.sql file (when your DB schema is not the same on master and your branch, and you haven't made any change) if the only change is this file
  • display git status and wait for a confirm: y to continue, n to exit
  • git commit -nm 'wip' (commit any changes to a commit named wip)
  • git checkout to the new branch
  • git pull --rebase
  • reset last commit if its name is exactly wip
  • display git status
  • run bundle install and write the output into /tmp/bundle-install.log
  • run bundle exec rake db:migrate

Install

git clone https://github.com/rclavel/bash-scripts.git

# Replace the paths by the correct value
echo 'export DEV_ROOT_PATH=~/dev/my-dev-directory' >> ~.bashrc
echo 'export BASH_SCRIPTS_PATH=~/dev/bash-scripts' >> ~.bashrc
echo 'source "${BASH_SCRIPTS_PATH}/bash-scripts.sh"' >> ~.bashrc

Usage

Current situation
$ git status
On branch my_branch
Your branch is up-to-date with 'origin/my_branch'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   README.md
	modified:   db/structure.sql

no changes added to commit (use "git add" and/or "git commit -a")
Checkout to master
$ checkout master
+ git status
On branch my_branch
Your branch is up-to-date with 'origin/my_branch'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   README.md
	modified:   db/structure.sql

no changes added to commit (use "git add" and/or "git commit -a")
These files will be wiped. Continue? > (yN)y
+ git add .
+ git commit -nm 'wip'
[my_branch bb5246a] wip
 2 files changed, 713 insertions(+), 704 deletions(-)
+ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
+ git pull --rebase
remote: Counting objects: 107, done.
remote: Compressing objects: 100% (86/86), done.
...
+ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
+ bundle install > /tmp/bundle-install.log
+ bundle exec rake db:migrate
== 20170221133341 ThisIsAMigration: migrating ==========================
-- add_column(:users, :foobar, :string)
   -> 0.0032s
== 20170221132341 ThisIsAMigration: migrated (0.0060s) =================
Checkout to last branch before master
$ checkout -
+ git checkout db/structure.sql
+ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
+ git checkout -
Switched to branch 'my_branch'
Your branch is up-to-date with 'origin/my_branch'.
+ git pull --rebase
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (8/8), done.
...
+ git status
On branch my_branch
Your branch is up-to-date with 'origin/my_branch'.
nothing to commit, working directory clean
+ bundle install > /tmp/bundle-install.log
+ bundle exec rake db:migrate
comments powered by Disqus