Collaborative Development with Git
local graph
Chapter 6 — Collaborative Development with Git
Section titled “Chapter 6 — Collaborative Development with Git”Chapter 2*, Developing with Git*, and Chapter 3*, Managing Your Worktrees*, taught you how to make new contributions to a project, but limited this information to affecting only your own clone of the project’s repository on your computer. Chapter 2 described how to commit new revisions, while Chapter 3 showed you how Git can help you prepare it.
This chapter and Chapter 7*,* Publishing Your Changes, present a bird’s-eye view of the various ways to collaborate with others, showing centralized and distributed workflows. These two chapters will focus on the repository-level interactions in collaborative development, while the patterns of branches used will be covered in Chapter 8, Advanced Branching Techniques.
This chapter describes different collaborative workflows, explaining the advantages and disadvantages of each one. You will also learn about the chain of trust concept and how to use signed tags, signed merges, and signed commits.
The following topics will be covered in this chapter:
- Centralized and distributed workflows, and bare repositories
- Managing remotes and one-off single-shot collaboration
- How versions are addressed—the chain of trust
- Tagging; lightweight tags versus signed tags
- Signed tags, signed merges, and signed commits
Collaborative workflows
Section titled “Collaborative workflows”There are various levels of engagement while using a version control system. One might, for example, only be interested in using Git to examine how the project came to be. Chapter 4, Exploring Project History, and Chapter 5, Searching Through the Repository, covered this use of Git. Of course, examining a project’s history is an important part of development, too.
One might use version control for one’s private development, using it for a single developer project, on a single machine. Chapter 2, Developing with Git, and Chapter 3, Managing Your Worktrees, show how to do this with Git. Of course, people usually don’t work in isolation, but in a team: one’s own development is usually part of a collaboration.
But one of the main goals of version control systems is to help multiple developers work together on a project, collaboratively. Version control makes it possible for them to work simultaneously on a given piece of software in an effective way, ensuring that their changes do not conflict with each other, and thus helping with merging those changes.
One might work on a project together with a few other developers, or with many. One might be a contributor, or a project maintainer. Maybe the project is so large that it needs subsystem maintainers. One might work in tight software teams, or one might want to make it easy for external contributors to provide proposed changes (for example, to fix bugs, or fix an error in the documentation). Various workflows might be best suited for those different situations:
- Centralized workflow
- Peer-to-peer workflow
- Maintainer workflow
- Hierarchical workflow
Bare repositories
Section titled “Bare repositories”There are two types of repositories:
- One with a working directory, and a staging area (non-bare)
- A bare repository, without the working directory
The former type is meant for private solo development, and creating new history, while the latter is intended for collaboration and synchronizing development results.
By convention, bare repositories use the .git extension—for example, project.git —while non-bare repositories don’t have it—for example, project (with the administrative area and the local repository in project/.git).
You can usually omit this extension when cloning the repository, pushing to it, or fetching from it; using either https://github.com/git/git.git as the repository URL or https://github.com/git/git will work the same.
To create the bare repository, you need to add the --bare option to the git init or git clone commands, as in the following example:
$ git init --bare project.gitInitialized empty Git repository in /home/user/project.git/Interacting with other repositories
Section titled “Interacting with other repositories”After creating a set of revisions and extending the project’s history, you usually need to share it with other developers. You need to synchronize with other repository instances: publish your changes, and get changes from others.
From the perspective of the local repository instance – your own clone of the repository – you need to push your changes to the repository meant for publishing changes and fetch changes from other repositories. Often the only repository you need to interact with is simply the repository you cloned from. Chapter 7, Publishing Your Changes, will describe this process (and its alternatives) in more detail.
After fetching changes, you sometimes need to incorporate them into your work by merging two lines of development (or rebasing)—which you can do in one operation with pull. Merging and rebasing operations will be described in more detail in Chapter 9, Merging Changes Together.
Git assumes that you don’t want your local repository to be visible to the public, because these repositories are intended for private work (which helps to keep work not yet ready for the public from being visible).
This means that there is an additional step required to make your finished work available: you need to publish your changes, for example with the git push command.
The diagram in Figure 6*.1*, which is an extension of the one in Figure 2*.2* in Chapter 2, Developing with Git, demonstrates the steps one can take when creating and publishing commits. The arrows in this diagram show the Git commands used to copy contents from one place to another, including to and from the remote repository.

Figure 6.1 – Creating commits, publishing commits, and fetching changes published by other developers into your local repository
Now, let us understand the centralized workflow.
The centralized workflow
Section titled “The centralized workflow”With distributed version control systems, you can use different collaboration models, some more distributed, some less distributed. In a centralized workflow, there is one central hub: a shared repository, usually bare, that everyone uses to synchronize their work.

Figure 6.2 – Centralized workflow – the shared repository is bare
In this workflow, each developer has their own non-bare clone of the central shared repository, which they use to develop new revisions of software. When changes are ready, they push those changes to the central repository, and fetch (or pull) changes from other developers from it. One might have to merge changes before being able to push. In this workflow integration of changes is distributed. This workflow is shown in Figure 6*.2*.
Let us now look into the advantages and disadvantages of a centralized workflow.
Advantages of a centralized workflow
Section titled “Advantages of a centralized workflow”Some of the key advantages of centralized workflows include the following:
- This workflow has a simple setup; it is a familiar paradigm for people coming from centralized version control systems and used to working with centralized management. It provides centralized access control and easy backups.
- It makes it easy to set up continuous integration (CI).
- The process of merging changes is shared among developers, with no person solely responsible for integrating changes.
- It might be a good setup for a private project with a small team, or where all developers are trusted and capable.
Disadvantages of a centralized workflow
Section titled “Disadvantages of a centralized workflow”Some of the disadvantages of centralized workflows are as follows:
- The shared repository is a single point of failure: if there are problems with the central repository, then there is no way to synchronize changes.
- Each developer pushing changes (making them available for other developers) might require updating one’s own repository first, and merging changes from others. Shared integration means that each developer needs to know how to do it.
- You also need to trust developers with access to the shared repository in this setup, or to provide access controls.
The peer-to-peer or forking workflow
Section titled “The peer-to-peer or forking workflow”The opposite of a centralized workflow is a peer-to-peer or forking workflow. Instead of using a single central shared public repository, each developer has a public repository (which is bare), in addition to a private working repository (with a working directory), like in the Figure 6*.3*.

Figure 6.3 – Peer-to-peer workflow – here, lines pointing up represent push operation, while lines pointing down represent fetch/pull operation
When the changes are ready, developers push to their own public repositories. To incorporate changes from other developers, one needs to fetch them from the public repositories of each of the other developers.
The advantages and disadvantages of this rarely used peer-to-peer workflow, also called forking workflow, are as follows:
Advantages of the peer-to-peer workflow
Section titled “Advantages of the peer-to-peer workflow”- One advantage of the forking workflow is that contributions can be integrated without the need for a central repository; it is a fully distributed workflow
- Another advantage is that you are not forced to integrate if you want to publish your changes; you can merge at your leisure
- It is a good workflow for organic teams without requiring much setup
Disadvantages of the peer-to-peer workflow
Section titled “Disadvantages of the peer-to-peer workflow”- The disadvantages are a lack of the canonical version, no centralized management, and the fact that in the basic form of this workflow you need to interact with many repositories. Though the
git remote updateorgit fetch --multiplecommands can help here by doing multiple fetches with a single command. - Setting up this workflow requires developers’ public repositories to be reachable from other developers’ workstations, which might not be as easy as using one’s own machine as a server for one’s public repositories
- Also, as can be seen in Figure 6*.3*, collaboration gets more complicated with the growing number of developers; this workflow does not scale well
The maintainer or integration manager workflow
Section titled “The maintainer or integration manager workflow”One of the problems with peer-to-peer workflows is that there is no canonical version of a project, something that non-developers can use. Another is that each developer has to do their own integration (which was also the case for the centralized workflow). If we promote one of the public repositories in Figure 6*.3* to be the canonical (official) repository and make one of the developers responsible for integration, we arrive at the integration manager workflow (or maintainer workflow). The following diagram shows this workflow, with bare repositories at the top and non-bare at the bottom.

Figure 6.4 – Integration-manager (maintainer) workflow – lines pointing up are push operations, while lines pointing down are fetch operations
In this workflow, when changes are ready, the developer pushes those changes to their own public repository and tells the maintainer (for example, via a pull request) that they are ready. The maintainer pulls changes from the developer’s repository into their own working repository and integrates the changes. Then the maintainer pushes the merged changes to the “blessed” repository, for all to see, making them available to be fetched.
The advantages and disadvantages are as follows:
Advantages of the integration manager workflow
Section titled “Advantages of the integration manager workflow”- The advantages are having an official version of a project, and that developers can continue to work without doing or waiting for integration, as maintainers can pull their changes at any time.
- It is a good workflow for a large organic team, as in open source projects.
- The fact that the blessed repository is decided by social consensus makes it easy to switch to other maintainers, either temporarily (for example, when one maintainer takes some time off) or permanently (such as when forking a project), without the need to hand out access rights.
- This setup makes it easy for a smaller group of developers to collaborate by simply denoting one of the repositories in the group as the one to fetch from. The dotted line in Figure 6*.4* shows this possibility of fetching from a non-official repository.
Disadvantages of the integration manager workflow
Section titled “Disadvantages of the integration manager workflow”- The primary disadvantage is that the ability of the maintainer to integrate changes can be a bottleneck (as opposed to the centralized workflow, with distributed integration). This can happen especially for large teams and large projects. Thus, for very large organic teams, such as in Linux kernel development, it is better to use the hierarchical workflow, described in the next section.
- There needs to be dedicated person that does the merging and is responsible for the state of the “ blessed ” repository.
- Another disadvantage is that it is more difficult to set up continuous integration than in the centralized repository workflow.
The hierarchical or dictator-and-lieutenants workflow
Section titled “The hierarchical or dictator-and-lieutenants workflow”The hierarchical workflow is a variant of the blessed repository workflow, generally used in huge projects with hundreds of collaborators. In this workflow, the project maintainer (sometimes called the benevolent dictator) is accompanied by additional integration managers, usually in charge of certain parts of the repository (subsystems). They are called lieutenants. The benevolent dictator’s public repository serves as the blessed reference repository from which all the collaborators need to pull. Lieutenants pull from developers and the maintainer pulls from the lieutenants, as shown in Figure 6*.5*. (Note that in the following diagram, repositories shown with dashed patterns are actually pairs of private and public repositories of a developer or a lieutenant).

Figure 6.5 – Dictator and lieutenants workflow (hierarchical workflow)
In a dictator and lieutenants workflow, there is a hierarchy (a network) of repositories. Before starting work, either development or merging, one would usually pull updates from the canonical (blessed) repository for a project.
- Developers prepare changes in their own private repository, then send changes to an appropriate subsystem maintainer (lieutenant). Changes can be sent as patches in email, or by pushing them to the developer’s public repository and sending a pull request to an appropriate integration manager (appropriate subsystem maintainer).
- Lieutenants are responsible for merging changes in their respective areas of responsibility.
- The master maintainer (dictator) pulls from the lieutenants (and occasionally directly from developers). The dictator is also responsible for pushing merged changes to the reference (canonical) repository, and usually also for release management (for example, creating tags for releases).
An overview of the advantages and disadvantages of this workflow follows.
Advantages of the hierarchical workflow
Section titled “Advantages of the hierarchical workflow”- The advantage of this workflow is that it allows the project leader (the dictator) to delegate much of the integration work.
- This can be useful in very big projects (concerning the number of developers and/or changes), or in highly hierarchical environments. Such a workflow is used, for example, to develop the Linux kernel.
Disadvantages of the hierarchical workflow
Section titled “Disadvantages of the hierarchical workflow”- Its complicated setup is a disadvantage of this workflow. It is usually overkill for an ordinary project.
- Almost all other disadvantages of the integration manager workflow are present in this workflow, which is its more complex variant.
Which workflow to choose, and how to set up repositories, depend on how the project is developed. You need to decide which drawbacks are acceptable and which advantages matter most.
Managing remote repositories
Section titled “Managing remote repositories”When collaborating on any project managed with Git, you will interact often with a constant set of other repositories. For example, using the integration-manager workflow will involve (at least) the canonical blessed repository of a project. In many cases, you will interact with more than one remote repository.
Git allows us to save the information about a remote repository (or just remote for short) in the config file by giving it a nickname (a shorthand name). This configuration can be managed with the git remote command.
Legacy mechanisms for storing remote repository information
There are also two legacy mechanisms to store the information about remote repositories.
This first is a named file in .git/remotes —the name of this file will be the nickname of the remote. This file can contain information about the URL or URLs, and fetch and push refspecs.
The second is a named file in .git/branches —the name of this file will also be the nickname of the remote. The contents of this file are just a URL for the repository, optionally followed by # and the branch name.
Neither of those mechanisms is likely to be found in modern repositories. See the Remotes section in the git-fetch(1) manpage for more details.
The “origin” remote
Section titled “The “origin” remote”When cloning a repository, Git will create one remote for you—the origin remote, which stores information about where you cloned from—that is the origin of your copy of the repository (hence the name). You can use this remote to fetch updates.
This is the default remote; for example, git fetch without the remote name will use the origin remote. You can change this using the remote.default configuration variable on a per-repository basis, or you can set up a default remote differently for a given branch with branch.<branchname>.remote.
Listing and examining remotes
Section titled “Listing and examining remotes”To see which remote repositories you have configured, you can run the git remote command. It lists the short names of each remote you’ve got. In a cloned repository you will have at least one remote named origin:
$ git remoteoriginTo see the URL together with remotes, you can use the -v or -- verbose options:
$ git remote --verboseorigin https://github.com/git/git.git (fetch)origin https://github.com/git/git.git (push)From the output of this command, you can easily guess that the fetch and push URLs can be different (in a so-called triangular workflow).
If you want to inspect remotes to see more information about a particular remote, you can use the git remote show < remote> subcommand:
$ git remote show originremote origin Fetch URL: https://github.com/git/git.git Push URL: https://github.com/git/git.git HEAD branch: master Remote branches: maint tracked master tracked next tracked pu tracked todo tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (up to date)Git will consult the remote configuration, the branch configuration, and the remote repository itself (for an up-to-date status). If you want to skip contacting the remote repository and use cached information instead, add the -n option to git remote show. If there is no internet connection,and you did not use '-n' option, Git will tell you that it was unable to contact the repository.
As the information about remotes is stored in the repository configuration file, you can simply examine .git/config:
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git://git.kernel.org/pub/scm/git/git.gitThe difference between local and remote branches (and remote-tracking branches: local representations of remote branches) will be described in Chapter 8*, Advanced Branching Techniques*, together with an explanation of refspecs. The refspec is a thing that is used to describe mapping between branches in remote repository and local remote-tracking branches, which looks like this: +refs/heads/*:refs/remotes/origin/*. You can see it in the second line in the preceding example.
Adding a new remote
Section titled “Adding a new remote”To add a new remote Git repository and to store its information under a short name, run git remote add < shortname> <URL>:
$ git remote add alice \ https://git.company.com/alice/random.gitAdding remote doesn’t fetch from it automatically—you need to use the -f option for that (or run git fetch < shortname> afterwards).
This command has a few options that affect how Git creates a new remote. You can select which branches in the remote repository you are interested in with the -t <branch> option. You can change which branch is the default one in the remote repository (and which you can refer to by the remote name) using the -m <branch> option; otherwise, it would be the current branch in the remote repository. You can fetch all tags or no tags with --tags or --no-tags, respectively; otherwise, only tags on fetched branches would be imported. Or you can configure the remote repository for mirroring rather than for collaboration with --mirror=push or --mirror=fetch.
For example, running the following command:
$ git remote add -t master -t next -t maint github \ https://github.com/jnareb/git.gitwill result in the following configuration of the remote:
[remote "github"] url = https://github.com/jnareb/git.git fetch = +refs/heads/master:refs/remotes/github/master fetch = +refs/heads/next:refs/remotes/github/next fetch = +refs/heads/maint:refs/remotes/github/maintUpdating information about remotes
Section titled “Updating information about remotes”The information about the remote repository is stored in three places:
- In the remote configuration:
remote.<remotename>, - In remote-tracking branches and in the remote-HEAD (
refs/remotes/<remote name>/HEAD) - And optionally, in the per-branch configuration:
branch.<branch name>
The remote-HEAD is a symbolic reference (symref) that defines the default remote-tracking branch. The remote-HEAD is a symbolic reference (symref) that defines the default remote-tracking branch. This means it determines which remote-tracking branch <remote name> refers to when used as a branch name, such as in the command 'git log < remote name>'.
You could manipulate this information directly—either by editing the appropriate files or using manipulation commands such as git config and git symbolic-ref —but Git provides various git remote subcommands for this.
Renaming remotes
Section titled “Renaming remotes”Renaming the remote—that is, changing its nickname—is quite a complicated operation. Running git remote rename <old> <new> will not only change the section name in remote.<old>, but also the remote-tracking branches and accompanying refspec, their reflogs (if there are any—see the core.logAllRefUpdates configuration variable), and the respective branch configurations.
Changing the remote URLs
Section titled “Changing the remote URLs”You can add or replace the URL for a remote with git remote set-url, but it is also quite easy to simply directly edit the configuration.
You can also use the insteadOf (and pushInsteadOf) configuration variables. This can be useful if you want to temporarily use another server, for example, if the canonical repository is temporarily down. Say that you want to fetch Git from the repository on GitHub, because https://www.kernel.org that you cloned Git from is down. You can do this by adding the following text to the config file:
[url "https://github.com/git/git.git"] insteadOf = git://git.kernel.org/pub/scm/git/git.gitAnother use case for this feature is handling repository migration. You can use insteadOf rewriting in the per-user configuration file, that is, in ~/.gitconfig (or ~/.config/git/config), without having to change the URL in each and every repository’s .git/config file. In the case of more than one match, the longest match is used.
Tip – multiple URLs for a remote
You can set multiple URLs for a remote. Git will try all these URLs sequentially when fetching and use the first one that works. When pushing, Git will publish to all URLs (all servers) simultaneously.
Changing the list of branches tracked by remote
Section titled “Changing the list of branches tracked by remote”A similar situation to changing the URL occurs when changing the list of branches tracked by a remote (that is, the contents of fetch lines). You can use git remote set-branches (with a sufficiently modern Git client) or edit the config file directly.
Note – stale remote-tracking branches
Freeing a branch in a remote repository from being tracked does not remove the remote-tracking branch—the latter is simply no longer updated on fetch. This is explained in more detail in the Deleting remote-tracking branches and remotes section later in this chapter, which describes how to prune remote-tracking branches that correspond to branches deleted in remote the repository.
Setting the default branch of the remote
Section titled “Setting the default branch of the remote”Having a default branch on the remote is not required, but it lets us specify the remote name (for example, origin) instead of a specific remote-tracking branch (for example, origin/master). This information is stored in the symbolic ref <remote name>/HEAD (for example, origin/HEAD).
You can set this with git remote set-head command. The --auto option does that based on what the current branch in the remote repository is:
$ git remote set-head origin master$ git branch -r origin/HEAD -> origin/master origin/masterYou can delete the default branch on the remote with the -- delete option.
Deleting remote-tracking branches and remotes
Section titled “Deleting remote-tracking branches and remotes”When a public branch is deleted in the remote repository, Git nevertheless keeps the corresponding remote-tracking branch. It does that because you might want to do, or might have already done, your own work on top of it. You can, however, delete the remote-tracking branch with git branch -r -d, or you can ask Git to prune all stale remote-tracking branches under the remote with git remote prune. You can configure Git to do this automatically on every fetch, as if git fetch were run with the --prune option, by setting the fetch.prune and/or remote.<name>.prune configuration variables (the latter on a per-remote basis).
You can check which remote-tracking branches are stale with the --dry-run option to git remote prune, or with the git remote show command.
Deleting remote is as simple as running git remote delete (or its alias, git remote rm). It also removes remote-tracking branches for the deleted remote.
Support for triangular workflows
Section titled “Support for triangular workflows”In many collaborative workflows, such as the maintainer (or integration manager) workflow, you fetch from one URL (from the blessed repository), but push to another URL (to your own public repository).
As shown in Figure 6*.4*, the developer interacts with three repositories: they fetch from the blessed repository (top left) into their own private repository (darker, at the bottom), then push their work into their own public repository (lighter, at the top).
In such a triangular workflow (three repositories), the remote you fetch or pull from is usually the default origin remote (or remote.default). One option for configuring which repository you push to is to add this repository as a separate remote, and perhaps also set it up as the default with remote.pushDefault:
[remote "origin"] url = https://git.company.com/project fetch = +refs/heads/*:refs/remotes/origin/*[remote "myown"] url = [email protected]:user/projectfetch = +refs/heads/*:refs/remotes/myown/*[remote] pushDefault = myownYou could also set it as pushRemote in the per-branch configuration:
[branch "master"] remote = origin pushRemote = myown merge = refs/heads/masterAnother option is to use a single remote (perhaps even origin) but set it up with a separate pushurl. This solution, however, has the slight disadvantage that you don’t have separate remote-tracking branches for the push repository (and thus there is no support @{push} notation in addition to having @{upstream} as a shortcut for specifying the appropriate remote-tracking branches):
[remote "origin"] url = https://git.company.com/project pushurl = [email protected]:user/project fetch = +refs/heads/*:refs/remotes/origin/*Having separate remote-tracking branches for the push repository allows you to track which branches were pushed to the push remote, and which have local unpublished changes.
Chain of trust
Section titled “Chain of trust”An important part of collaborative efforts during the development of a project is ensuring the quality of its code. This includes protection against the accidental corruption of the repository, and also from malicious intent —a task that the version control system can help with. Git needs to ensure trust in the repository contents: both your own and other developers’ (especially trust in the canonical repository of the project).
Content-addressed storage
Section titled “Content-addressed storage”In Chapter 4, Exploring Project History, in the SHA-1 and the shortened SHA-1 identifier section, we learned that Git currently uses SHA-1 hashes as a native identifier of commit objects (which represent revisions of the project and form the project’s history). This mechanism makes it possible to generate commit identifiers in a distributed way, taking a cryptographic hash of the commit object. This hash is then used to link to the previous commit (to the parent commit or commits).
Moreover, all other data stored in the repository (including the file contents in the revision represented by the blob objects, and the file hierarchy represented by the tree objects) also use the same mechanism. All types of object are addressed by their contents, or to be more accurate, the hash function of the object. You can say that the base of a Git repository is the content-addressed object database.
Thus, Git provides a built-in trust chain through secure SHA-1 hashes, via a kind of a hash tree, also known as a Merkle tree. In one dimension, the SHA-1 hash of a commit depends on its contents, which includes the SHA-1 hash of the parent commit or commits, which depends on the contents of the parent commit, and so forth down to the initial root commit. In the other dimension, the content of a commit object includes the SHA-1 hash of the tree representing the top directory of a project, which in turn depends on its contents, and these contents include the SHA-1 hash of the subdirectory trees and blobs of file contents, and so forth down to the individual files.

Figure 6.6 – Hash tree of a short history of a project, with a tag, two commits, and their contents. The SHA-1 hashes, shown in shortened form, depending on their contents
All of this allows SHA-1 hashes to be used to verify whether objects obtained from a (potentially untrusted) source have been corrupted or modified since they were created.
Lightweight, annotated, and signed tags
Section titled “Lightweight, annotated, and signed tags”The trust chain allows us to verify the contents but does not verify the identity of the person who created the content (the author and committer name are fully configurable and under user control). This is the task for GPG/PGP signatures: signed tags, signed commits, and signed merges.
Since Git version 2.34, you can also use SSH keys for signing by setting the gpg.format configuration variable to the value ssh, for example with git config gpg.format ssh (you may also need to use your public key as the configuration value for the user.signingKey configuration variable).
Lightweight tags
Section titled “Lightweight tags”Git uses two types of tags: lightweight tags and annotated tags (there are also signed tags, which are a special case of annotated tags).
A lightweight tag is very much like a branch that doesn’t change – it’s just a pointer (reference) to a specific commit in the graph of revisions, though in the refs/tags/ namespace rather than in refs/heads/.
Annotated tags
Section titled “Annotated tags”Annotated tags, however, involve tag objects. Here the tag reference (in refs/tags/ namespace) points to a tag object, which in turn points to a commit. Tag objects contain a creation date, the tagger identity (name and e-mail), and a tagging message. You create an annotated tag with git tag -a (or --annotate). If you don’t specify a message for an annotated tag on the command line (for example, with -m "<message>"), Git will launch your editor so you can enter it.
You can view the tag data along with the tagged commit with the git show command as follows (commit skipped):
$ git show v0.2tag v0.2Tagger: Joe R Hacker <[email protected]>Date: Sun Jun 1 03:10:07 2014 -0700random v0.2commit 5d2584867fe4e94ab7d211a206bc0bc3804d37a9Signed tags
Section titled “Signed tags”Signed tags are annotated tags with a clear text PGP signature (or, with modern Git, an SSH signature) of the tag data attached. You can create them with git tag -s (which uses your committer identity to select the signing key, or user.signingKey if set), or with git tag -u <key-id>; both versions assume that you have a private GPG key (created, for example, with gpg --gen-key).
Lightweight tags versus annotated and signed tags
Annotated or signed tags are meant for marking a release, while lightweight tags are meant for private or temporary revision labels. For this reason, some Git commands (such as git describe) will ignore lightweight tags by default.
Of course in collaborative workflows, it is important that the signed tag is made public, and that there is a way to verify it; both of those operations will be described in the following sections.
Publishing tags
Section titled “Publishing tags”Git does not push tags by default: you need to do it explicitly. One solution is to individually push a tag with git push <remote> tag <tag-name> (here, tag <tag> is equivalent to the longer refspec (describing how refs on the remote translate to refs in the local repository), namely refs/tags/<tag>:refs/tags/<tag>); however, if you don’t have the naming conflict between a branch and a tag (i.e., you don’t have branch and tag with the same name), then you can skip the word tag here in this operation.
Another solution is to push tags en masse: either all the tags—both lightweight and annotated—with the use of the --tags option, or just all annotated tags that point to pushed commits with --follow-tags. This explicitness allows you to re-tag (using git tag -f) with impunity if it turns out that you tagged the wrong commit, or there is a need for a last-minute fix—but only if the tag was not made public. Git does not (and should not) change tags behind the user’s back; thus, if you pushed the wrong tag, you need to ask others to delete this old tag to change it.
When fetching changes, Git automatically follows tags, downloading annotated tags that point to fetched commits. This means that downstream developers will automatically get signed tags, and will be able to verify releases.
Tag verification
Section titled “Tag verification”To verify a signed tag, you use git tag --verify <tag-name> (or -v for short). You need the signer’s public GPG key in your keyring for this (imported using gpg --import or gpg --keyserver <key-server> --recv-key <key-id>), and of course the tagger’s key needs to be vetted in your chain of trust. For SSH keys there is no web of trust; you need to specify the trusted public keys with the gpg.ssh.allowedSignersFile configuration variable.
$ git tag --verify v0.2object 1085f3360e148e4b290ea1477143e25cae995fddtype committag signedtagger Joe Random 1411122206 +0200project v0.2gpg: Signature made Fri Jul 19 12:23:33 2014 CEST using RSA key ID A0218851gpg: Good signature from "Joe Random <[email protected]>"Signed commits
Section titled “Signed commits”Signed tags are a good solution for users and developers to verify that the tagged release was created by the maintainer. But how do we make sure that a commit purporting to be by somebody named Jane Doe, with the [email protected] e-mail, is actually a commit from her? How can we make it so anybody can check this?
One possible solution is to sign individual commits. You can do this with git commit --gpg-sign[=<keyid>] (or -S for short). The key identifier is optional—without this, Git would use your identity as the author. Note that -S (capital S) is different from -s (small s); the latter adds a Signed-off-by line at the end of the commit message for the Digital Certificate of Ownership:
$ git commit -a --gpg-signYou need a passphrase to unlock the secret key foruser: "Jane Doe <[email protected]>"2048-bit RSA key, ID A0218851, created 2014-03-19[master 1085f33] README: eol at eof 1 file changed, 1 insertion(+), 1 deletion(-)To make commits available for verification, just push them. Anyone can then verify them with the --show-signature option to git log (or git show), or with one of the %Gx placeholders in git log --format=<format>:
$ git log -1 --show-signaturecommit 1085f3360e148e4b290ea1477143e25cae995fddgpg: Signature made Wed Mar 19 11:53:49 2014 CEST using RSA key ID A0218851gpg: Good signature from "Jane Doe <[email protected]>Author: Jane Doe <[email protected]>Date: Wed Mar 19 11:53:48 2014 +0200 README: eol at eofYou can also use the git verify-commit command for this.
Merging signed tags (merge tags)
Section titled “Merging signed tags (merge tags)”The signed commit mechanism, described in the previous section, may be useful in some workflows, but it is inconvenient in an environment where you push commits out early, and only after a while do you decide whether they are worth including in the upstream. In such cases, you would want to sign only those parts that are ready to be published.
This situation can happen if you follow the recommendations in Chapter 10, Keeping History Clean; you know only after the fact (long after the commit was created) that the given iteration of the commit series passes code review. Commits need to be signed at commit creation time, but you can create a signed tag after the fact, after the series of commits gets accepted.
You can deal with this issue by rewriting the whole commit series after its shape is finalized (after passing the review), signing each rewritten commit, or just by amending and signing only the top commit. Both of those solutions would require a forced push to replace the old history where commits were not signed. You can always sign every commit, or you can create an empty commit (with --allow-empty), sign it, and push it on top of the series. But there is a better solution: requesting the pull of a signed tag.
In this workflow, you work on your changes and, when they are ready, you create and push a signed tag (tagging the last commit in the series). You don’t have to push your working branch—pushing the tag is enough. If the workflow involves sending a pull request to the integrator, you create it using a signed tag instead of the end commit:
$ git tag -s 1253-for-maintainer$ git request-pull origin/master public-repo \ 1253-for-maintainer >msg.txtThe signed tag message is shown between the dashed lines in the pull request, which means that you may want to explain your work in the tag message when creating the signed tag. The maintainer, after receiving such a pull request, can copy the repository line from it, fetching and integrating the named tag. When recording the merge result of pulling the named tag, Git will open an editor and ask for a commit message. The integrator will see a template starting with the following:
Merge tag '1252-for-maintainer'Work on task tsk-1252# gpg: Signature made Wed Mar 19 12:23:33 2014 CEST using RSA key ID A0218851# gpg: Good signature from "Jane Doe <[email protected]>"This commit template includes the commented-out output of the verification of the signed tag object being merged (so it won’t be in the final merge commit message). The tag message helps describe the merge better.
The signed tag being pulled is not stored in the integrator’s repository, not as a tag object. Its content is stored, hidden, in a merge commit. This is done to avoid polluting the tag namespace with a large number of such working tags. The developer can safely delete the tag (git push public-repo --delete 1252-for-maintainer) after it gets integrated.
Recording the signature inside the merge commit allows for after-the-fact verification with the -- show-signature option:
$ git log -1 --show-signaturecommit 0507c804e0e297cd163481d4cb20f3f48ceb87cbmerged tag '1252-for-maintainer'gpg: Signature made Wed Mar 19 12:23:33 2014 CEST using RSA key ID A0218851gpg: Good signature from "Jane Doe <[email protected]>"Merge: 5d25848 1085f33Author: Jane Doe <[email protected]>Date: Wed Mar 19 12:25:08 2014 +0200 Merge tag 'for-maintainer' Work on task tsk-1252Summary
Section titled “Summary”Through this chapter, we learned how to use Git for collaborative development and how to work together in a team on a project. We got to know different collaborative workflows, that is, different ways of setting up repositories for collaboration. Which one to use depends on circumstances: how large the team is, how diverse, and so on. This chapter focuses on repository-to-repository interaction; the interplay between branches and remote-tracking branches in those repositories is left for Chapter 8, Advanced Branching Techniques.
We learned how Git can help manage information about remote repositories involved in the chosen workflow. We were shown how to store, view, and update this information. This chapter explains how one can manage triangular workflows, in which you fetch from one repository (canonical), and push to the other (public).
We learned about the chain of trust: how to verify that a release comes from the maintainer, how to sign your work so that the maintainer can verify that it comes from you, and how the Git architecture helps with this.
The next chapter, namely Chapter 7, Publishing Your Changes, will talk about how to get your contribution to other remote repositories. The two further following chapters will expand on the topic of collaboration: Chapter 8, Advanced Branching Techniques, will explore relations between local branches and branches in a remote repository and how to set up branches for collaboration, while Chapter 9, Merging Changes Together, will talk about the opposite issue—how to join the results of parallel work.
Questions
Section titled “Questions”Answer the following questions to test your knowledge of this chapter:
- What operation do you need to publish your changes to your public remote repository, and what operation do you need to get changes from a remote?
- What is the difference between
git fetchandgit pull? - How can you remove stale remote-tracking branches (that is, remote-tracking branches where the corresponding branch on the remote was deleted)?
Answers
Section titled “Answers”Here are the answers to the questions given above:
- Use
git pushto publish your changes, and usegit fetchorgit pull(orgit remote update) to get changes from the remote repository. - The
fetchoperation only downloads changes and updates the remote-tracking branches, while thepulloperation also tries to update the current branch withmergeorrebase(if it is configured as tracking some branch in the remote repository). - You can use
git branch -d -rto delete individual remote-tracking branches, orgit remote pruneto delete all stale remote-tracking branches.
Further reading
Section titled “Further reading”To learn more about the topics that were covered in this chapter, take a look at the following resources:
- Scott Chacon and Ben Straub: Pro Git, 2nd Edition (2014) https://git-scm.com/book/en/v2
- Chapter 5.1 Distributed Git - Distributed Workflows
- Chapter 2.5 Git Basics - Working with Remotes
- Chapter 7.4 Git Tools - Signing Your Work
- Chapter 5.1 Distributed Git - Distributed Workflows
- Ryan Brown: gpg-sign releases (2014) https://gitready.com/advanced/2014/11/02/gpg-sign-releases.html
- Danilo Bargen: Signing Git Commits with SSH Keys (2021) https://blog.dbrgn.ch/2021/11/16/git-ssh-signatures/
- Carl Tashian: SSH Tips & Trick – Add a second factor to your SSH login (2020) https://smallstep.com/blog/ssh-tricks-and-tips/#add-a-second-factor-to-your-ssh
- Junio C Hamano: Git Blame: Fun (?) with GnuPG (2014) https://git-blame.blogspot.com/2014/09/fun-with-gnupg.html