Use Git to manage project files

Git is a version control software. It promotes teamwork by allowing contributors to work on several “branches” of a project. These branches, independent of each other, can be modified and validated before being merged into the rest of the project.

This post presents how to use Git to manage project files.

Prerequisites

Have a server running Linux (see Install and set up Linux Debian 7).

Install Git

On Linux Debian ou Ubuntu, execute:

apt-get install git

Create a repository

A repository is a directory that centralizes versions of a project. Each project has a repository whose content is automatically managed by Git.

To create a repository, execute:

mkdir /home/username/projectname.git
cd /home/username/projectname.git
git init --bare

Where /home/username/projectname.git is the path of the repository to create.

Initialize a working directory

A working directory contains the files on which a contributor is working on. A project can have as many working directories as necessary (eg one on the computer of each contributor).

To be managed by Git, a working directory must be initialized.

To initialize a working directory, execute:

git init
git remote add origin /home/username/projectname.git

Where origin is the identifier of the repository (origin being the default identifier of a Git repository), and /home/username/projectname.git the path of the repository (this path can be local or remote, and use the http, https or ssh protocol).

Import a branch of a repository into the working directory

git pull origin master

Where origin is the identifier of the repository (origin being the default identifier of a Git repository), and master the name of the branch on the repository (master being the default name of the first Git branch).

Make changes to the working directory

Each contributor can freely modify the content of his working directory (add, edit and delete of files and directories).

The files and directories relevant to the project must be indicated to Git.

To add a file or a directory to the project, execute:

git add filename

The files and directories that have not been added will be ignored by Git. They will appear in the Untracked files section of the working directory’s status.

To know the working directory’s status, execute:

git status

Once completed, the changes must be submitted to Git.

Once changes made, they must be submitted to Git.

To submit the changes, execute:

git commit -am "Modifications' description"

Export a working directory to a branch of a repository

git push origin master

Where origin is the identifier of the repository (origin being the default identifier of a Git repository) and master the name of the branch on the repository (master being the default name of the first Git branch).

Manage the branches of a project

Manage branches allows to develop a project in several directions at once. It is thus possible for a contributor to work on a branch without compromising the functioning of the rest of the project. Each working directory and each repository may contains its own list of branches.

To create a branch in the working directory, execute:

git branch branchname

To list the branches of the working directory, execute:

git branch

To load the contents of a branch into the working directory, execute:

git checkout branchname

To merge a branch into the working directory, execute:

git merge branchname

To remove a branch of the working directory, execute:

git branch -D branchname

To remove a branch of the repository, execute:

git push origin --delete branchname

Where origin is the identifier of the repository (origin being the default identifier of a Git repository) and branchname the name of the remote branch to delete.

Leave a Reply

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

*