-
1. Getting Started
- 1.1 About Version Control
- 1.2 A Short History of Git
- 1.3 Git Basics
- 1.4 The Command Line
- 1.5 Installing Git
- 1.6 First-Time Git Setup
- 1.7 Getting Help
- 1.8 Summary
-
2. Git Basics
- 2.1 Getting a Git Repository
- 2.2 Recording Changes to the Repository
- 2.3 Viewing the Commit History
- 2.4 Undoing Things
- 2.5 Working with Remotes
- 2.6 Tagging
- 2.7 Git Aliases
- 2.8 Summary
-
3. Git Branching
- 3.1 Branches in a Nutshell
- 3.2 Basic Branching and Merging
- 3.3 Branch Management
- 3.4 Branching Workflows
- 3.5 Remote Branches
- 3.6 Rebasing
- 3.7 Summary
-
4. Git on the Server
- 4.1 The Protocols
- 4.2 Getting Git on a Server
- 4.3 Generating Your SSH Public Key
- 4.4 Setting Up the Server
- 4.5 Git Daemon
- 4.6 Smart HTTP
- 4.7 GitWeb
- 4.8 GitLab
- 4.9 Third Party Hosted Options
- 4.10 Summary
-
5. Distributed Git
- 5.1 Distributed Workflows
- 5.2 Contributing to a Project
- 5.3 Maintaining a Project
- 5.4 Summary
-
6. GitHub
-
7. Git Tools
- 7.1 Revision Selection
- 7.2 Interactive Staging
- 7.3 Stashing and Cleaning
- 7.4 Signing Your Work
- 7.5 Searching
- 7.6 Rewriting History
- 7.7 Reset Demystified
- 7.8 Advanced Merging
- 7.9 Rerere
- 7.10 Debugging with Git
- 7.11 Submodules
- 7.12 Bundling
- 7.13 Replace
- 7.14 Credential Storage
- 7.15 Summary
-
8. Customizing Git
- 8.1 Git Configuration
- 8.2 Git Attributes
- 8.3 Git Hooks
- 8.4 An Example Git-Enforced Policy
- 8.5 Summary
-
9. Git and Other Systems
- 9.1 Git as a Client
- 9.2 Migrating to Git
- 9.3 Summary
-
10. Git Internals
- 10.1 Plumbing and Porcelain
- 10.2 Git Objects
- 10.3 Git References
- 10.4 Packfiles
- 10.5 The Refspec
- 10.6 Transfer Protocols
- 10.7 Maintenance and Data Recovery
- 10.8 Environment Variables
- 10.9 Summary
-
A1. Git in Other Environments
- A1.1 Graphical Interfaces
- A1.2 Git in Visual Studio
- A1.3 Git in Eclipse
- A1.4 Git in Bash
- A1.5 Git in Zsh
- A1.6 Git in Powershell
- A1.7 Summary
-
A2. Embedding Git in your Applications
- A2.1 Command-line Git
- A2.2 Libgit2
- A2.3 JGit
-
A3. Git Commands
- A3.1 Setup and Config
- A3.2 Getting and Creating Projects
- A3.3 Basic Snapshotting
- A3.4 Branching and Merging
- A3.5 Sharing and Updating Projects
- A3.6 Inspection and Comparison
- A3.7 Debugging
- A3.8 Patching
- A3.9 Email
- A3.10 External Systems
- A3.11 Administration
- A3.12 Plumbing Commands
7.4 Git Tools - Signing Your Work
Signing Your Work
Git is cryptographically secure, but it’s not foolproof. If you’re taking work from others on the internet and want to verify that commits are actually from a trusted source, Git has a few ways to sign and verify work using GPG.
GPG Introduction
First of all, if you want to sign anything you need to get GPG configured and your personal key installed.
$gpg --list-keys/Users/schacon/.gnupg/pubring.gpg---------------------------------pub 2048R/0A46826A 2014-06-04uid Scott Chacon (Git signing key) <[email protected]>sub 2048R/874529A9 2014-06-04
If you don’t have a key installed, you can generate one with gpg --gen-key.
gpg --gen-key
Once you have a private key to sign with, you can configure Git to use it for signing things by setting the user.signingkey config setting.
git config --global user.signingkey 0A46826A
Now Git will use your key by default to sign tags and commits if you want.
Signing Commits
In more recent versions of Git (v1.7.9 and above), you can now also sign individual commits.
If you’re interested in signing commits directly instead of just the tags, all you need to do is add a -S to your git commit command.
$git commit -a -S -m'signed commit'You need a passphrase to unlock the secret key foruser: "Scott Chacon (Git signing key) <[email protected]>"2048-bit RSA key, ID 0A46826A, created 2014-06-04[master 5c3386c] signed commit4 files changed, 4 insertions(+), 24 deletions(-)rewrite Rakefile (100%)create mode 100644 lib/git.rb
To see and verify these signatures, there is also a --show-signature option to git log.
$git log --show-signature -1commit 5c3386cf54bba0a33a32da706aa52bc0155503c2gpg: Signature made Wed Jun 4 19:49:17 2014 PDT using RSA key ID 0A46826Agpg: Good signature from "Scott Chacon (Git signing key) <[email protected]>"Author: Scott Chacon <[email protected]>Date: Wed Jun 4 19:49:17 2014 -0700signed commit
Additionally, you can configure git log to check any signatures it finds and list them in its output with the %G? format.
$git log --pretty="format:%h %G? %aN %s"5c3386c G Scott Chacon signed commitca82a6d N Scott Chacon changed the version number085bb3b N Scott Chacon removed unnecessary test codea11bef0 N Scott Chacon first commit
Here we can see that only the latest commit is signed and valid and the previous commits are not.
In Git 1.8.3 and later, "git merge" and "git pull" can be told to inspect and reject when merging a commit that does not carry a trusted GPG signature with the --verify-signatures command.
If you use this option when merging a branch and it contains commits that are not signed and valid, the merge will not work.
$git merge --verify-signatures non-verifyfatal: Commit ab06180 does not have a GPG signature.
If the merge contains only valid signed commits, the merge command will show you all the signatures it has checked and then move forward with the merge.
$git merge --verify-signatures signed-branchCommit 13ad65e has a good GPG signature by Scott Chacon (Git signing key) <[email protected]>Updating 5c3386c..13ad65eFast-forwardREADME | 2 ++1 file changed, 2 insertions(+)
You can also use the -S option with the git merge command itself to sign the resulting merge commit itself.
The following example both verifies that every commit in the branch to be merged is signed and furthermore signs the resulting merge commit.
$git merge --verify-signatures -S signed-branchCommit 13ad65e has a good GPG signature by Scott Chacon (Git signing key) <[email protected]>You need a passphrase to unlock the secret key foruser: "Scott Chacon (Git signing key) <[email protected]>"2048-bit RSA key, ID 0A46826A, created 2014-06-04Merge made by the 'recursive' strategy.README | 2 ++1 file changed, 2 insertions(+)
Everyone Must Sign
Signing tags and commits is great, but if you decide to use this in your normal workflow, you’ll have to make sure that everyone on your team understands how to do so. If you don’t, you’ll end up spending a lot of time helping people figure out how to rewrite their commits with signed versions. Make sure you understand GPG and the benefits of signing things before adopting this as part of your standard workflow.