git branching « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ git branching ’

Using Git Rebase

Posted by on January 22nd, 2012

Working on a cool project, doing cool and unusual stuff has its own charms and challenges. For doing cool things, you need cool tools to play with and Git surely is one of the coolest ones around. One of the many powerful features of git is rebase.

While working on a large project, you almost always develop your feature on a new branch, and for a large enough feature/requirement, It could take up to month or more to deliver it.

Lets consider a scenario where you need to begin on a new feature and you create a separate branch,(coolNewStuff) , off your main integration branch, (master).

New Branch from master

While you were working on the stuff, other guys in your team were pushing and integrating their stuff into the master branch. As a result master branch moved ahead and there were loads of commits and other stuff added to it.

Now before pushing your cool stuff into master, you would need to pull in the changes in the master to your current(coolNewStuff) branch. You could easily do that using

git pull origin master

Resolve conflicts. commit changes and merge this branch with master.

Nothing wrong with that actually, except that it creates a lot of intermingled commits in the branch, and it won’t give a clear roll back point.

What you actually want to do is put your work on top of the existing master branch as a separate commit so that you get a clear roll back point in case something goes wrong.

You want your changes of coolNewStuff branch on top of current master head, you don’t want master on top your coolNewStuffBranch.


Enter Rebase.

What rebase allows you to do is rewrite the history of your branch. You can actually pull in the changes from master branch place them above the master branch, you branched out off  (some 30-45 days back) and then place all the stuff you developed (all commits you made since the day you branched off) on top of this new pulled in changes from master. So, this looks like…

All you need to do is on the coolNewStuff branch call

git rebase master

This will start your rebasing process. It will basically collect the changes you made since you branched out of master to another temporary location, merge the old coolNewStuff (which was same as the master at the time you branched off) with the new changes on the master and then places your changes on top of it.

This process rewrites the history of the branch.

If you get conflicts. resolve the conflicts and add the files back

git add fileName

Continue with rebase

git rebase --continue

Do a git log and see that your changes are on top of the latest changes of master branch.

Now You won’t be able to push the changes to coolNewStuff branch as the history of branch has been rewritten.

On doing git status you would get a message saying

Your branch and ‘origin/coolNewStuff’ have diverged

You will need to remove your remote branch

git push origin :coolNewStuff

and push again. Do remember to notify other guys who are working on the coolNewStuff branch to delete their local coolNewStuff branch and fetch the new coolNewStuff branch.

Now when you merge this branch with master you will see that your changes are on top of the recent stuff on master and you can easily roll back the stuff in case something goes wrong.

Hope this helps.

Sachin Anand

email : sachin[at]intelligrape[dot]com

twitter : @sachin__anand

Posted in git

Git – How to Change Master Branch

Posted by on August 29th, 2011

While working on Git with a big team, there are chances that a situation might arise when you want to set some other branch as master branch.Recently we were in a same situation. Several of branches were merged to master branch. And unfortunately one of the branches merged was created from an unstable branch. Making our master branch unstable. And we cherry-picked all stable changes to masterTemp branch.So we landed in a situation where we wanted masterTemp branch as master branch.And Here is how we did it.

1 ) Renamed master branch to oldmaster.

git branch -m master oldmaster

Now there is no master branch on my local machine.

2) Renamed my masterTemp branch to master

git branch -m masterTemp master

The branch which was named masterTemp on my local machine is now master

3) Delete the branch from remote

git branch -rD master

4) Push the new master branch to remote

git push --force origin master


And its done.We had to perform step 3 and 4 because git does not allows us to delete master branch from remote. i.e git push origin :master wont work. Hence we forcefully pushed out new master branch to remote. Plus anyone who is working on same repo should delete their master branch , take git pull and create new master branch.
(i.e git branch -d master; git branch -rD master;git pull ; git checkout -b master origin/master )

_________________________________
Hitesh Bhatia
Mail,LinkedIn,Facebook,Twitter
_________________________________
Posted in Grails

git branching model: choose branches for deletion – see whats remaining for the merge

Posted by on March 4th, 2011

This post is for people who are already familiar with Git and work on multiple branches (esp. feature branches).
By practice, feature branches are the branches created from a root branch (master or any other as per your branching model) and once feature is complete/tested – they are merged back into the same root branch.



Working in such a manner is pretty nice and clean. But things start getting messy if we don’t delete the branches on time (esp. after merging into root branch). In that case when we run command –

     git branch

It shows us zillions of branches and we start getting feeling to get a rid of all futile branches. But we don’t know which branches are already merged and which are still under progress or yet to be tested.

Solution to this problem:

      # I am assuming that your root branch is master
      git checkout master
      git branch --merged


Above command will show you all branches which are already merged into the current branch (master branch in this case). This means you can delete these (feature) branches now.
For deleting a branch on local: git branch {-d | D} ‘feature-branch-1′
For deleting from remote: git push origin :feature-branch-1
I assumed that origin is your remote.

Similarly if you run

      git branch --no-merged

It will give a list of branches which are yet to be merged.



Question: I got a list of branches which are not merged yet. Now I want to see the commits in particular branch which are not merged into root branch yet?
Solution:

      git log --pretty=oneline maser..branch1

Again, master is your root branch. And branch1 is your feature branch having commits yet to be merged.

Your comments are always Welcome. Please post if you have any!



Cheers!
Salil Kalia
Salil [at] IntelliGrape [dot] com
Twitter LinkedIn

Posted in Grails

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

Posted by on September 9th, 2010

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

http://in.linkedin.com/in/svivekkrishna

Posted in System