Month: December 2015

5 least used Git hacks

Git is undoubtedly the best thing happened to the programming lifecycle. To the people who don’t know git, it is a widely used version control system for software development. While git being git , saves most of the troubles software engineers having related to versions, collaborating, going back and forth in developing features and adding them to the software, there are lot of commands in git which are not very commonly used but saves your time in a great deal.

(This article assumes that you have a basic understanding of git workflow)

Git stashing

Whenever you have any uncommitted changes in your git repository and you want to change the branch, you may have come across the following error.

Capture

When you have uncommitted changes which you still want to keep, then you just have to type “git stash”. It will record both your staged and unstaged changes internally. When you are ready to comeback to your incomplete work you just have to type the command “git stash apply”. This command will only apply your most recent stashed changes.

You can see your stash history by “git stash list”, It will give you the list of stashes which are stored. If you want to apply an older stash, you have to specify the name with the command like “git stash apply stash@{1}”.

Capture stash list

Git doesn’t provide a command like “git stash unapply” to unapply a stash. If you want to unapply the changes that came from the stash , you have to do it by simply retrieving the patch associated with a stash and apply it in reverse using “git stash show –p | git apply -R”. This will revert the changes in the most recently applied stash. If you want to specify a stash to unapply you can use the above command as this “git stash show –p stash@{1} | git apply -R”.

If you are more into it , you can also create a git unapply command yourself so that you don’t have to remember the whole thing or google it everytime you need it by using aliases which is discussed in the last section in this blogpost.

Following are few more commands associated with stash

git stash apply –index” – When the stash is applied using the normal “git stash apply” command then your previous staged changes will be unstaged. If you want your previous position with all your staged changes staged, then you have to use the apply command with “–index”. This will give you the original position you were in before stashing.

git stash drop stash@{0}” – When you apply a stash using “git stash apply” command it will continue to stay on your stash list. To remove your stash from the list you can use the above command.

git stash pop” – If you want to apply the stash and immediately remove from the list, then you can use the above command.

One of the other use cases of using stash can be like this. You have two feature branches in your git repository named feature1, feature2. You start developing something in feature1 branch and suddenly you figure out this is not something you need in feature1 branch, but needed in feature2 branch. All you have to do is stash the changes, switch the branch to feature2 from feature1 and apply the stash. Applying the stash might cause you merge conflicts. But if you are using git you should have lot of experience in resolving merge conflicts. So I am not going to focus on them here.

Git reset

This is not very uncommon command as you may have expected. But I am sure the only command you use all of the time is “git reset –hard”. But there is more to the reset command.

If you want to unstage a file or set of files from the staging index, you can use the “git reset” or “git reset <file_name>

If you have a commit to undo you can use “git reset –hard HEAD~1” or “git reset –hard“. 1 can be replaced by number of commits you want to undo. If you want to move some commits to a separate branch, all you have to do is make a branch from the branch currently you are in and then undo the commits. In this scenario you will have the commits safely on the branch you created new.

Suppose while you are working on a certain branch and suddenly you got to fix a bug in another branch. But your current work is not completed. So instead of stashing them you just commit the changes of the working directory and checkout the other branch and fix the bug. But when you return to your previous branch, the last commit of it would contain incomplete work. So you can use “git reset –soft” to revert the commit and keep the changes in the index. So you can start completing work from there.

If you want to remove a file from the index before a commit you can use “git reset <filename>”.

Git ignore

There are standards defining what types of files you should commit and not. If you are a collaborator of a large project, you may have come across what kind of files to commit and not. Most probably these are the files related to your IDE. You can use a .gitignore file in your repository to mention all the files which should not be tracked intentionally. A .gitignore file should be committed in your repository in order to share your ignore rules with other collaborators. Github provides a list of recommended .gitignore files for popular operating systems, environments and languages. (https://github.com/github/gitignore)

But if you have a file which is already tracked by your repository adding that to the .gitignore file will not stop git from tracking it. So you have to manually remove it.

Each line in .gitignore file specifies a pattern . # denotes a comment. ‘!’ can be used to negate patterns. Sample .gitignore file would look like this

#Complied Object Files

*.slo

*.lo

*.o

*.obj

# Precompiled Headers

*.gch

*.pch

Git cherrypick

This is the most useful command which will get out of you from most of the problems. Git cherrypick allows you to merge a single commit from one branch to another. This is very useful in situations which you do not merge the whole branch because of inconsistencies in versions and etc. cherrypicking is very simple. You have to first get your hash of the commit. You can use git log or any other preferred method. If you are using github the yellow highlightes numbers represent the hash associated with each commit in the following picture.

hash.PNG

Then all you have to do is use the command “git cherry-pick <hash>” in the branch you want to apply the commit.

Git aliases

Aliases is the most amazing concept of git which will let you own it. Here is how it happens. If you are too lazy to type the git commands you can add short aliases for them. For example let’s take the command “git checkout” . You can create an aliase “c” for it by using the following command “git config –global alias.co checkout”. Then next time when you need checkout you just have to say “git c” instead of “git checkout”. As we discussed earlier in the stashing section you can create your own “git stash unapply” command using “git config –global alias.stash-unapply ‘!git stash show -p | git apply –R

 

Resources: https://git-scm.com/docs

Getting Started in Open Source

Last week I have met several college students who are willing to contribute to open source but do not know where to start from. Few years ago I was in the same boat. Since GSoC 2016 is getting nearer and students have already started to find organizations and start contributing, this would be the ideal time to publish the post.

Get your skills and abilities listed

Fist you have to analyze your self. You have to figure out what you actually want and what are you comfortable with. For example you may be an expert in Android and want to contribute to a project related to Android. Or else you may know the basics in Android, by contributing you want to get more experienced. In a completely different scenario, you know Java but you want to contribute to c++ project to learn c++ while contributing. So there can be many reasons why you want to contribute to open source. First take some time. Take a pen and paper and write down what you really expect from contributing to open source.

Once you know what you want, you have to figure out your strengths and weaknesses related to that area. Then you will be able to get a good understanding on for what you want to contribute. Once you get your skills and abilities listed with your expectation you can start finding a project.

Select a project or two to contribute

Many people have this misconception that finding a project to contribute is the most difficult thing in the process.  Believe me it is not. Once you know what your expectations are, you have to list out what kind of a project you need. You have to focus on the project’s technical details for this. For example you are a java expert and you want to learn c++ in the process of contributing, selecting a java project is not going to work. So you have to identify the technical details such as programming language and focus of the project (such as algorithms, machine learning and data science).

Once you figure out the details you can directly search an open source project which matches your specification in google. Another way is go to the last years GSoC page and search your specifications. Then you will find the related projects.

Make a list of projects you like and go through each and every project. Most projects maintain wikis and you can find them in their start contributing page or similar pages. Read through them and try to understand what the projects are about. According to your understanding on them select one or two projects of your interest. Then comes the interesting part.

Subscribe to their mailing list

Once you found the project, subscribe to their dev mailing list. Most projects have the details on how to contribute/ getting started sections. If you are not comfortable introducing yourself to the community just yet, wait for some time and read the mails coming from the list. If you do not understand anything they are talking about, just ignore them. But by reading these mails you will get a good idea about communication and  Do not try to overthink and ditch the project and start another.

Shoot an introductory mail

Once you are familiar with the mailing list and mailing list etiquette, send an introductory mail to the mailing list introducing your self and asking them  to help you on getting started. In most of the cases as a result to this question , a community member will direct you to a page where there will be some beginner tasks related to the projects listed. You can read about them and claim a task. The way of claiming a task is different from organization to organization. In most organization when you go to the ticket of that task, you can comment and clarify your questions and tell them that you are interested in doing that specific task. Then they will assign it to you.

Start Contributing

Now that you have introduced yourself to the community and found a task, you can can start working on the tasks. In most of the open source projects, they use git for version controlling. What you usually have to do is fork the project, make changes to the code according to the task you are assigned and then make a pull request to the project’s repository in the organization. You can ask from the community about how they are going to do this.

 

GHCI Preconference Tips

Since GHCI (Grace Hopper Celebration of Women in Computing India) is getting near , here are some tips for the first time attendees to make your experience a comfortable one.

Plan your travel itinerary.

If you are an outstation attendee and haven’t been to Bangalore before, it’s better to plan your travel itinerary before arriving at Bangalore. Plan the travel details from the airport to your accommodation and, from your accommodation to the conference. If you are unfamiliar with the local language and area, you can always use the Google map offline feature and save the route beforehand so that you can even use the Google map with the gps to identify your location and route without an internet connection.

Plan the sessions you want to attend beforehand.

In GHCI there are several sessions going parallel. So deciding the sessions which you want to attend can be a little bit tricky, since all the sessions looks promising. So before coming to the conference take some time and read the detailed version of agenda and description of sessions. Never judge the session from its name. Go through the detailed description of each sessions and figure out the sessions you need to attend.It’s better to be prepared early to avoid last minute mistakes of missing your favorite sessions. Remember that during the conference sometimes the sessions can change and the changes will be notified. You can also use the Convene app for the conference. Believe me, it will make your life easy.

Go to the conference venue early (first day).

In  the first day, registration ques tend to be long. To avoid the long ques get there early and then you have the time to get familiar with the conference venue before the conference starts.