Creating a New Git Branch on the Local Machine and Pushing it to a Remote

09 / Sep / 2010 by Vivek Krishna 1 comments

Our project went into production a few weeks back and was on Grails 1.3.1. Since the code was stable, we decided that it was time to upgrade to grails 1.3.4. However, we didn’t want to push it straight to the QA Server or Production server because that would’ve meant that the maintenance of the existing production code would become a pain.

To experiment with grails 1.3.4 and making sure that things were working smoothly, I’d created a new branch grails134, which was a copy of the master branch, which was deployed on production. After doing a grails upgrade and making some changes, we felt that a demo to the client and other developers was in order. Now, the issue was that I already had a new branch on the local machine and this needed to be pushed to the Git repository and the new branch tracked, so that we could push/pull changes to/from the new repository. I found this excellent post at David James’ blog on how to do this gracefully.

The tip under “Take Three” was the most helpful, as that was my use case.

The steps that I followed were :

  1. git checkout -b <new_branch_name>
  2. Make changes in the new branch
  3. git push origin <new_branch> //This pushes the new branch to the Git repository
  4. git checkout master //This was needed because git throws an error otherwise
  5. git branch -f <new_branch> origin/<new_branch> //The -f flag forces the recreation of the new_branch and tracks it to the remote, origin
  6. git checkout <new_branch>
  7. Start working on the new branch like we do with any normal branch and push changes to it.

With these steps, non-linear development was possible on multiple versions of grails in our project. Once the code base is stable for the new version, we intend to merge the changes from there to the master branch and delete this branch from the remote with the command:

git push origin :new_branch //This will delete the branch from the remote.
//We will need to delete the branch manually from the local machine

Hope this helps.

Vivek

FOUND THIS USEFUL? SHARE IT

comments (1 “Creating a New Git Branch on the Local Machine and Pushing it to a Remote”)

Leave a comment -