git « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ git ’

Using Git Rebase

Posted by Sachin 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

  • Share/Bookmark
Posted in git

Creating GIT Alias

Posted by Hitesh Bhatia on December 30th, 2011

Since Git is awesome. It also provides functionality of making aliases.
Example

git config --global alias.co checkout 

Here we created co as an alias for checkout (All the aliases that are created goes into .gitconfig file under home folder). Now to checkout a branch named testBranch, We can also write

git co testBranch
Switched to branch 'testBranch'

Here are couple of interesting git aliases that we can make

1.git config –global alias.undo ‘reset –hard’

Now if we write “git undo” then any of changes after my last commit will be removed.

2.git config –global alias.cleanup = ! git fsck && git prune && git gc

In this Command we have used exclamation mark (!), this indicates that these commands would be running on terminal.
And in git if I want multiple command to be running one after another we use double ampersand (&&) (We cannot use semicolon as we would usually do in terminal).
This command will run git fsck (file check), git prune (to clean dangling commits/blobs) and git garage collection

Number of git alias that can be made are endless all it needs is little creativity.

_________________________________
Hitesh Bhatia
Mail,LinkedIn,Facebook,Twitter
_________________________________
  • Share/Bookmark
Posted in Grails

GIT Maintenance

Posted by Hitesh Bhatia on December 30th, 2011

Since we use git numerous times a day over and over, hence needs maintenance. There are few commands that will help you to keep our git healthy. If you feel that you git has somewhat slowed down, these would do the trick for you.

Since ours is a pretty big project . I run it once a week to make sure my git is healthy.

1.git fsck

This commands helps to identify any corrupted, unreachable (dangling blobs) objects in your database.
Example

git fsck
dangling commit 652424f02c17b5d881158a625f1a34f6039dc231
dangling commit 7e6510c2663530d949ea9fb67900496dcf5cbc4c
dangling commit 177864469ea87564ade9101106d5aa85cceb0d29
dangling commit 848665007bc412910b68dc3e528e74c23ce1f165
dangling blob dfa6756511b525996946de84fa7f67b6f9a0d32f
dangling blob e7f8f1145c7e739a6ffd8806a6d26d60f0443897
dangling blob f1f905a5eb35724622249f25f6d9ef7c646e4283
dangling blob 221417781e017cf02f90b872aea80f30ed41787c
dangling blob c1356b24fb2a4ea453dba87ed4058e7a48e97dce
dangling blob cb6fe3d5321b7b152a8781541bbc6132c59b9c94
dangling blob 3ce2e76cd54f0c8265ec6167f78d5dfe71c2242d
dangling blob daf073729960c40a44c5291c745092445bb2c3b0

2.git prune
With git fsck, we identified some of dangling commits and blobs, now to remove them we use git prune.
Now After running git prune, If I run git fsck again, my git would be cleaner than before and healthy.

git fsck
dangling commit 177864469ea87564ade9101106d5aa85cceb0d29
dangling commit 848665007bc412910b68dc3e528e74c23ce1f165

But it still has some dangling commits.This is where git’s garbage collection comes in play.

3.git gc (Garbage Collection)
This command will gathers up all the loose objects and places them in packfiles. And removes objects that aren’t reachable from any commit. This command will save some space and speed up many of git commands.
Example

git gc
Counting objects: 121775, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (18239/18239), done.
Writing objects: 100% (121775/121775), done.
Total 121775 (delta 76189), reused 120913 (delta 75659)
Removing duplicate objects: 100% (256/256), done.

Output of this commands tells several things to us , that is compressed 18239 objects for us out of total 121775 objects. And it also removed 256 duplicate objects. This way my git stayed healthy.

_________________________________
Hitesh Bhatia
Mail,LinkedIn,Facebook,Twitter
_________________________________
  • Share/Bookmark
Posted in Grails

Git – How to Change Master Branch

Posted by Hitesh Bhatia 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
_________________________________
  • Share/Bookmark
Posted in Grails

Using git diff feature on Github

Posted by Abhishek Tejpaul on June 7th, 2011

Hi Folks,

Recently I came across a cool way to compare differences between two branches, two tags, or between two commits on Github. Many a times in our project we have to thoroughly see what has been a specific change in the code base before we push it on our production branch.

Here is how you can view the differences in commits:

On the Github, go to the Source view of your project. You will see a link named ‘Branch List’. Once the page opens you can see a list of all the remote branches. Hit on the Compare button in front of any of the available branches to see the difference between two branches.

Now note the URL in the address bar. It should end with something like ‘…/compare/<x>…<y>’ where x and y are separated by three dots(…) and their values could be project’s branch names

Isn’t it good?

Well, Git’s (read about git diff) and Github’s goodness does not stop just here. Instead of branch names as the values of x and y, you can also put two different commit hashes or tag names to view the differences in the code-base. More so, the commit hashes do not have to belong to the same branch. So you can pretty much compare your code’s current snap-shot with any of its past snap-shot irrespective of branch or a tag or a commit hash.

Hope this helps someone.

Abhishek Tejpaul
abhishek@intelligrape.com
[IntelliGrape Software Pvt. Ltd.]

  • Share/Bookmark
Posted in System

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

Posted by Salil 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

  • Share/Bookmark
Posted in Grails

GIT –pretty

Posted by Hitesh Bhatia on February 18th, 2011

git log is one of most useful commands to see information about commit , author , date and subject(comment)  while using GIT.This displays information in blocks and hardly 5 – 7 can be listed on normal screen size. But git provides –pretty option , so that we can format the output of git log;

Here are some ways to do that

git log --pretty=oneline

Displays just a single line log ,which includes a commit-hash and Subject(Comment).something like this

d1fc23c1edfd2d4ffe8ef5a873adff9321cde909 comment 3
a8560c814ece4e03d39164885f44e0c5cb749928 comment 2
db6195c635179b3556c41e2a974829c7e101ef51 comment 1

some other predefined options are raw,fuller,full,medium and short. Since Git is Cool we can also use some custom formats.Like

git log --pretty=format:"$h"

will give abbreviated hash of committs(Where %h defines abbreviated hash).And it would be something like

d1fc23c
a8560c8
db6195c

Which is not quite useful , to make it better lets first see other options

  • %H  – Commit hash
  • %s  – Subject
  • %h  – Abbreviated hash
  • %an – Author name
  • %ae – Author e-mail
  • %ad – Author date
  • %ar – Author date, relative



While using “format” with option “–pretty” we can also specify string inside quotes.Like

git log --pretty=format:"%h was committed by %an , %ar"

Would give something like

d1fc23c was committed by hiteshBhatia , 5 months ago a8560c8 was committed by hiteshBhatia , 5 months ago db6195c was committed by hiteshBhatia , 5 months ago

To make this more useful we can make slight changes to string passed to “format”

git log -3 --pretty=format:"%h ||  %an  ||   %s  (%ar)"

Which will give us commit hash , author name , subject  and relative date inside paranthesis.And -3 here is number of logs .So that his whole screen is not cluttered.This is the way I like to use it.

d1fc23c ||  hiteshBhatia  ||   comment 3  (5 months ago)
a8560c8 ||  hiteshBhatia  ||   comment 2  (5 months ago)
db6195c ||  hiteshBhatia  ||   comment 1  (5 months ago)



To explore more visit. git grep manual page or the git community book which is maintained by Scott Chacon

_________________________________
Hitesh Bhatia
Mail,LinkedIn,Facebook,Twitter
_________________________________
  • Share/Bookmark
Posted in System

Git – Grep

Posted by Hitesh Bhatia on January 22nd, 2011

Still in my early days of using Git. Recently just to make sure that I have merged two branches  and have pushed changes to QA server successfully,I often used command “find | xargs grep” .But to deal with this kind of situations Git provides “grep” command. Just as name suggests this commands searches for regex pattern passed to it.

 git grep "regex" 

Example – To find usage of word “collections”  in each file.

 git grep --ignore-case "collections"

output :

ReadMe.txt:groovy Collections
ReadMe.txt:nice collections

Now this just described which of project files have word ‘list’ in it and how many times.One can also use -c option to count occurrences  of word “collections”

git grep --ignore-case -c "collections"

Output:

ReadMe.txt:2
Now a question  why I am not using command (and favoring “git grep”)
find | xargs grep -y "collections"
which will easily do the same for me and would perhaps list some extra  files (which are not exactly part of my app). Now here come the answer with “git grep” I can also search in previous versions of my project.
Example
git grep --ignore-case  "collections" ea145f1
output :
 ea145f1:ReadMe.txt:groovy Collections
It searches for word “collections” in my previous commits . (As second line in file ReadMe.txt was added after commit numbered ea145f1) .Simply awesome.And this is  not it.  To explore more visit. git grep manual page or the git community book which is maintained by Scott Chacon
_________________________________

Hitesh Bhatia

Mail,LinkedIn,Facebook,Twitter

_________________________________

  • Share/Bookmark
Tags: , ,
Posted in Linux, System

Fetching an old deleted file of a project using Git

Posted by Abhishek Tejpaul on November 15th, 2010

Recently in one of my projects, I had to bring the content of old-forgotten-deleted file back to the application code base using Git. The problem took a ‘fancy’ turn as the path to the file was no more a valid path in Git as there had been a major refactorings in our project esp. renaming of the packages as well as the folder structure since the file in question was deleted.

This is how I could see the deleted file (& its content) using the Git commands:

1. First of all, we will try to get the revision number of the git commit which committed our deleted file.
Type in, git log
on your console at the location where you have cloned/forked your Git project. This command will show a all the commits that has been done since the start of the project.
If you know at what period of time the deleted file was committed, you can use another flavor of git log command i.e.
git log –before=”Oct 01 2010″, or,
git log –after=”Oct 01 2010″
If you have an idea about who all could have or have committed the deleted file, then you can also use another option provided by git log command i.e
git log –author=”Author Name” or
git log –author=authorId
The commands above are self-explanatory.

NOTE: The step above would be helpful only if your team has followed good practices such as putting appropriate and self-explanatory comments while committing. So like if you have deleted a file & the comment while committing specifies that in some way or other it would be really easy for you to find out the probable revision number of that old commit by seeing the logs.

2. Now once we have the revision number, we can try using the

git show 

command to make sure that the deleted file was actually present in the commit. Type the following:

git show --name-only <revisionNumber> 

Once you see your sought-after file there, then you can see the contents of your file by typing

  git show <revisionNumber> 

This command will show you the change-set and the content of the file. Now you can fetch or copy the code out of the deleted file and place it in your new code or create an entirely new file out of it.

Hope this helps !!!

Reference: http://cheat.errtheblog.com/s/git

– Abhishek Tejpaul
abhishek@intelligrape.com
[IntelliGrape Software Pvt. Ltd.]

  • Share/Bookmark
Posted in System

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

Posted by Vivek Krishna 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

  • Share/Bookmark
Posted in System