Git Usage Guide

Git Usage Guide

Git is a free version control system to manage the source code of your projects. As well as other similar systems, Git allows you to keep track of changes made in your application, allows you to separate the implementation of new features and bug fixes of your production version, in addition to assisting in your project’s management. In this post I will explain how to install and how to use the main functions of Git. As a complement I’m going to talk a little bit about GitHub, too.

An important difference when compared to most version control systems is that every user of a Git repository has a full history of changes. Most systems have a central server, where you search and commit your changes – in order to check the history you need access to this server. In Git’s case you have everything you need in your local repository. You access other repositories only to synchronize changes from other people with yours.

Installation

Installing Git is pretty simple. Visit http://git-scm.com/ for downloads and additional instructions.

Linux

On Linux there is no need to download anything from the website, you can use the command line directly.

Debian/Ubuntu

On distributions derived from Debian, use apt-get. You may need to use sudo before the command.

Fedora

Windows

On Windows the installation is quite simple too. Simply download the installer and run it. The installation wizard will start. Do not change any of the default options, unless you know what you’re doing. The installer will create two applications, Git GUI and Git Bash. Git GUI is a small interface for some of the commands, while Git Bash is a command-line utility that allows you to work the same way on Linux, without the need to change the Windows’ PATH (you can change during the installation if you want).

Mac

To install on Mac, just download and run the file. Run the wizard and you are ready to go.

Commands

Now let’s get down to business. Git has several commands, but the main features are creating/cloning repositories, add/confirm changes, get/send (synchronize) changes to other repositories and create/merge branches.

Create / Clone

In order to control the source code it is necessary to create a Git repository. This repository must be created inside the directory where your source code resides – this will create a sub-directory .git with the versions information.

Create a new repository:

Clone a repository locally:

Clone a remote repository:

You can clone Github repositories using “git clone https://github.com/user/repository.git” or “git clone git@github.com:user/repository.git”.

Add / Confirm

When you create a new file or modify a file that is controlled by Git, you need to add these changes so that they are committed in the next commit. If you start a new repository with existing files, it means that all files are new, so you need to add all of them before commiting.

Add modifications to a file:

Add all files (often used after git init):

Perform the commit (confirmation) of updates:

Synchronize

When we do the commit we are confirming the change only locally – in our repository. To work with other people we need to synchronize our changes with other repositories. This involves sending our modifications and also getting the commits from other people.

Connecting our repository to a remote repository (origin is the name that we give to the remote repository):

Submit changes to the remote server (origin is the remote repository and master is the branch):

Get updates from the remote server:

Git pull does a git fetch (fetches the code) and a git merge (merge the remote branch) at once. It is more practical, but you need to be careful not to pull the wrong branch, or Git will update your code with the wrong remote code.

Branches

Finalizing the basic commands, one of the greatest benefits of a version control system is the use of branches. You can keep your production code in the master branch and make new developments in the dev branch. This allows you to fix any bugs straight into the master branch and update your production server with this branch, without worrying about new developments in dev.

Creating a branch:

Switch to the master branch (you will always be working with one branch at a time, and can change what the branch is being modified):

Remove a branch:

Merge changes from one branch to another branch (if we are in dev and want to copy changes made to master):

View differences between two branches:

GitHub

One of the great things of Git is the existence of GitHub. If you don’t know it yet, there’s something wrong. GitHub is a source code repository where you can share your code openly (there are lots of open source projects on GitHub) or not, using private repositories for your yourself or your company. This way several people can work on the same code even if they are in completely different locations.

You can create your own Git repository server, but GitHub offers some additional services like activities management (issues – they might be bugs, new features, etc). Each activity is assigned a unique number. At the time a developer does a commit on your code, he or she can associate the number of this issue so you can check exactly what was done. It even offers some “social” features for you that want to share your code openly.

Last commit done in the jQuery project

Graphical Interfaces

There are several applications that allow you to work with Git through graphical interfaces, without the need to type all the commands manually. These applications implement the functionality in a graphical interface, making your life easier. In fact, any good code editor is already integrated with Git (NetBeans, KomodoIDE, etc). A list of GUIs is available at http://git-scm.com/downloads/guis.

Conclusion

If you don’t use any version control system, Git is a good alternative to change that. For those who work as a team and need to control what is done in your application (who doesn’t?), Git is a great on of the best options.  Even if you work alone having this kind of control is very important. It’s a kind of insurance for when you need to rollback changes in your code.

Links

Some interesting links for you that want to learn how to use Git:

Leave a Reply

Your email address will not be published. Required fields are marked *