Estimated reading time: 5 minutes
Posted on July 16, 2012

Use Git Like a Pro

We all use different version control systems (VCS). Each has it’s own 
peculiarities, pros and cons, but my heart fully belongs to one – Git. It 
is enough to know just 5 key commands and vuala! – you are a Git user. But 
at the same time, it is a very powerful and sophisticated tool aimed
 at resolving issues of any kind.


git pull
 #make changes
git commit -am "changes description"
git push

Below, I’m going to talk about my favorite commands (the upper intermediate level).

Commit nazi (Git merge –squash)

OK, let’s say I get a new task for the whole week and keeping to common
 sense I create a separate branch where I commit on a regular basis 
several times a day.

In the end, it turns out that the customer prefers 
reading log files on GitHub, and requires that each task has a single 
commit. No problem!

git checkout master #leave our branch
     
git merge --squash dev_branch #get changes
     
git commit -am "A nice and clear single commit for #123 task"

Merging branches with the squash parameter prevents you from merging
 all commits, and it only merges changes you need to commit with a nice 
description as per the customer request.

IMPORTANT, this method should be used only if you are not going to merge the master branch with the branch you have just merged using a 
squash flag, otherwise conflicts are inevitable.


Hurry up, the top priority! (Git stash)

Imagine another situation. You are writing the application core,
 and BAM! The designer says that it’s urgent, almost vital to
 change the color of the lower left footer block from sky-blue to azure 
blue :) OK, in order to change the branch, but not pull all 
changes with you (b/c making a commit is too long and it’ll be full of debug
 code), you can do the following:

git stash #push all changes into the "pocket"

git checkout another_branch # change branches

make necessary fixes,

git commit

git checkout main_branch #get back to the point you've been before
     
git stash apply #restore the changes from the "pocket"

git stash clear #shake out the "pocket"

and keep coding as if nothing had happened!

Make me unsee it (Git revert)

It’s Monday morning, Youtube videos are especially interesting, and 
coffee is surprisingly good! But your tranquility is suddenly
 interrupted with a blinking Skype
 icon:

Your disappointed colleague:

Hey, dude, do you remember the cool thing that you installed 2 weeks ago? Unfortunately, the client says that they do not need it anymore because they have changed the scope of activities from slave trading to sheep shearing :) So please roll back to the previous state of things.

It’s a shame, but okay:

git log # search for a commit hash

git revert 87gd87g6h #editor opens with text like "This
reverts commit 302c00a4f85d5962d109e9365d86c912e8040cae."

After saving the message you’ll see a commit with all the changes made indicated in the revert command.




NOTE 1 You can also specify the flag -no-edit, and this
 commit with the above mentioned comments will be created automatically
 without the necessity to open an editor.




NOTE 2 If you want to completely undo the last commit 
changes, you can use the command git reset – hard, BUT first
 remember to carefully read the manual about this command. It physically removes all 
the changes together with the commit and does not require making another rollback commit.


Spy, get out! (Git file checkout)

Everything is very simple here. Sometimes you check Git status and see a stranger file you don’t remember adding, but it is still there, like a little spy =) In such a case you can do the following:

git checkout spy_filename

The command will reset all uncommitted changes to the specified file.

Who’s the star? (Git blame)

All of you have probably faced a situation when you open some 
class and see a code snippet that is incredibly beautiful, so 
amazing and elegant that it overwhelms you with its harmony and a 
brilliant architectural idea. And you just cannot pass by such a 
beautiful creation without knowing the name of its author.

Nothing could be simpler:

git blame genius_class_filename`

Git will show what changes have been made to the file, when and by whom. So all you 
need to do is find the desired line, see the name of its author and express your admiration =)

See you, bye (Git push origin :branch)

A problem often migrates from one developer to another (someone is
 busy with a more important project, other one has taken a vacation, someone is 
just too lazy but has a powerful gift of eloquence and
 convinces that it’s you whose mission is solving this problem =)). In any case, the most 
rapid transfer is by pushing code into the main development branch from which 
another developer can easily pull it into his own. But at the same time, 
you do not want to show the unfinished code to your customer with comments 
like:

My God, I wish this customer would be gnawed by beavers for such tasks =)


You may wonder how this magic is supposed to work.

But it’s easy: after you upload the branch to the local repository, execute


git push origin: dev_branch

The whole branch will be removed from the prying eyes.

Finally…

Git is a very powerful system and you can never-endingly find something
 new in it. It is just a teeny-tiny part of it that is described here. Those who wish to get better familiar with this VCS can look at a 
wonderful free manual “Git Magic” which, by the way, has been 
translated into several languages. The English version can be found here.

Feel free to share your own Git experience!

Adios!