Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / Git

How-to Install and Setup Git and GitList

5.00/5 (1 vote)
29 Jan 2015CPOL9 min read 20.2K  
In this tutorial we will provide you with detailed instructions on how to install and setup Git and GitList for your Git repositories

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

Introduction

When you are working on a Web or software project, alone or with a team of developers, tracking your changes over time is important. Version control allows easier collaboration between different groups of people working on the same project. There are many VCS (version control system) tools available that can be used for source code management, one of which is Git.

What is Git?

Git is a free and open-source, distributed VCS designed to handle everything from small- to large-scale projects with speed and efficiency. Unlike most other SCM systems like Subversion and Perforce, Git does not store information as a list of file-based changes. Instead, it stores a snapshot of what all the files in your project look like in a tree structure each time you commit. This is an important distinction between Git and nearly all other conventional VCSs. For more information, visit http://git-scm.com/.

When developing great Web projects, you need great hosting to unlock their full potential. 1&1 Internet provides all the tools you need for ultimate freedom as a developer

Installation

Linux

To install Git on Linux, execute the following commands based on your distribution (as root or using sudo):

Red Hat/Fedora: yum install git

Debian/Ubuntu: apt-get install git

You may also compile Git from sources. For more options and installation instructions for other UNIX flavors, visit http://git-scm.com/download/linux

Mac

The easiest way to install Git on MacOS is to install the XCode Command Line Tools which are part of XCode. For the most up-to-date version, you can download and run the OSX Git installer from the Git website, at http://git-scm.com/download/mac.

Windows

In order to install Git on Windows, download the latest installer package from http://git-scm.com/download/win and run the installation executable. The installer includes a command line version of Git as well as the GUI. Click the Git Bash icon to launch the shell -

Welcome to Git (version 1.9.5-preview20141217)

Run 'git help git' to display the help index.
Run 'git help <command>' to display help for specific commands.

user@1&1 ~
$ git --version
git version 1.9.5.msysgit.0

user@1&1 ~/training/HelloWorld (master)
$ git config --global user.email"user@example.com"

user@1&1 ~/training/HelloWorld(master)
$ git config --global user.name"username"

Setting up a Git Repository

The git init command creates a new empty Git repository or reinitializes an existing one.

$ git init

Usage

user@1&1 ~/training/HelloWorld
$ git init
Initialized empty Git repository in c:/Users/user/training/HelloWorld/.git/

Creates a new .git subdirectory under the current directory which contains the git data files and is used for tracking revisions to the project.

The git clone command copies an existing Git repository.

$ git clone <repository>

Usage

user@1&1 ~/training
$ git clone https://github.com/klaussilveira/gitlist.git
Cloning into 'gitlist'...
remote: Counting objects: 3313, done.
remote: Total 3313 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3313/3313), 1.76 MiB | 2.61 MiB/s, done.
Resolving deltas: 100% (1463/1463), done.
Checking connectivity... done.

Clones the repository specified under the current directory. This copy is a working directory of the main branch of the project with entire history of the cloned repository.

Staging and Committing Changes

Add

The git add command adds a change in the working directory to the index or staging area. The index separates the working directory from the Git repository.

$ git add <file>

Usage

#create a simple readme file
user@1&1 ~/training/HelloWorld (master)
$ echo "Simple readme file" >> README.md

# stage files or directories and prepare them for next commit
user@1&1 ~/training/HelloWorld (master)
$ git add README.md.

Stage

After you have staged your changes, you can use the git status command to preview the changes and display status of the working directory.

$ git status

Usage

# shows differences between the working tree and the index.
user@1&1 ~/training/HelloWorld (master)
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached<file>..." to unstage)

   new file: README.md

Commit

The git commit command is used to commit stages changes.

$ git commit –m “message”

Usage

user@1&1 ~/training/HelloWorld (master)
$ git commit -m "Added simple readme file"
[master (root-commit) accf639] Added simple readme file
1 file changed, 1 insertion(+)
create mode 100644 README.md

user@1&1 ~/training/HelloWorld (master)
$ git status
On branch master
nothing to commit, working directory clean

Creates a new commit with the current contents of the index along with a log message from the user describing the changes.

Viewing Commit History

Log

The git log command is used to list the entire commit history.

$ git log

Usage

user@1&1 ~/training/HelloWorld (master)
$ git log
commit accf6399df2f10aba7b9a32b4b51e8df664256ce
Author: username <user@example.com>
Date: Thu Jan 1 17:28:10 2015 -0500

Added simple readme file

Displays the commit logs using default formatting. The command takes options to display a shorter version of the history.

Diff

The git diff command is used to show differences between commits, commit and working tree.

$ git diff

Usage

#add some more text to readme
user@1&1 ~/training/HelloWorld (master)
$ echo "Add second line to readme file" >> README.md

#shows differences between the working directory and index
user@1&1 ~/training/HelloWorld (master)
$ git diff
diff --git a/README.md b/README.md
index 54a3fec..a47991d 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
Simple readme file
+Add second line to readme file

#shows differences between index and the most recent commit
user@1&1 ~/training/HelloWorld(master)
$ git diff --cached
#shows differences between the working directory and the most recent commit
user@1&1 ~/training/HelloWorld (master)
$ git diff HEAD
diff --git a/README.md b/README.md
index 54a3fec..a47991d 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
Simple readme file
+Add second line to readme file

Branching, Merging, and Tagging

Branching

The git branch command is used to list and create new branches.

$ git branch

Usage

# lists all the branches in your current repository
user@1&1 ~/training/HelloWorld (master)
$ git branch
* master

# creates a new branch development based on the last commit
user@1&1 ~/training/HelloWorld (master)
$ git branch development

# move the HEAD and resets the index and working directory to development branch
user@1&1 ~/training/HelloWorld (master)
$ git checkout development

Switched to branch 'development'

Merging

The git merge command allows you to merge two or more development histories together.

$ git merge <branch>

Usage

user@1&1 ~/training/HelloWorld (development)
$ echo "Add third line to readme file" >> README.md

user@1&1 ~/training/HelloWorld (development)
$ git add README.md

user@1&1 ~/training/HelloWorld (development)
$ git commit -m "New line from development"
d15d59e] New line from development
1 file changed, 1 insertion(+)

user@1&1 ~/training/HelloWorld (development)
$ git checkout master
Switched to branch 'master'

# display readme contents before merge
user@1&1 ~/training/HelloWorld (master)
$ cat README.md
Simple readme file
Add second line to readme file

# merges development branch into the master using the default fast-forward strategy.
user@1&1 ~/training/HelloWorld (master)
$ git merge development
Updating c26f8e6..d15d59e
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)

# display readme contents after merge
user@1&1 ~/training/HelloWorld(master)
$ cat README.md
Simple readme file
Add second line to readme file
Add third line to readme file

Tagging

The git tag command is used to list and create new tags.

$ git tag

Usage

# creates a new tag with the specified name based on the last commit
user@1&1 ~/training/HelloWorld (master)
$ git tag v1.0

# creates and assigns a tag to the specified commit
user@1&1 ~/training/HelloWorld (master)
$ git tag v1.5 c26f8e6e703d2b7dac903e3ef387c519261284f5

# lists all tags in your current repository
user@1&1 ~/training/HelloWorld (master)
$ git tag
v1.0
v1.5

What is GitList?

GitList is an elegant and modern Git repository viewer to browse your Git repositories over a Web interface. It allows you to browse repositories using your favorite browser, viewing files under different revisions, commit history and diffs. GitList is free and open-source software, written in PHP, on top of Silex and the Twig template engine. For more information, visit http://gitlist.org/.

Installation

Pre-requisites: Apache with mod_rewrite or Nginx, PHP 5.3+ and Git are pre-requisites before continuing the installation process of GitList.

In this tutorial, we will cover GitList installation on Windows (using WAMP) but GitList can be installed and configured on any platform. Please ensure mod_rewrite module is enabled in WAMP as shown in screenshot below.

Image 1

Installation

Step-1: Clone or download GitList directly from the GitHub website

$ git clone https://github.com/klaussilveira/gitlist.git

Step-2: Open a command prompt window and cd into the “GitList” directory

C:\Users\user>cd training\gitlist
C:\Users\user\training\gitlist>

Step-3: Download and install Composer (dependency management tool for PHP) by running the following command

C:\Users\user\training\gitlist>curl-s http://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...
Composer successfully installed to: C:\Users\user\training\gitlist\composer.phar
Use it: php composer.phar

Step-4: GitList includes a composer.json file that lists all libraries required by GitList. Execute the following command for Composer to fetch and install those libraries

C:\Users\user\training\gitlist>php composer.phar install

Step-5: Next, copy the GitList folder to the Web-server’s public root directory

C:\Users\user\training>cp -r gitlist C:/wamp/www

Step-6: To configure GitList, first rename the config.ini-example file to config.ini. Then, comment out the Linux section, un-comment the Windows section and update the properties with proper values. For Windows environment, your config.ini file should look something like this -

[git]
;client = '/usr/bin/git' ; Your git executable path
;default_branch = 'master' ; Default branch when HEAD is detached
;repositories[] = '/home/git/repositories/' ; Path to your repositories
     ; If you wish to add more repositories, just add a new line

; WINDOWS USERS
client = '"C:\Program Files (x86)\Git\bin\git.exe"' ; Your git executable path
repositories[] = 'C:\Users\user\training' ; Path to your repositories

; You can hide repositories from GitList, just copy this for each repository you want to hide
; hidden[] = '/home/git/repositories/BetaTest'

Step-7: Finally, launch your browser and navigate to http://localhost/gitlist. You should see all your repositories as shown in screenshot below:

Image 2

Conclusion

In this article, we learned how to install Git, setup a repository, basic commands to get started, branching, merging, tagging and installation of GitList. Git is a flexible, efficient and powerful distributed version control system that allows you to work on complex workflows and become more productive. There are many other functionalities available within Git for you to explore.

Delivering a positive user experience is essential for achieving success online. It is important to ensure that your Web hosting package can accommodate the performance demands of your Web project. Choosing a solution that exceeds your day-to-day requirements can ensure the availability and performance of your website. 1&1’s Web Hosting provides a convenient and powerful environment for even the most resource-intensive Web projects.

For more tips and advice on how to achieve online success, for both beginners and advanced users, visit the 1&1 Community or the 1&1 Blog.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)