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

eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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
Estimated delivery fee Deliver to Italy

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

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 : 9781782168454
Vendor :
GitHub
Category :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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
Estimated delivery fee Deliver to Italy

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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 €5 each
Feature tick icon Exclusive print discounts
€264.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 €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 80.97
Git Version Control Cookbook
€36.99
Git Best Practices Guide
€22.99
Git Essentials
€20.99
Total 80.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

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela