Git Version Control Cookbook

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

Git Version Control Cookbook

Cookbook
Aske Olsson, Rasmus Voss

90 hands-on recipes that will increase your productivity when using Git as a version control system
$26.99
$44.99
RRP $26.99
RRP $44.99
eBook
Print + eBook
$12.99 p/month

Get Access

Get Unlimited Access to every Packt eBook and Video course

Enjoy full and instant access to over 3000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

Book Details

ISBN 139781782168454
Paperback340 pages

About This Book

  • Filled with practical recipes that will teach you how to use the most advanced features of the Git system
  • Improve your productivity by learning to work faster, more efficiently, and with more confidence
  • Discover tips and tricks that will show you when and how to use the advanced features of Git

Who This Book Is For

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.

Table of Contents

Chapter 1: Navigating Git
Introduction
Git's objects
The three stages
Viewing the DAG
Extracting fixed issues
Getting a list of the changed files
Viewing history with Gitk
Finding commits in history
Searching through history code
Chapter 2: Configuration
Configuration targets
Querying the existing configuration
Templates
A .git directory template
A few configuration examples
Git aliases
The refspec exemplified
Chapter 3: Branching, Merging, and Options
Introduction
Managing your local branches
Branches with remotes
Forcing a merge commit
Using git rerere to merge known conflicts
The difference between branches
Chapter 4: Rebase Regularly and Interactively, and Other Use Cases
Introduction
Rebasing commits to another branch
Continuing a rebase with merge conflicts
Rebasing selective commits interactively
Squashing commits using an interactive rebase
Changing the author of commits using a rebase
Auto-squashing commits
Chapter 5: Storing Additional Information in Your Repository
Introduction
Adding your first Git note
Separating notes by category
Retrieving notes from the remote repository
Pushing notes to a remote repository
Tagging commits in the repository
Chapter 6: Extracting Data from the Repository
Introduction
Extracting the top contributor
Finding bottlenecks in the source tree
Grepping the commit messages
The contents of the releases
Chapter 7: Enhancing Your Daily Work with Git Hooks, Aliases, and Scripts
Introduction
Using a branch description in the commit message
Creating a dynamic commit message template
Using external information in the commit message
Preventing the push of specific commits
Configuring and using Git aliases
Configuring and using Git scripts
Setting up and using a commit template
Chapter 8: Recovering from Mistakes
Introduction
Undo – remove a commit completely
Undo – remove a commit and retain the changes to files
Undo – remove a commit and retain the changes in the staging area
Undo – working with a dirty area
Redo – recreate the latest commit with new changes
Revert – undo the changes introduced by a commit
Reverting a merge
Viewing past Git actions with git reflog
Finding lost changes with git fsck
Chapter 9: Repository Maintenance
Introduction
Pruning remote branches
Running garbage collection manually
Turning off automatic garbage collection
Splitting a repository
Rewriting history – changing a single file
Back up your repositories as mirror repositories
A quick submodule how-to
Subtree merging
Submodule versus subtree merging
Chapter 10: Patching and Offline Sharing
Introduction
Creating patches
Creating patches from branches
Applying patches
Sending patches
Creating Git bundles
Using a Git bundle
Creating archives from a tree
Chapter 11: Git Plumbing and Attributes
Introduction
Displaying the repository information
Displaying the tree information
Displaying the file information
Writing a blob object to the database
Writing a tree object to the database
Writing a commit object to the database
Keyword expansion with attribute filters
Metadata diff of binary files
Storing binaries elsewhere
Checking the attributes of a file
Attributes to export an archive
Chapter 12: Tips and Tricks
Introduction
Using git stash
Saving and applying stashes
Debugging with git bisect
Using the blame command
Color UI in the prompt
Autocompletion
Bash prompt with status information
More aliases
Interactive add
Interactive add with Git GUI
Ignoring files
Showing and cleaning ignored files

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

In Detail

Starting with the Git data model, you will learn how Git stores files and how it looks at commits. You will then learn how you can recover from mistakes; from committing on the wrong branch to recovering lost commits/files. Next, you will discover how you can force rebase on some branches and use regular Git merge on other branches. You will also learn how to extract information from the repository.

As you progress through this book, you will learn how you can automate the usual Git processes by utilizing the hook system built into Git. The book also covers advanced repository management, including different options to rewrite the history of a Git repository. Finally, you will discover how you can work offline with Git, how to track what is going on behind the scenes, and how to use the stash for different purposes.

 

 

Read an Extract from the book

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

Now, we need to populate the directory with some template files. This could be a hook or
an exclude file. We will create one hook file and an exclude file. The hook file is located in
.git/hooks/name-of-hook and the exclude file in .git/info/exclude. Create the
two directories needed,
hooks and info, as shown in the following command:

$ mkdir ~/.git_template/{hooks,info}

To keep the sample hooks provided by the default template directory (the Git installation),
we copy the files in the default template directory to the new one. When we use our newly created template directory, we'll override the default one. So, copying the default files to our template directory will make sure that except for our specific changes, the template directory
is similar to the default one, as shown in the following command:

$ cd ~/.git_template/hooks
$ cp /usr/share/git-core/templates/hooks/* .

We'll use the commit-msg hook as the example hook:

#!/bin/sh
MSG_FILE="$1"
echo "\nHi from the template commit-msg hook" >> $MSG_FILE

The hook is very simple and will just add Hi from the template commit-msg hook to the end of the commit message. Save it as commit-msg in the ~/.git_template/hooks directory and make it executable by using the following command:

chmod +x ~/.git_template/hooks/commit-msg

Now that the commit message hook is done, let's also add an exclude file to the example. The exclude file works like the .gitignore file, but is not tracked in the repository. We'll create an exclude file that excludes all the *.txt files, as follows:

$ echo *.txt > ~/.git_template/info/exclude

Now, our template directory is ready for use.

How to do it...

Our template directory is ready and we can use it, as described earlier, as a command-line option, an environment variable or, as in this example, to be set as a configuration:

$ git config --global init.templatedir ~/.git_template

Now, all Git repositories we create using init or clone will have the default files of the template directory. We can test if it works by creating a new repository as follows:

$ git init template-example
$ cd template-example

Let's try to create a .txt file and see what git status tells us. It should be ignored by the exclude file from the template directory:

$ echo "this is the readme file" > README.txt
$ git status

The exclude file worked! You can put in the file endings yourself or just leave it blank and keep to the .gitignore files.

To test if the commit-msg hook also works, let's try to create a commit. First, we need a file to commit. So, let's create that and commit it as follows:

$ echo "something to commit" > somefile
$ git add somefile
$ git commit –m "Committed something"

We can now check the history with git log:

$ git log -1
commit 1f7d63d7e08e96dda3da63eadc17f35132d24064
Author: Aske Olsson <aske.olsson@switch-gears.dk>
Date:   Mon Jan 6 20:14:21 2014 +0100
 
    Committed something
 
    Hi from the template commit-msg hook

How it works...

When Git creates a new repository, either via init or clone, it will copy the files from the template directory to the new repository when creating the directory structure. The template directory can be defined either by a command-line argument, environment variable, or configuration option. If nothing is specified, the default template directory will be used (distributed with the Git installation). By setting the configuration as a --global option, the template directory defined will apply to all of the user's (new) repositories. This is a very nice way to distribute the same hooks across repositories, but it also has some drawbacks. As the files in the template directory are only copied to the Git repositories, updates to the template directory do not affect the existing repositories. This can be solved by running git init in each existing repository to reinitialize the repository, but this can be quite cumbersome. Also, the template directory can enforce hooks on some repositories where you don't want them. This is quite easily solved by simply deleting the hook files in .git/hooks of that repository.

 

Authors

Table of Contents

Chapter 1: Navigating Git
Introduction
Git's objects
The three stages
Viewing the DAG
Extracting fixed issues
Getting a list of the changed files
Viewing history with Gitk
Finding commits in history
Searching through history code
Chapter 2: Configuration
Configuration targets
Querying the existing configuration
Templates
A .git directory template
A few configuration examples
Git aliases
The refspec exemplified
Chapter 3: Branching, Merging, and Options
Introduction
Managing your local branches
Branches with remotes
Forcing a merge commit
Using git rerere to merge known conflicts
The difference between branches
Chapter 4: Rebase Regularly and Interactively, and Other Use Cases
Introduction
Rebasing commits to another branch
Continuing a rebase with merge conflicts
Rebasing selective commits interactively
Squashing commits using an interactive rebase
Changing the author of commits using a rebase
Auto-squashing commits
Chapter 5: Storing Additional Information in Your Repository
Introduction
Adding your first Git note
Separating notes by category
Retrieving notes from the remote repository
Pushing notes to a remote repository
Tagging commits in the repository
Chapter 6: Extracting Data from the Repository
Introduction
Extracting the top contributor
Finding bottlenecks in the source tree
Grepping the commit messages
The contents of the releases
Chapter 7: Enhancing Your Daily Work with Git Hooks, Aliases, and Scripts
Introduction
Using a branch description in the commit message
Creating a dynamic commit message template
Using external information in the commit message
Preventing the push of specific commits
Configuring and using Git aliases
Configuring and using Git scripts
Setting up and using a commit template
Chapter 8: Recovering from Mistakes
Introduction
Undo – remove a commit completely
Undo – remove a commit and retain the changes to files
Undo – remove a commit and retain the changes in the staging area
Undo – working with a dirty area
Redo – recreate the latest commit with new changes
Revert – undo the changes introduced by a commit
Reverting a merge
Viewing past Git actions with git reflog
Finding lost changes with git fsck
Chapter 9: Repository Maintenance
Introduction
Pruning remote branches
Running garbage collection manually
Turning off automatic garbage collection
Splitting a repository
Rewriting history – changing a single file
Back up your repositories as mirror repositories
A quick submodule how-to
Subtree merging
Submodule versus subtree merging
Chapter 10: Patching and Offline Sharing
Introduction
Creating patches
Creating patches from branches
Applying patches
Sending patches
Creating Git bundles
Using a Git bundle
Creating archives from a tree
Chapter 11: Git Plumbing and Attributes
Introduction
Displaying the repository information
Displaying the tree information
Displaying the file information
Writing a blob object to the database
Writing a tree object to the database
Writing a commit object to the database
Keyword expansion with attribute filters
Metadata diff of binary files
Storing binaries elsewhere
Checking the attributes of a file
Attributes to export an archive
Chapter 12: Tips and Tricks
Introduction
Using git stash
Saving and applying stashes
Debugging with git bisect
Using the blame command
Color UI in the prompt
Autocompletion
Bash prompt with status information
More aliases
Interactive add
Interactive add with Git GUI
Ignoring files
Showing and cleaning ignored files

Book Details

ISBN 139781782168454
Paperback340 pages
Read More