Monday, 29 August 2011

Basic guide about git and basic commands

Like subversion (SVN), git is a specific program to maintain the code for the different versions. The user can update, commit and merge code with differente versions, even fork a version into several and have several branch of the same program running at the same time.

The main difference between the subversion and git is while with the subversion, the communication is direct between the local directory and the server, in git, there is a local repository where the code is saved.
  • Subversion: Local copy <-> Remote repository
  • Git: Local copy <-> Local repository <-> Remote repository
This creates a extra level of security and enables the possibility to make several commits while the user is not connected to the Remote repository.

To use git, you can follow several steps:
1. Set up git and test git:
I found really good guides done by the guys of GitHubs, so I am going to link them:
In fact, the guides have been created to use the account of gitHub. Because it is completely free (with open source conditions, which means your code is going to be read by everyone), it is worth enough to create a new account and test it. Anyway, if you need a more private server, I recommend unfuddle, which with a free account you have 200 Mb of private space.

2. Create a new project or join to a existence project
Once git is installed and correctly tested, you have two options: Create a new repository or use an existence one. In both cases you have to make sure that the remote server has your public key created in the Step 1 of this guide.

2.1 Create a new repository
Basically you creates a new folder and you use the command
git init

to create a local repository. More information here:

2.2 Join to a new repository
In case someone else has already created a repository and you want to join it, you can use the command
git clone username@server:serverUserName/directory
to download the repository from the remote server to the local directory. For example:
git clone
where username is your username in the github server.
This options is equivalent to the command checkout  of SVN.

3. Use git commands
I am going to list the commands in git that could be useful. You may be familiar with someone of them because they are similar to the commands in Subversion (SVN)

git commit -a -m "message"
It commit the actual changed to the local repository, which is stored in your own machine. Since the remote repository is what most of the people want to save changes, you should use the command push for it.

The option -m is used to directly input the log without using any text editor.
The option -a is used to commit all the changes from all the files. This is specially important when the user remove a file because with this option, all the files removed with the command rm will be also removed from the repository.
More information here:

git remote add remoteRepositoryName userName@UrlOfTheServer:localDirectory.git
This command is used to manage the remote repository. At beginning you should set the direction of the remote directory. The sentence is shown above. Make sure that the local directory ends in ".git".

After the remote directory is set, you can always modify it by editing the config file in the directory .git (.git/config).

Of course you can set several remote directory by using this command several times, but make sure that they have different names.

For example, this is the command to add my git repository in gitHub:
git remote add origin

Notice that I have used "origin" for the name of the remote repository. This is because the command push which is used to upload the content of the local repository to the remote repository could be used without type the name of the remote directory. It assume that the local directory is the one in the configuration file which is called "origin".

More information here:

git push remoteRepository LocalDirectory
This command commits the last version in the local repository to the remote repository. Normally the local repository is consider master and the remote directory consider origin. So, normally this command is used like:
git push origin master
If you have set the name of the remote repository as "origin", this command could be shorted and used like:
git push
It automatically looks for in the file .git/config the remote repository called "origin" and commit the changed on it.

git pull origin master
This command works like the command update of subversion. It is used to obtain the last version of the remote repository and save it to the local repository.

More information here:

git status
This command works like svn status and it checks the actual status of the repository. As in subversion, it is relative to the directory. The status could be:

  • ' ': unmodified 
  • A: Added
  • C: Copied
  • D: Deleted
  • M: Modified
  • R: Renamed
  • U: updated by unmerged

git log
This command is equivalent to the command log of SVN. It shows you all the logs sorted by time that has been uploaded when the command commit has been executed. More or less the lines could be like this:

commit 718a280f2d595d2s82bf6579e07d471a8376beb8
Author: Jiahao Liu Liu <>
Date:   Mon Aug 29 15:12:36 2011 +0200

    change a bit readme file

It includes a hash code, the author, the date of commitment and the logs.

More information here:

Add new files
git add fileName
In order to keep track about any file in the repository, it must be explicitly added with this command. It could also add a directory. In such case, all the files in such directory will be added recursively.
After add new files, don't forget to do commit and push. Otherwise, the changes won't appear in the remote repository.

More information here:

Remove files
There are two ways to remove a file:
git rm fileDir/fileName
This command removes the file from the directory and notice it to the local repository. Then, with the command git commit, the removed files will be also removed from the repository.

rm fileDir/fileName
The second command remove the file without any notification to the local directory. Then, when the command git commit is used, the option "-a" must be added. With this option, when git upload any change to the local repository, it checks also all the changes from all the files under the directory and also removed files.

More information here:

For more complete information about git, you can visit:

For who come from SVN, this guide will helps you better:

No comments:

Post a Comment