Learn Git and stop being one :D

Posted on diciembre 29, 2011

0


Well during the linux kernel development Linus Torvalds wasn’t much happy about the version control system used, so that’s how he started to work on git. As he had that background of developing operative systems kind of tools and stuff, he developed this a lot like if it was a file system, and you can notice it in the way git works.

So this is how Git was born and has started to become more used, since nowadays people have been really starting to migrate from the original centralized type version control systems, to a more distributed type version control systems like Git. Eventually it also has become more known thanks to the rise of forges, like GitHub and Gitorious, that had become very popular since they make it very easy to start and to make a fork of a hosted project.

This is how git popularity has increased in this last few years, so this is the reason why I suggest that if you are a developer you should stop being a “git”, and start learning it :D. And this is also the reason why I am going to give here a quick review of the main git commands.

So lets say you want to start a fresh git repository from scratch, what you would have to do is first to create from a shell a new directory, open the directory and initialize the git in there, with the following:

$ mkdir project
$ cd project
$ git init

this last line is the one that will actually create the git under the project directory. What this instruction does is quite simple and it basically creates a ./git (which in linux is hidden) where it stores all the repository. So if you would like to loose your repository you would have to simply delete this ./git file and that’s all.

Then we have the “git add” which is used as the example below, where file1 and file2 are files that I just made some changes so I’m about to save them to the repository

$ git add file1 file2

what really git add does is just to mark the files received as parameters and put them in a list with the files that are about to commit, called index. So this index acts kind of like  a precommit zone, since it is where all the added files go. Then you’ll have to commit them in order to get them finaly saved into the trunk, like this

$ git commit

the commit allows me to make a point to which I can return at any given time. But before doing this git commit you could always see the changes you just did and that you are about to put into the repository with

$ git diff –cached

specifically this diff –cached shows you the changes between the index and the trunk. But you have to know there are more options that the diff command have that could help you to get useful information while working with your repository.

Another one which is very useful is

$ git status

this one tells you the files that have been modified since the last commit, so actually it is a good idea to do this everytime before doing a git commit just to be sure that now you are going to commit the right files.

also very useful is the log, which is the way to get all the history of our repository

$ git log

you can also use this with the help of the dot “.” in case you would like to get the history of an specify directory like this

$ git log .

And finaly I’m going to talk about the commands that will help you work with remote repositories. So let’s say you want to work with some repository, once you have the url the fist thing you will do is to get your own copy of that repository, so you do this with

$ git clone <uri>

Then of course if you have this repository is because you may want to be watching the latest changes the developers do, so in order to update your local repository with the changes that people may have done in the remote repository you do it with

$ git fetch <uri>

and

$ git merge origin/master

the first one gets the remote changes but stores them in my .git, so I won’t be able to see them in my working directory. This is why then we’ll have to use git merge, to finaly merge my working directory with the changes. But this two instructions are so much used that you could do them both with the following

$ git pull

and the last one you’ll need is

$ git push

which is how you update your changes into the remote repository. And this are the basics I consider you’ll have to know to begin using git, so I hope that helps you people.

Tagged: ,
Posted in: Uncategorized