Git Recipes - Wlodzimierz Gajda - www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index Please use

Git Recipes - Wlodzimierz Gajda - www.it-ebooks.info For...

This preview shows page 1 out of 411 pages.

Unformatted text preview: For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Contents at a Glance About the Author������������������������������������������������������������������������������������������������������������ xxvii About the Technical Reviewer����������������������������������������������������������������������������������������� xxix Acknowledgments����������������������������������������������������������������������������������������������������������� xxxi ■■Chapter 1: Getting Started with Git�����������������������������������������������������������������������������������1 ■■Chapter 2: Working with Well-Known Repositories�����������������������������������������������������������7 ■■Chapter 3: Creating Local Repositories with Linear History��������������������������������������������41 ■■Chapter 4: Managing Files�����������������������������������������������������������������������������������������������79 ■■Chapter 5: Branches������������������������������������������������������������������������������������������������������105 ■■Chapter 6: Merging Branches����������������������������������������������������������������������������������������147 ■■Chapter 7: Rebasing Branches��������������������������������������������������������������������������������������163 ■■Chapter 8: Modifying the History�����������������������������������������������������������������������������������183 ■■Chapter 9: Resolving Conflicts��������������������������������������������������������������������������������������209 ■■Chapter 10: Remote Repositories and Synchronization������������������������������������������������227 ■■Chapter 11: Hosting git Git Repositories����������������������������������������������������������������������279 ■■Chapter 12: Working with Github.com��������������������������������������������������������������������������327 ■■Chapter 13: More Recipes���������������������������������������������������������������������������������������������355 Index���������������������������������������������������������������������������������������������������������������������������������381 v Chapter 1 Getting Started with Git The manufacturers of computer software are facing difficult challenges caused by quite trivial reasons. A typical application is produced by a team of developers working on hundreds, if not thousands, of files on a short schedule. Each file needs to be available for modification by all of the developers at any moment. The situation is complicated even more when we supplement the scenario with the time line. Every file can be modified by any developer at any chosen moment. The following three simple factors make the management of a source code a nontrivial task: • The number of files • The number of developers • The time line These problems have been known for many years, and as you might expect, there are various software tools that make group work on text files a lot of easier. These tools are commonly referred to as version control software or revision control software. And git belongs to this family. What is git? Git is a distributed version control system created to support the development of a Linux kernel. It was started in April 2005 by Linus Torvalds and is now maintained by Junio C. Hamano. The main features that set git apart among other version control systems are: • Branching • Data integrity • Locality • Distributed system • Open source • Last but not least—popularity The branching model is git's most amazing feature. I consider it alone to be a sufficient reason to switch to git. With git, branches can be created almost instantaneously and can be easily merged and shared with other developers. Although there are many sophisticated operations that can be performed on branches, the basic usage is easy and straightforward. This encourages the extensive usage of branches, and I think I am not exaggerating when I say that the git branching model changed the way developers work. Data integrity means that git tracks all the files and directories of your project in such a way that it is not possible to introduce unnoticed changes. Even if you want to change a single byte you have to create a revision. When you create a revision, there is no way to hide something inside. This is a built-in feature that cannot be turned off. Therefore you can trust git completely: all the changes are introduced as revisions and every revision can be inspected. 1 Chapter 1 ■ Getting Started with Git Locality increases git’s efficiency and allows you to execute many git commands even if the network is down. When you work with git you are not connected to any server. Most commands, such as commit, branch, merge, and rebase are performed locally in a similar way to typical filesystem commads such as mkdir, ls, rm. They don’t carry out any data transfer. Because git is a distributed version control system, every repository is fully functional and can serve both as a sender and a receiver. If there is a channel of communication between the computers, their repositories can exchange the contents in both directions. Therefore, you can create more complicated workflows than just the client/server paradigm that are used by centralized revision control systems. Added to this is the fact that git is an open-source project and it is becoming the most popular version control system on the world—you’ll see that there is good reason to start learning git. 1-1. Installing git on Windows Problem You want to install git on Windows. Solution Go to and download the most recent installer version of git for Windows. At the time of writing this was version 1.8.3. The installer was named: Git-1.8.3-preview20130601.exe. Run the installer leaving all options set to default values. After this, git is ready to be run on your system. How It Works There are two methods for installing git on Windows: • Use Cygwin package available at • Use the standalone installer called msysgit. In this procedure we use msysgit package. When you run the msysgit installer downloaded from you will be asked two questions: • How to configure the paths? • How to configure the conversion of end of line character? The dialog box titled Adjusting your PATH environment sets the path environment variable. Msysgit installer contains not only git binaries but also a number of Linux programs such as ssh, curl, ls, mkdir, rm, and find. With default settings the installer copies git and these programs to the following directories:   C:\Program Files (x86)\Git\bin   This folder contains ls, mkdir, ssh, curl, find, etc.   C:\Program Files (x86)\Git\cmd   This folder contains the git binary file and the shell script to run git 2 Chapter 1 ■ Getting Started with Git The first choice in the dialog box is Use Git Bash only. With this setting the path variable is not modified. When you start a Windows command line and type git, the command line will respond with a message that the git command does not exist. The second choice, Run Git from the Windows Command Prompt, adds the C:\Program Files (x86)\Git\cmd folder to your path. Thus, you can use git command in the windows command line. When you type ssh in the windows command line, however, the system will respond with an unknown command message. The last choice is Run Git and included Unix tools from the Windows Command Prompt. This choice adds two folders C:\Program Files (x86)\Git\bin and C:\Program Files (x86)\Git\cmd to your path. Now, you can use all the included tools in the Windows Command line: ssh, git, ls, curl, and so on. But some commands in C:\Program Files (x86)\Git\bin, such as find, overlap with the original commands available in Windows. The original find is not available now in the command line. When I was writing this book my intention was to present the commands that can work in exactly the same way on all platforms. Thus I decided to use the bash command line. If you work on Windows and want to use bash command line, then you can leave the default first choice Use Git Bash only. The second dialog box, which is titled Configuring the line ending conversions, sets the configuration option named core.autocrlf to one of these values: true, input, or false. The meaning of this setting is summarized in Table 1-1. Table 1-1.  All values of the core.autocrlf option and their influence on the checkout and commit operations Value Checkout Commit True LF => CRLF CRLF => LF input None CRLF => LF false None None When you choose the first setting, the value true, git will convert the end-of-line characters in your files during the checkout and commit operations. When you check the files out, git will convert LF to CRLF and when you commit git will convert CRLF to LF. The second choice, input, turns the conversion of new lines only when you commit. In this case git converts the line endings from CRLF to LF. The third setting (false) turns all the conversions off. The conversion of end-of-line characters is explained in greater detail in Recipes 13-2 through 13-6. No matter which is your current choice, you can always change the setting using one of these commands:   $ git config --global core.autocrlf true $ git config --global core.autocrlf input $ git config --global core.autocrlf false   When the installer finishes, run the git bash application available in the Start menu. To verify that the installation was successful, you can run the command:   $ git --version   It should print the version of git installed on your system. ■■Hint If you want to change the current directory to the root directory of drive c use the following command: $ cd /c This is the equivalent of the command: c: 3 Chapter 1 ■ Getting Started with Git 1-2. Installing git on Linux Problem You want to install git on Linux. Solution Depending on your system run one of the commands:   # for Ubuntu $ sudo apt-get install git   # for Fedora $ sudo yum install git  How It Works The easiest way to install git on Linux is to use the available packages. If you want to compile and install git using its source follow the procedure described in Recipe 11-3. To verify that the installation was successful, you can run the command:   $ git --version   It should print the version of git installed on your system. 1-3. Installing git on OS X Problem You want to install git on OS X. Solution Visit the site and download the most recent available version of git. Run the downloaded installer leaving all options set to the default values. How It Works The easiest way of installing git on OS X is to use the graphical installer. To verify that the installation was successful, you can run the command:   $ git --version   It should print the version of git installed on your system. 4 Chapter 1 ■ Getting Started with Git 1-4. Accessing the manual Problem You want to access the git manual. Solution Run the following commands:   $ git help $ git help -a $ git help -g   $ git help commit $ git commit --help   $ git help merge $ git merge --help  How It Works Git commands are divided into two major groups: • Porcelain commands • Plumbing commands Porcelain commands are high-level commands meant for every day use. This group includes, among the others:   $ $ $ $   git git git git add commit help push The other group, called plumbing, contains low-level commands. Here are some examples:   $ git receive-pack $ git update-index $ git upload-pack   By default, the command $ git help lists only porcelain commands. If you want to the list plumbing commands as well as the porcelain commands, use -a switch $ git help -a. You can access the documentation for a specific git subcommand using the following syntax:   $ git help [COMMAND] $ git [COMMAND] --help   Here are the commands to access the documentation of the $ git commit command:   $ git help commit $ git commit --help   5 Chapter 1 ■ Getting Started with Git 1-5. Configuring git Problem You want to configure git to be ready for work. Solution Run the following command:   $ git config --global user.name   It should print the empty results. That is because right after the installation the user name is not configured. Set the user.name configuration option using the following command:   $ git config --global user.name "John Doe"   Instead of John Doe type your name and surname. Next, run the command:   $ git config --global user.email [email protected]   This command will set your email. How It Works If you want to create commits within git repository, you have to configure two settings: user.name and user.email. Otherwise, when you run the $ git commit command, git will print the warning. The strings that you use as values for user.name and user.email will be stored within every commit you create. 6 Chapter 2 Working with Well-Known Repositories We will start our tour exploring existing and quite well-known repositories. The main goal of this chapter is to get familiar with repositories—their types and structure. In this chapter, you will learn the following: • What are the most popular hosting solutions for git repositories? • How to create a local copy of a repository that is hosted on Github.com or Bitbucket.org? Once we know how to clone a repository, we can then analyze its structure. Then we will explore the working directory—the git directory and its contents. At that point we will be able to classify a repository as either bare or non-bare. Next, we will discuss the various commands that print the information about the repository, such as • The list of revisions • The list of contributors • The number of revisions • The number of contributors • The disk usage of the git directory and the working directory To make the chore of typing long git commands easier, I will define their aliases. ■■Note  I have tested all the commands presented in this chapter and in the book on two platforms: Linux and Windows. My aim was to provide one set of instructions that will work regardless of your platform. To achieve this goal, file system operations are performed with Linux commands, such as ls and rm. Moreover, the listings start with $ and paths use / as separator—suggesting that they are prepared for Linux. However, do not worry if you are using Windows. Just use the bash command interpreter distributed with git and all the commands will work fine. If you are using a Unix-like system that is different than Linux, some commands (i.e., du or echo) can use different switches than those that are presented in this book. Therefore, you will need to customize these commands. 7 Chapter 2 ■ Working with Well-Known Repositories 2-1. Cloning a repository hosted on Github Problem You want to get a local copy of a jQuery repository. Solution Start the command line and create the git-recipes/ and git-recipes/02-01/ directories:   $ cd /some/where/on/your/system $ mkdir git-recipes $ cd git-recipes $ mkdir 02-01   Change the current directory to 02-01/:   $ cd 02-01   Then execute the command as shown in Listing 2-1. Listing 2-1.  The command to clone jQuery repository $ git clone   ■■Caution  During a clone command, git copies a complete repository from its original location to your local storage system. Depending on your bandwidth and the project size, this operation can take quite some time. But don’t worry about it. The cloning is done only once, when you set up your local repository. All subsequent data transfers are very efficient because git transfers only the missing portions of data. The internals of cloning are explained in Chapter 10 (Recipe 10-1). After you run the command shown in Listing 2-1, your git-recipes/02-01/ directory will contain files and directories that are shown in Figure 2-1. Notice that jQuery is stored under the subdirectory jquery/ and not just within git-recipes/02-01/. 8 Chapter 2 ■ Working with Well-Known Repositories Figure 2-1.  The contents of git-recipes/02-01/ directory after the git clone command ■■Caution  Figure 2-1 was prepared in April 2013. As you can guess, the jQuery project goes forward all the time. Thus the contents of your git-recipes/02-01/ directory may be different. How It Works To clone jQuery you have to find the URL that points to its repository. Start your web browser, go to Google.com, and search for "jquery git repository". The results will include:     In a similar, way you can find URLs for other popular open source projects. Table 2-1 lists keywords and URLs for three other projects: Linux, git and Mozilla. Table 2-1.  How to find git repositories for other projects? Phrase searched in Google.com The URL of the repository linux git repository git git repository mozilla git repository 9 Chapter 2 ■ Working with Well-Known Repositories Once you know URL of the jQuery repository, you can start your web browser and visit:     You will see the page presented as shown in Figure 2-2. Figure 2-2.  The main page of jQuery repository Every repository stored on Github is available under two different URLs: HTTPS and SSH. You can copy them using the buttons pointed to by the arrows in Figure 2-2. The URLs for the jQuery repository are:   HTTPS: SSH: [email protected]:jquery/jquery.git   If you don’t have a Github account with SSH keys installed you cannot use SSH URL. You have to choose HTTPS URL. 10 Chapter 2 ■ Working with Well-Known Repositories ■■Hint  In chapter 13 we will create and configure a Github account. Then you will be able to use SSH URL as well; until then, you must use HTTPS URLs. In this way you can clone all repositories available on Github.com. Remember this command:   $ git clone   For it creates the subdirectory def/. And the clone is stored inside it. However, if you work on Windows and try to clone the Linux source code, for example, you will encounter problems because filename restrictions are different with different systems. We will analyze this in chapter 11. What will happen if you don’t have a Github account with SSH keys configured and use SSH URL? The SSH URL for jQuery is:   [email protected]:jquery/jquery.git   If you use it for git clone command:   $ git clone [email protected]:jquery/jquery.git   Then you would get the following error:   Permission denied (publickey)  2-2. Cloning a repository hosted on Bitbucket Problem You want to get a local copy of the Atlassian AUI repository that is stored on Bitbucket.org:     You want the clone to be saved directly under git-recipes/02-02/, without having an additional aui/ subdirectory. Solution Start the command line and create a git-recipes/02-02/ directory:   $ cd git-recipes $ mkdir 02-02   Change your current directory to 02-02/:   $ cd 02-02   Then run the command shown in Listing 2-2. Notice the last parameter—a dot. This dot represents a current directory, thus the clone will be placed directly under git-recipes/02-02/. Without the dot the cloned repository would be stored in a subdirectory git-recipes/02-02/aui/. 11 Chapter 2 ■ Working with Well-Known Repositories Listing 2-2.  The command to clone Atlassian AUI repository $ git clone .  ■■Note  The syntax for the git clone command is: $ git clone URL [directory]. If used, the optional [directory] parameter sets the target directory for the cloned repository. How It Works Start your web browser and go to . The main page of Atlassian AUI repository is shown in Figure 2-3. Figure 2-3.  The main page of Atlassian AUI repository 12 Chapter 2 ■ Working with Well-Known Repositories Bitbucket offers two URLs for every repository: HTTPS and SSH. Figure 2-3 and the following listing will give you access to both of them:   HTTPS: SSH: [email protected]:atlassian/aui.git   As with Github, SSH URL can only be used if you have a Bitbucket account with the SSH keys configured. ■■Hint  The two most popular hosted solutions for git are Github.com and Bitbucket.org. Both offer unlimited free accounts ...
View Full Document

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture