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 a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781782168454
Vendor :
GitHub
Category :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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%
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
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
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
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.