Getting Started with Git: Installation and Setup

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/.

git-features-overview-en

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.

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

Conclusion

In this article, we learned how to install Git, setup a repository, basic commands to get started, branching, merging, and tagging.  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 Blog.

Please rate this post :

Leave a Reply

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