Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Git Version Control Cookbook
Git Version Control Cookbook

Git Version Control Cookbook: 90 hands-on recipes that will increase your productivity when using Git as a version control system

Arrow left icon
Profile Icon Olsson Profile Icon Voss
Arrow right icon
zł59.99 zł158.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (3 Ratings)
eBook Jul 2014 340 pages 1st Edition
eBook
zł59.99 zł158.99
Paperback
zł197.99
Subscription
Free Trial
Arrow left icon
Profile Icon Olsson Profile Icon Voss
Arrow right icon
zł59.99 zł158.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (3 Ratings)
eBook Jul 2014 340 pages 1st Edition
eBook
zł59.99 zł158.99
Paperback
zł197.99
Subscription
Free Trial
eBook
zł59.99 zł158.99
Paperback
zł197.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Git Version Control Cookbook

Chapter 2. Configuration

In this chapter, we will cover the following topics:

  • Configuration targets
  • Querying the existing configuration
  • Templates
  • A .git directory template
  • A few configuration examples
  • Git aliases
  • The refspec exemplified

Configuration targets

In this section, we will look at the different layers that can be configured. The layers are:

  • SYSTEM: This layer is system-wide and found in /etc/gitconfig
  • GLOBAL: This layer is global for the user and found in ~/.gitconfig
  • LOCAL: This layer is local to the current repository and found in .git/config

Getting ready

We will use the jgit repository for this example; clone it or use the clone you already have from Chapter 1, Navigating Git, as shown in the following command:

$ git clone https://git.eclipse.org/r/jgit/jgit
$ cd jgit

How to do it...

In the previous example, we saw how we could use the command git config --list to list configuration entries. This list is actually made from three different levels of configuration that Git offers: system-wide configuration, SYSTEM; global configuration for the user, GLOBAL; and local repository configuration, LOCAL.

For each of these configuration layers, we can query the existing configuration. On a Windows box with a default installation...

Querying the existing configuration

In this example, we will look at how we can query the existing configuration and set the configuration values.

Getting ready

We'll use jgit again by using the following command:

$ cd jgit

How to do it...

To view all the effective configurations for the current Git repository, run the following command:

$ git config --list
user.name=Aske Olsson
user.email=askeolsson@switch-gears.dk
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://git.eclipse.org/r/jgit/jgit
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

The previous output will of course reflect the user running the command. Instead of Aske Olsson as the name and the e-mail, the output should reflect your settings.

If we are just interested in a single configuration item, we can just query it by its section.key or section.subsection.key:

$ git config user.name...

Templates

In this example, we will see how to create a template commit message that will be displayed in the editor when creating a commit. The template is only for the local user and not distributed with the repository in general.

Getting ready

In this example, we will use the example repository from Chapter 1, Navigating Git:

$ git clone https://github.com/dvaske/data-model.git
$ cd data-model

We'll use the following code as a commit message template for commit messages:

Short description of commit

Longer explanation of the motivation for the change

Fixes-Bug: Enter bug-id or delete line
Implements-Requirement: Enter requirement-id or delete line

Save the commit message template in $HOME/.gitcommitmsg.txt. The filename isn't fixed and you can choose a filename of your liking.

How to do it...

To let Git know about our new commit message template, we can set the configuration variable commit.template to point at the file we just created with that template; we'll do it globally...

A .git directory template

Sometimes, having a global configuration isn't enough. You will also need to trigger the execution of scripts (also known as Git hooks), exclude files, and so on. It is possible to achieve this with the template option set to git init. It can be given as a command-line option to git clone and git init, or as the $GIT_TEMPLATE_DIR environment variable, or as the configuration option init.templatedir. It defaults to /usr/share/git-core/templates. The template option works by copying files in the template directory to the .git ($GIT_DIR) folder after it has been created. The default directory contains sample hooks and some suggested exclude patterns. In the following example, we'll see how we can set up a new template directory, and add a commit message hook and exclude file.

Getting ready

First, we will create the template directory. We can use any name we want, and we'll use ~/.git_template, as shown in the following command:

$ mkdir ~/.git_template...

A few configuration examples

There are configuration targets in the core Git system. In this section, we'll take a closer look at a few of them that might be useful in your daily work.

We'll look at the following three different configuration areas:

  • Rebase and merge setup
  • Expiry of objects
  • Autocorrect

Getting ready

In this exercise, we'll just set a few configurations. We'll use the data model repository from Chapter 1, Navigating Git:

$ cd data-model

How to do it...

Let's take a closer look at the previously mentioned configuration areas.

Rebase and merge setup

By default, when performing git pull, a merge commit will be created if the history of the local branch has diverged from the remote one. However, to avoid all these merge commits, a repository can be configured so it will default to rebase instead of merging when doing git pull. Several configuration targets related to the option exist as follows:

  • pull.rebase: This configuration, when set to true, will pull to rebase...

Configuration targets


In this section, we will look at the different layers that can be configured. The layers are:

  • SYSTEM: This layer is system-wide and found in /etc/gitconfig

  • GLOBAL: This layer is global for the user and found in ~/.gitconfig

  • LOCAL: This layer is local to the current repository and found in .git/config

Getting ready

We will use the jgit repository for this example; clone it or use the clone you already have from Chapter 1, Navigating Git, as shown in the following command:

$ git clone https://git.eclipse.org/r/jgit/jgit
$ cd jgit

How to do it...

In the previous example, we saw how we could use the command git config --list to list configuration entries. This list is actually made from three different levels of configuration that Git offers: system-wide configuration, SYSTEM; global configuration for the user, GLOBAL; and local repository configuration, LOCAL.

For each of these configuration layers, we can query the existing configuration. On a Windows box with a default...

Querying the existing configuration


In this example, we will look at how we can query the existing configuration and set the configuration values.

Getting ready

We'll use jgit again by using the following command:

$ cd jgit

How to do it...

To view all the effective configurations for the current Git repository, run the following command:

$ git config --list
user.name=Aske Olsson
user.email=askeolsson@switch-gears.dk
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://git.eclipse.org/r/jgit/jgit
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

The previous output will of course reflect the user running the command. Instead of Aske Olsson as the name and the e-mail, the output should reflect your settings.

If we are just interested in a single configuration item, we can just query it by its section.key or section.subsection.key:

$ git config user.name
Aske...
Left arrow icon Right arrow icon

Description

This practical guide contains a wide variety of recipes, taking you through all the topics you need to know about to fully utilize the most advanced features of the Git system. If you are a software developer or a build and release engineer who uses Git in your daily work and want to take your Git knowledge to the next level, then this book is for you. To understand and follow the recipes included in this book, basic knowledge of Git command-line code is mandatory.

What you will learn

  • Understand the Git data model and how you can navigate the database with simple commands
  • Learn how you can recover lost commits/files
  • Discover how you can force rebase on some branches and use regular Git merge on other branches
  • Extract metadata from a Git repository
  • Familiarize yourself with Git notes
  • Discover how you can work offline with Git
  • Debug with Git and use various techniques to find the faulty commit

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 24, 2014
Length: 340 pages
Edition : 1st
Language : English
ISBN-13 : 9781782168461
Vendor :
GitHub
Category :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Jul 24, 2014
Length: 340 pages
Edition : 1st
Language : English
ISBN-13 : 9781782168461
Vendor :
GitHub
Category :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just zł20 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just zł20 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 427.97
Git Version Control Cookbook
zł197.99
Git Best Practices Guide
zł120.99
Git Essentials
zł108.99
Total 427.97 Stars icon

Table of Contents

13 Chapters
1. Navigating Git Chevron down icon Chevron up icon
2. Configuration Chevron down icon Chevron up icon
3. Branching, Merging, and Options Chevron down icon Chevron up icon
4. Rebase Regularly and Interactively, and Other Use Cases Chevron down icon Chevron up icon
5. Storing Additional Information in Your Repository Chevron down icon Chevron up icon
6. Extracting Data from the Repository Chevron down icon Chevron up icon
7. Enhancing Your Daily Work with Git Hooks, Aliases, and Scripts Chevron down icon Chevron up icon
8. Recovering from Mistakes Chevron down icon Chevron up icon
9. Repository Maintenance Chevron down icon Chevron up icon
10. Patching and Offline Sharing Chevron down icon Chevron up icon
11. Git Plumbing and Attributes Chevron down icon Chevron up icon
12. Tips and Tricks Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(3 Ratings)
5 star 33.3%
4 star 33.3%
3 star 33.3%
2 star 0%
1 star 0%
Venkatesh Aug 27, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Having read "Git in Practice" and "Git for Teams", I was familiar with most of the content of this book. That said, I did learn some new bits in this book, e.g., git scripts. However, there two issues with this book. First, the way the code is typeset in book was a big obstacle to learn from the book. I hope PacktPub will improve their code typesetting schemes -- do not use the same font/color/styling for both commands and their outputs. Second, the exposition seemed hard to follow. Again, this is more an artifact of the format of Cookbooks from PacktPub. I'd rather they used a cookbook format (i.e., problem, solution, discussion) used by Oreilly and Manning publishers.
Amazon Verified review Amazon
Jascha Casadio Jan 05, 2016
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Git is one of those technologies that has been there since like forever and, for a developer, it is one of the the best things invented since sliced bread. Among the most widely used version control systems, it stands out for being distributed and for how easy it makes it to create, merge and destroy branches. The spotlight it deserves since years resulted in many blog posts introducing the technology, as well as in more advanced ones covering how to best use it depending on the size and distribution of a team. On top of this, it also resulted in many titles made available to us at any decent book store. While there are indeed tons of titles to choose from, only a very limited number of them are really outstanding and deserve the title of must have. Git Version Control Cookbook is the first book that tackles the subject with the winning problem-solution approach, and is thus a good candidate to be part of that short list.Before getting into the details of the book, which, spoiler, deserves some praise, a quick note: as the title itself suggests, it's a cookbook, not an introductory text. As such, it does not teach the reader what is Git and how to clone a remote repository. The reader is expected to have a good knowledge of Git and know by heart how to clone, branch, merge, fast-forward and tag, among other things.Spanning through some twelve chapters, this co-authored book is one of those that you won't finish in an afternoon. Not unless you simply walk quickly through the pages. This book, as typical of a cookbook, is best used when sitting in front of a terminal, with a coffee cup next to the keyboard and enough time to try out the examples, writing down precious notes. As a cookbook, it delivers. The authors follow the consolidated problem-solution approach and cover different subjects, ranging from the global configuration up to patching, passing through edgy topics such as rebasing. Each recipe follows a specific pattern: the problem is introduced; the solution is presented and then explained. Finally, each recipe ends with a paragraph where the authors extend the solution adding more flavors, redirecting the reader to either online resources or the man pages.Technically the book is well written, easy to follow (as long as the concepts are already known). Proofreaders did their job. While not all the recipes will be interesting to everyone, anyone, independently of his skills, will walk away learning something new. Among the concepts that I have particularly enjoyed is pruning. Very clear and exhaustive.Despite the many good things about this book, and the fact that overall is a good pick, there are a couple of things that I did not like: first, I think way too much time is dedicated to the configuration, which is something very basic; similarly, more often than not, the same recipe is presented twice, one solved with the terminal and one with a GUI, which is instead something that I would have added to that extra paragraph at the end of each recipe. Some recipe, moreover, felt way too edge case to happen in real life.Before tying it all up, a final note: this title, just like most of the other books covering Git out there, lacks something: presenting different real life scenarios where, based on the project, team and distribution, we are presented with guidelines and best strategies to model branches and deliveries. But maybe this would deserve a book on its own.A very good book, no doubts. While not outstanding, it definitely serves well anyone working with Git.As usual, you can find more reviews on my personal blog: http://books.lostinmalloc.com. Feel free to pass by and share your thoughts!
Amazon Verified review Amazon
Kevin P Sep 22, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I thought I was great with Git, until I read this book. It made me realize I have a lot to learn and encouraged me to do so. If you already know Git and want to push yourself to the next level, I can definitely recommend this book. I already applied some of the techniques described in the book at my company to improve our work flow and all the developers love it.It's not the book you should get if you're new to Git of version control systems. There are no deep explanations to the techniques and the authors seem to assume you've worked with the command line and Git before. So if that's you, and you want to get better, then this is the book for you.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.