Book Image

Git Essentials - Second Edition

By : Ferdinando Santacroce
Book Image

Git Essentials - Second Edition

By: Ferdinando Santacroce

Overview of this book

Since its inception, Git has attracted skilled developers due to its robust, powerful, and reliable features. Its incredibly fast branching ability transformed a piece of code from a niche tool for Linux Kernel developers into a mainstream distributed versioning system. Like most powerful tools, Git can be hard to approach since it has a lot of commands, subcommands, and options that easily confuse newcomers. The 2nd edition of this very successful book will help you overcome this fear and become adept in all the basic tasks in Git. Building upon the success of the first book, we start with a brief step-by-step installation guide; after this, you'll delve into the essentials of Git. For those of you who have bought the first edition, this time we go into internals in far greater depth, talking less about theory and using much more practical examples. The book serves as a primer for topics to follow, such as branching and merging, creating and managing a GitHub personal repository, and fork and pull requests. You’ll then learn the art of cherry-picking, taking only the commits you want, followed by Git blame. Finally, we'll see how to interoperate with a Subversion server, covering the concepts and commands needed to convert an SVN repository into a Git repository. To conclude, this is a collection of resources, links, and appendices to satisfy even the most curious.
Table of Contents (8 chapters)

Installing Git

Git is open source software. You can download it for free from http://git-scm.com, where you will find a package for all the most common environments (GNU-Linux, macOS and Windows). At the time of writing this book, the latest version of Git is 2.11.0.

Installing Git on GNU-Linux

If you are a Linux user, you may have Git out of the box.
If not, you can use the distribution package manager to download and install it; an apt-get install git command or equivalent will provide you Git and all the necessary dependencies in seconds, as shown in the following image:

Installing Git on Kubuntu

Installing Git on macOS

There are several ways to install Git on macOS. The easiest way is to install the Xcode command line tools. Since Mavericks (10.9), you can do this simply by trying to run git from the terminal for the very first time. If you don't have it installed already, it will prompt you to install it, as shown in the following image:

Clicking on the Install button will fire the installation process.

If you want a more up-to-date version, you can also install it via the *.dmg binary installer, downloaded from git-scm.com (it says mavericks in the file name, but just ignore that). Beware the macOS policies while installing packages downloaded from the Internet; to allow execution, you need to hold down CTRL and click on the package icon to open it:

Hold down CTRL and click to let macOS prompt you to open the package

After this, the installation will be very easy-it's just a matter of clicking on the Continue button and following the steps represented in the following screenshot:

Let's start the installation process

Click on the Continue button and then go on; a window like the one shown in the following image will appear:

Here you can change the installation location, if you need; if in doubt, simply click Install

Click now on the Install button to start the installation. After a few seconds, the installation will be completed:

Installation complete

Installing Git on Windows

While clicking on the Download button on http://git-scm.com, you will automatically download Git in either the x86 or the x64 variant. I won't go into too much detail about the installation process itself, as it is trivial; I will only provide a few recommendations in the following screenshots:

Enabling Windows Explorer integration is generally useful; you will benefit from a convenient way to open a Git prompt in any folder by right-clicking on the contextual menu.

You should also enable Git to be used in the classic DOS command prompt, as shown in the following screenshot:

Git is provided with an embedded, Windows-compatible version of the famous Bash shell (see https://en.wikipedia.org/wiki/Bash_(Unix_shell)), which we will use extensively. By doing this, we will also make Git available to third-party applications, such as GUIs and so on. It will come in handy when we give some GUI tools a try.

Use defaults for line endings. This will protect you from future annoyances while working on multiplatform repositories:

Now it's time to choose a terminal emulator for Git; I recommend using MinTTY (see https://mintty.github.io), as it is a very good shell, fully customizable and user friendly:

Now, let's look at some new stuff from the latest Git releases-that is file system caching, the Git Credential Manager, and symbolic links:

File system caching has been considered experimental until Git for Windows v2.7.4 (March 18, 2016), but now it is stable, and since Git 2.8 this feature is enabled by default. This is a Windows-only configuration option, and allows Git to be quicker when dealing with the underlying read/write operations. I recommend that you enable it for optimal performance.

Git Credential Manager (see https://github.com/Microsoft/Git-Credential-Manager-for-Windows) is included in the Git for Windows installer since v2.7.2 (February 23, 2016). Thanks to Microsoft, today, you can deal with Git users and passwords as easily as you can on other platforms. It requires .NET framework v4.5 or later, and perfectly integrates even with Visual Studio (see https://www.visualstudio.com/) and the GitHub for Windows (see https://desktop.github.com) GUI. I recommend that you enable it, as it saves some time while working.

Symbolic links is a feature that Windows has lacked from the beginning, and even when they were introduced in Windows Vista, they have highlighted many incompatibilities with with Unix-like symlinks.

Anyway, Git and its Windows subsystem can handle them (with some limitations), so, if needed, you can try to install this feature and enable it in configuration options (they are disabled by default). For now, however, the best thing is to not use them at all in your repository if you need to work on the Windows platform. You can find more info at https://github.com/git-for-windows/git/wiki/Symbolic-Links.

Git for Windows v2.10.2 (November 2 2016) introduced a new, built-in difftool that promises quicker diffs. I use it on a daily basis, and I find it quite stable and fast. Enable it if you want give it a try, but this is not mandatory for the purposes of this book.

Git for Windows will install it in the default Program Files folder, as all the Windows programs usually do.

At the end of the process, we will have Git installed, and all its *nix friends will be ready to use it.

Please keep an eye on the Release Notes to see what's new in the latest release.