CSC/ECE 517 Fall 2013/ch1 1w6 zs

From Expertiza_Wiki
Jump to navigation Jump to search

CSC/ECE 517 Fall 2013/ch1w6 zs

Synopsis of Git book

Introduction

Git is by far the best Version Control System on the market and it's free.

Version Control System

Version control system is a system that keeps track of changes to a set of files for a certain project. You can recall all specific versions of your file later. Actually any file on your computer is placed under version control. A VCS allows you to revert file to a previous version or even revert the whole project back to a certain version. It allows you to see what when changes are made and who modified it. It also allows you to recover yourself easily from a messed up project.

Generally, there are three kinds of version control system, which are local, centralized and distributed VCSs. Figures are illustrated below. Local Version Control

Centralized Version Control System

Distributed Version Control System

History

Git was born with a bit of creative destruction and fiery controversy. The linux kernel , which is an open-source software project, kept its changes as patches and archived files from 1991 to 2002 and since 2002. The project switched to a Distributed VCS called BitKeeper.

When things between the community that developed linux kernel and the commercial company that developed Bitkeeper went bad. The free version of the BitKeeper was revoked and this prompt the community that developed Linux kernel to develop their own tools. And that is the origin of Git.

For over 7 years since its birth in 2005, Git has matured and it’s now handy to use while keeping the good qualities. It's especially efficient for large projects and it has a amazing branching system(another link here for branching ) for non-linear development .

What is Git

Git stores ,process and thinks about information in a different way than any other VCS. Systems like CVS deals with the information they hold as a set of files and changes made to each single file over time. But Git doesn't do it this way.Git takes your file more like a series of snapshots. Every time you commit or save your file, Git takes a snapshot of all your files. And to avoid unnecessary memory consumption , those files that stay unchanged are stored as a link to its previous real version. Illustrated as below.

Git's Snapshots way of thinking data

This is a huge difference between Git and other VCSs, actually nearly all other VCSs. Git take care of very aspect of version control at a minimum cost.It's more like a set of mini file system with lots of advanced tools built in.

Most operation in Git is local and no data or information is needed on another computer. You always have an entire history of your project on your local computer. It's so efficient that most operations seems instant. That means you can browse your entire project history on you local database almost instantaneous. Git can look up a file no matter how long ago and compare it with your current version with diff calculation.When you are offline , you can easily make changes to your projects and commit the change once you are connected to the internet. Besides that, if you are off VPN, you can still work. Although it seems trivial, this will make a huge difference.

Everything you do on Git is known to Git. You won't lose lose anything or get file corruption before Git detecting it. This is done by adopting checksumming using SHA-1 hash. It's a 40 character string composed of hex characters. Git stores everything in the Git database addressable by the hash value of its contents, other than file name.

Generally Git only adds data to Git database. After committing a snapshot into Git database, it's very hard to lose data. This is especially secured if you regularly push Git database into another repo.

Important: Git has three states for a file to reside in, which are committed, staged and modified.Committed means that your file is safe and sound in Git database. Modified means that you have changed the file but it's not committed into database yet.Staged means that you mark a modified file in its current version and ready for the next snapshots. It's like cache. The three states and their connection is illustrated in the figure below.

The repo is where Git stores data for your project.When you clone a repo from another computer, this is the thing copied. The working directory is a simple check out for your current version of files.The files on working directory are pulled out of repo and put on disk for you to manipulate. The staging area is a simple file which is contained in repo. It stores information that will go into the commit stage.

Generally, the follow three steps are how Git works:

1.You modify files in your working directory.

2.You stage the files, adding snapshots of them to your staging area.

3.You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.

Getting Started on Git

Installing Git

First thing to use Git is to install it. You can either install it from sources or installing package on your platform(Win, Mac and Linux)

Install from Source

One of the advantage of installing from source is you always get the latest version. Each version of Git tend to add some UI enhancement and it will be comfy to compile on that. To start with, there are some must-have libraries that Git depends on, such as curl, zlib, openssl, expat and libiconv.If your system has yum (such as Fedora) or apt-get (such as a Debian based system), then you can use the one of the commands below to install the all the dependencies.

$ yum install curl-devel expat-devel gettext-devel \
 openssl-devel zlib-devel
$ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \
 libz-dev libssl-dev

Once this is done, you can go ahead and download the latest version of Git

 http://git-scm.com/download

And then, compile and install

$ tar -zxf git-1.7.2.2.tar.gz
$ cd git-1.7.2.2
$ make prefix=/usr/local all
$ sudo make prefix=/usr/local install

After this is done, Git is already installed on your local computer. You can use the following command to update your Git to latest version.

$ git clone git://git.kernel.org/pub/scm/git/git.git

Install on Linux

It's way much easier to install Git on Linux. If you are on Fedora, you can use the following yum

$ yum install git-core

If you are on a Debian-based distribution like Ubuntu, use apt-get

$ apt-get install git

Install on Windows

Suprisingly,it's very east to install Git on Windows. Download the Git installer exe file from Github page and run it.

http://msysgit.github.com/

After installing on windows, you will have both command-line and GUI version.It is suggested that windows users to use msysGit shell, which supports many complex commands.

First time set-up

You may want to customize your Git environment once and for all. It will persist after each upgrade and you can also make changes to your settings using the commands again.

A tool called git config is built in on Git , it allows you to get and set configs that control all things about how Git looks and runs.The config variables are stored in three different places:

1. /etc/gitconfig file: Contains values for every user on the system and all their repositories. If you pass the option --system to git config, it reads and writes from this file specifically.

2. ~/.gitconfig file: Specific to your user. You can make Git read and write to this file specifically by passing the --global option.

3. config file in the git directory (that is, .git/config) of whatever repository you’re currently using: Specific to that single repository. Each level overrides values in the previous level, so values in .git/config trump those in /etc/gitconfig.

Identity, Editor, Diff tool and Check

You should set your user name and email address once you get your Git installed. Every Git commits uses this info and it's etched in your commits.

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

Because you passed the --global option, you only need to do this once.

You can config your default editor when Git asks you to type any message. If you want to use another editor other than your system's default editor like emacs, you can use the following command. Usually it's Vi or Vim by default.

$ git config --global core.editor emacs

You will want to use diff tools for detecting and resolving emerge conflicts.There are many diff tools and let's assume you want to use vimdiff, you should type the following command

$ git config --global merge.tool vimdiff

Git also accept kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, and opendiff as valid merge tools.

If you want to check your settings, Git has provided a --list option for you to view all your settings. By typing "git config --list", you will get the following:

$ git config --list
user.name=Scott Chacon
user.email=schacon@gmail.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
...

Get help from Git

If you ever need help while using Git, there are three ways to get the manual page help for any of the Git commands:
$ git help <verb>
$ git <verb> --help
$ man git-<verb>

For example, you can get the manpage help for the config command by running

$ git help config

These commands are nice because you can access them anywhere, even offline. If the manual pages and this book aren’t enough and you need in-person help, you can try the #git or #github channel on the Freenode IRC server (irc.freenode.net). These channels are regularly filled with hundreds of people who are all very knowledgeable about Git and are often willing to help.

Basic Operation on Git

Record Changes on a Repo

To initiate a Git repository , you generally have two ways. One is using an existing project or directory and imports it into Git. The other is to clone an existing Git repo from anther server.

Initializing a Repository in an Existing Directory

If you’re starting to track an existing project in Git, you need to go to the project’s directory and type

$ git init

This creates a new subdirectory named .git that contains all of your necessary repository files, which is a Git repository skeleton. At this point, nothing in your project is tracked yet.

If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit. You can accomplish that with a few git add commands that specify the files you want to track, followed by a commit:

$ git add *.c
$ git add README
$ git commit -m 'initial project version'

At this point, you have a Git repository with tracked files and an initial commit.