Installing Git: Chapter 2 - Version Control with Git

At the time of this writing, Git is (seemingly) not
installed by default on any GNU/Linux distribution or any other operating
system. So, before you can use Git, you must install it. The steps to
install Git depend greatly on the vendor and version of your operating
system. This chapter describes how to install Git on Linux and Microsoft
Windows and within Cygwin.

This excerpt is from Version Control with Git.
Git permits practically an infinite variety of methods for development and collaboration, but its flexibility also means that some users don't understand how to use it to best advantage. This book offers tutorials on ways to use it, as well as
friendly yet rigorous advice to help you navigate Git's many functions.

Using Linux Binary Distributions

Many Linux vendors provide pre-compiled, binary packages
to make installation of new applications, tools, and utilities easy.
Each package specifies its dependencies, and the distribution’s package
manager typically installs the prerequisites and the desired package in
one (well-orchestrated and automated) fell swoop.

Debian/Ubuntu

On most Debian and Ubuntu systems, Git is offered as a
collection of packages, where each package can be installed
independently depending on your needs. The primary Git package is
called git-core, documentation is available in
git-doc, and there are other packages to
consider, too:

git-arch, git-cvs, git-svn

If you need to transfer a project from Arch, CVS,
or Subversion to Git or vice versa, install one or more of these
packages.

git-gui, gitk, gitweb

If you prefer to browse repositories in a
graphical application or your Web browser, install these as
appropriate. git-gui is a Tcl/Tk-based
graphical user interface for Git; gitk is
another Git browser written in Tcl/Tk but focuses more on
visualizing project history. gitweb is
written in Perl and displays a Git repository in a browser
window.

git-email

This is an essential component if you want to send
Git patches through electronic mail, which is a common practice
in some projects.

git-daemon-run

To share your repository, install this package. It
creates a daemon service that allows you to share your
repositories through anonymous download requests.

Because distributions vary greatly, it’s best to search your
distribution’s package depot for a complete list of Git-related
packages. git-doc and
git-email are strongly recommended.

Warning

Debian and Ubuntu provide a package named
git, but it isn’t a part of the Git version
control system discussed in this book. git is a
completely different program called GNU Interactive Tools. Be
careful not to install the wrong package by accident!

This command installs the important Git packages by running
apt-get as the root:

Again, be mindful that, like Debian, some distributions may
split the Git release among many different packages. If your system
lacks a particular Git command, you may need to install an additional
package.

Be sure to verify that your distribution’s Git packages are
sufficiently up-to-date. After Git is installed on your system, run
git --version. If your collaborators use a more
modern version of Git, you may have to replace your distribution’s
precompiled Git packages with a build of your own. Consult your
package manager documentation to learn how to remove previously
installed packages; proceed to the next section to learn how to build
Git from source.

Obtaining a Source Release

If you prefer to download the Git code from its canonical
source or if you want the latest version of Git, visit Git’s master
repository. As of this writing, the master repository for Git sources is
http://git.kernel.org in the
pub/software/scm directory.

The version of Git described in this book is roughly 1.6.0, but
you might want to download the latest revision of the source. You can
find a list of all the available versions at http://kernel.org/pub/software/scm/git.

To begin the build, download the source code for version 1.6.0 (or
later) and unpack it:

Building and Installing

Git is similar to other pieces of open source software.
Just configure it, type make, and install it. Small
matter of software, right? Perhaps.

If your system has the proper libraries and a robust build
environment and you do not need to customize Git, building the code can
be a snap. On the other hand, if your machine lacks a compiler or a
suite of server and software development libraries, or if you’ve never
built a complex application from source, consider building Git from
scratch only as a last resort. Git is highly configurable, and building
it shouldn’t be taken lightly.

To continue the build, consult the INSTALL
file in the Git source bundle. The file lists several external
dependencies, including the zlib,
openssl, and libcurl
libraries.

Some of the requisite libraries and packages are a bit obscure or
belong to larger packages. Here are three tips for a Debian stable
distribution:

curl-config, a small tool to extract
information about the local curl install, can
be found in the libcurl3-openssl-dev
package.

The header file expat.h comes from the
libexpat1-dev package.

The msgfmt utility belongs to the
gettext package.

Because compiling from sources is considered
“development” work, the normal binary versions of installed
libraries are not sufficient. Instead, you need the
-dev versions because the development variants also
supply header files required during compilation.

If you are unable to locate some of these packages or cannot find
a necessary library on your system, the Makefile
and configuration options offer alternatives. For example, if you lack
the expat library, you can set the
NO_EXPAT option in the Makefile.
However, your build will lack some features, as noted in the
Makefile. For example, you will not be able to push
changes to a remote repository using the HTTP and HTTPS
transports.

Other Makefile configuration options support
ports to various platforms and distributions. For instance, several
flags pertain to Mac OS X’s Darwin operating system. Either hand-modify
and select the appropriate options or find what parameters are set
automatically in the top-level INSTALL file.

Once your system and build options are ready, the rest is easy. By
default, Git is installed in your home directory in subdirectories
~/bin/, ~/lib/, and
~/share/. In general, this default is useful only
if you’re using Git personally and don’t need to share it with other
users.

These commands build and install Git in your home
directory:

$ cd git-1.6.0
$ ./configure
$ make all
$ make install

If you want to install Git into an alternate location, such as
/usr/local/ to provide general access, add
--prefix=/usr/local to the
./configure command. To continue, run
make as a normal user, but run make
install as root:

To install the Git documentation, add the doc
and install-doc targets to the
make and make install commands,
respectively:

$ cd git-1.6.0
$ make all doc
$ sudo make install install-doc

Several more libraries are needed to do a complete build of the
documentation. As an alternative, prebuilt manpages and HTML pages are
available and can be installed separately as well; just be careful to
avoid version mismatch problems if you choose to go this route.

A build from source includes all the Git subpackages and commands,
such as git-email and gitk. There
is no need to build or install those utilities independently.

Installing Git on Windows

There are two competing Git packages for Windows: a
Cygwin-based Git and a “native” version
called msysGit.

Originally, only the Cygwin version was supported and
msysGit was experimental and unstable. But as this
book went to press, both versions work well and support an almost
identical set of features. The most important exception, as of Git
1.6.0, is that msysGit does not yet properly
support git-svn. If you need interoperability between
Git and Subversion, you must use the Cygwin version of Git. Otherwise,
the version you choose is a matter of personal preference.

If you aren’t sure which one you want, here are some rules of
thumb:

If you use Cygwin already on Windows, use Cygwin’s Git because
it interoperates better with your Cygwin setup. For example, all
your Cygwin-style filenames will work in Git, and redirecting
program input and output will always work exactly as
expected.

If you don’t use Cygwin, it’s easier to install
msysGit because it has its own standalone
installer.

If you want Git integration with the Windows Explorer shell
(for example, the ability to right-click on a folder and pick
“Git GUI Here” or “Git Bash Here”),
install msysGit. If you want this feature but
prefer to use Cygwin, you can install both packages without
harm.

If you’re still in doubt about which package to use, install
msysGit. Make sure you get the latest version
(1.5.6.1 or higher), as the quality of Git’s Windows support steadily
improves in successive versions.

Installing the Cygwin Git Package

The Cygwin Git package, as the name implies, is a package inside the
Cygwin system itself. To install it, run Cygwin’s
setup.exe program, which you can download from
http://cygwin.com.

After setup.exe launches, use the default
settings for most options until you get to the list of packages to
install. The Git packages are in the devel
category, as shown in Figure 2.1, “Cygwin setup”.

Figure 2.1. Cygwin setup

After choosing the packages you want to install, click Next a
few more times until the Cygwin installation finishes. You can then
start the Cygwin Bash Shell from your Start menu, which should now
include the git command (Figure 2.2, “Cygwin shell”).

Figure 2.2. Cygwin shell

As an alternative, if your Cygwin configuration includes the
various compiler tools like gcc and
make, you can build your own copy of Git from
source code on Windows under Cygwin by following the same instructions
as on Linux.

Installing Standalone Git (msysGit)

The msysGit package is easy to install on a Windows system because the
package includes all its dependencies. It even has SSH commands to
generate the keys that repository maintainers require to control
access. msysGit is designed to integrate well
with the Windows-style native applications such as the Windows
Explorer shell.

First, download the latest version of the installer from its
home at http://code.google.com/p/msysgit. The
file to collect is usually called something like the following:
Git-1.5.6.1-preview20080701.exe.

Depending on the actual version being installed, you may or may
not need to click Next through a compatibility notice as shown in
Figure 2.4, “msysGit notice”. This notice concerns
incompatibilities between Windows-style and Unix-style line endings,
called CRLF and LF, respectively.

The best way to run msysGit day-to-day is
via Windows Explorer, so check the two pertinent boxes, as
shown.

In addition, an icon to start Git
Bash (a command prompt that makes the git
commands available) is installed in the Start menu in the section
called Git. Since most of the examples in this book
use the command line, use Git Bash to get
started.

All the examples in this book work equally well on Linux and
Windows, with one caveat: msysGit for Windows
uses the older Git command names mentioned in the section called “The Git Command Line”. To follow the examples with
msysGit, enter git-add for
git add.