Monday, June 18, 2012

Handy git commands

Clearcase was my first version control software. I used it for 2 years and eventually I liked it since most of the commands are really simple to understand. In these two years I used to debug the code on debugging branch. Once I was done with the fix, I renamed the branch to follow the standard specified by my company. The command below was really handy to rename the branch.
 [cmd-context] $rename replica:old_branch_name new_brach_name 
But when I migrated to Git version control, I couldn't get head around git. It took a while to discover that even git has command to rename the branch which is as following.
$ git branch -m old-branch-name new-branch-name 
This kind of practice leaves a lot of branches in your local and remote repo and sometimes you wonder to clean this up.
$git push origin :debug-branch 
The above command will delete debug-branch on the origin remote and below is the command used to delete the branch present in local repo.
$ git branch -D debug-branch 
This command can also be used with -d option. The only difference between these two commands is that -D forces the delete process but -d doesn't.
If you have made some changes in a file and now you don't want those changes. you can run the following command to do that.
$ git checkout <file-name>  
There are instances when you staged the file but now you want to unstage the file. For this use the below command.
$ git reset HEAD <file-name> ...  

Your valuable comments are always welcomed. It will help to improve my post and understanding.

  "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest." 
By : Confucius