Pro Git Manual

ManualGit

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 578

DownloadPro Git Manual
Open PDF In BrowserView PDF
This work is licensed under the Creative Commons AttributionNonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter
to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.

Preface by Scott Chacon

Welcome to the second edition of Pro Git. The first edition was published over
four years ago now. Since then a lot has changed and yet many important
things have not. While most of the core commands and concepts are still valid
today as the Git core team is pretty fantastic at keeping things backward compatible, there have been some significant additions and changes in the community surrounding Git. The second edition of this book is meant to address those
changes and update the book so it can be more helpful to the new user.
When I wrote the first edition, Git was still a relatively difficult to use and
barely adopted tool for the harder core hacker. It was starting to gain steam in
certain communities, but had not reached anywhere near the ubiquity it has today. Since then, nearly every open source community has adopted it. Git has
made incredible progress on Windows, in the explosion of graphical user interfaces to it for all platforms, in IDE support and in business use. The Pro Git of
four years ago knows about none of that. One of the main aims of this new edition is to touch on all of those new frontiers in the Git community.
The Open Source community using Git has also exploded. When I originally
sat down to write the book nearly five years ago (it took me a while to get the
first version out), I had just started working at a very little known company developing a Git hosting website called GitHub. At the time of publishing there
were maybe a few thousand people using the site and just four of us working on
it. As I write this introduction, GitHub is announcing our 10 millionth hosted
project, with nearly 5 million registered developer accounts and over 230 employees. Love it or hate it, GitHub has heavily changed large swaths of the Open
Source community in a way that was barely conceivable when I sat down to
write the first edition.
I wrote a small section in the original version of Pro Git about GitHub as an
example of hosted Git which I was never very comfortable with. I didn’t much
like that I was writing what I felt was essentially a community resource and also
talking about my company in it. While I still don’t love that conflict of interests,
the importance of GitHub in the Git community is unavoidable. Instead of an
example of Git hosting, I have decided to turn that part of the book into more
deeply describing what GitHub is and how to effectively use it. If you are going
to learn how to use Git then knowing how to use GitHub will help you take part

iii

Preface by Scott Chacon

in a huge community, which is valuable no matter which Git host you decide to
use for your own code.
The other large change in the time since the last publishing has been the development and rise of the HTTP protocol for Git network transactions. Most of
the examples in the book have been changed to HTTP from SSH because it’s so
much simpler.
It’s been amazing to watch Git grow over the past few years from a relatively
obscure version control system to basically dominating commercial and open
source version control. I’m happy that Pro Git has done so well and has also
been able to be one of the few technical books on the market that is both quite
successful and fully open source.
I hope you enjoy this updated edition of Pro Git.

iv

Preface by Ben Straub

The first edition of this book is what got me hooked on Git. This was my introduction to a style of making software that felt more natural than anything I had
seen before. I had been a developer for several years by then, but this was the
right turn that sent me down a much more interesting path than the one I was
on.
Now, years later, I’m a contributor to a major Git implementation, I’ve
worked for the largest Git hosting company, and I’ve traveled the world teaching people about Git. When Scott asked if I’d be interested in working on the
second edition, I didn’t even have to think.
It’s been a great pleasure and privilege to work on this book. I hope it helps
you as much as it did me.

v

Dedications

To my wife, Becky, without whom this adventure never would have begun. — Ben
This edition is dedicated to my girls. To my wife Jessica who has supported me
for all of these years and to my daughter Josephine, who will support me when
I’m too old to know what’s going on. — Scott

vii

Contribuidores

Debido a que este es un libro cuya traducción es “Open Source”, hemos recibido la colaboración de muchas personas a lo largo de los últimos años. A continuación hay una lista de todas las personas que han contribuido en la traducción del libro al idioma español. Muchas gracias a todos por colaborar a mejorar este libro para el beneficio de todos los hispanohablantes.
35
15
4
3
2
1

Andrés Mancera
Carlos A. Henríquez Q.
Dmunoz94
Sergio Martell
Mario R. Rincón-Díaz
Juan Sebastián Casallas

ix

Introduction

You’re about to spend several hours of your life reading about Git. Let’s take a
minute to explain what we have in store for you. Here is a quick summary of the
ten chapters and three appendices of this book.
In Chapter 1, we’re going to cover Version Control Systems (VCSs) and Git
basics—no technical stuff, just what Git is, why it came about in a land full of
VCSs, what sets it apart, and why so many people are using it. Then, we’ll explain how to download Git and set it up for the first time if you don’t already
have it on your system.
In Chapter 2, we will go over basic Git usage—how to use Git in the 80% of
cases you’ll encounter most often. After reading this chapter, you should be
able to clone a repository, see what has happened in the history of the project,
modify files, and contribute changes. If the book spontaneously combusts at
this point, you should already be pretty useful wielding Git in the time it takes
you to go pick up another copy.
Chapter 3 is about the branching model in Git, often described as Git’s killer
feature. Here you’ll learn what truly sets Git apart from the pack. When you’re
done, you may feel the need to spend a quiet moment pondering how you lived
before Git branching was part of your life.
Chapter 4 will cover Git on the server. This chapter is for those of you who
want to set up Git inside your organization or on your own personal server for
collaboration. We will also explore various hosted options if you prefer to let
someone else handle that for you.
Chapter 5 will go over in full detail various distributed workflows and how to
accomplish them with Git. When you are done with this chapter, you should be
able to work expertly with multiple remote repositories, use Git over e-mail and
deftly juggle numerous remote branches and contributed patches.
Chapter 6 covers the GitHub hosting service and tooling in depth. We cover
signing up for and managing an account, creating and using Git repositories,
common workflows to contribute to projects and to accept contributions to
yours, GitHub’s programmatic interface and lots of little tips to make your life
easier in general.
Chapter 7 is about advanced Git commands. Here you will learn about topics like mastering the scary reset command, using binary search to identify

xi

Introduction

bugs, editing history, revision selection in detail, and a lot more. This chapter
will round out your knowledge of Git so that you are truly a master.
Chapter 8 is about configuring your custom Git environment. This includes
setting up hook scripts to enforce or encourage customized policies and using
environment configuration settings so you can work the way you want to. We
will also cover building your own set of scripts to enforce a custom committing
policy.
Chapter 9 deals with Git and other VCSs. This includes using Git in a Subversion (SVN) world and converting projects from other VCSs to Git. A lot of organizations still use SVN and are not about to change, but by this point you’ll have
learned the incredible power of Git—and this chapter shows you how to cope if
you still have to use a SVN server. We also cover how to import projects from
several different systems in case you do convince everyone to make the plunge.
Chapter 10 delves into the murky yet beautiful depths of Git internals. Now
that you know all about Git and can wield it with power and grace, you can
move on to discuss how Git stores its objects, what the object model is, details
of packfiles, server protocols, and more. Throughout the book, we will refer to
sections of this chapter in case you feel like diving deep at that point; but if you
are like us and want to dive into the technical details, you may want to read
Chapter 10 first. We leave that up to you.
In Appendix A we look at a number of examples of using Git in various specific environments. We cover a number of different GUIs and IDE programming
environments that you may want to use Git in and what is available for you. If
you’re interested in an overview of using Git in your shell, in Visual Studio or
Eclipse, take a look here.
In Appendix B we explore scripting and extending Git through tools like libgit2 and JGit. If you’re interested in writing complex and fast custom tools and
need low level Git access, this is where you can see what that landscape looks
like.
Finally in Appendix C we go through all the major Git commands one at a
time and review where in the book we covered them and what we did with
them. If you want to know where in the book we used any specific Git command
you can look that up here.
Let’s get started.

xii

Table of Contents

Preface by Scott Chacon

iii

Preface by Ben Straub

v

Dedications

vii

Contribuidores

ix

Introduction

xi

CHAPTER 1: Inicio - Sobre el Control de Versiones

25

Acerca del Control de Versiones

25

Sistemas de Control de Versiones Locales

26

Sistemas de Control de Versiones Centralizados

27

Sistemas de Control de Versiones Distribuidos

28

Una breve historia de Git

30

Fundamentos de Git

30

Copias instantáneas, no diferencias

31

Casi todas las operaciones son locales

32

Git tiene integridad

33

Git generalmente solo añade información

33

Los Tres Estados

33

La Línea de Comandos

35

Instalación de Git

35

Instalación en Linux

36

xiii

Table of Contents

Instalación en Mac

36

Instalación en Windows

37

Instalación a partir del Código Fuente

38

Configurando Git por primera vez
Tu Identidad

39

Tu Editor

40

Comprobando tu Configuración

40

¿Cómo obtener ayuda?

41

Resumen

41

CHAPTER 2: Fundamentos de Git

43

Obteniendo un repositorio Git

43

Inicializando un repositorio en un directorio existente

43

Clonando un repositorio existente

44

Guardando cambios en el Repositorio

45

Revisando el Estado de tus Archivos

46

Rastrear Archivos Nuevos

47

Preparar Archivos Modificados

48

Estatus Abreviado

49

Ignorar Archivos

50

Ver los Cambios Preparados y No Preparados

51

Confirmar tus Cambios

54

Saltar el Área de Preparación

56

Eliminar Archivos

56

Cambiar el Nombre de los Archivos

58

Ver el Historial de Confirmaciones

59

Limitar la Salida del Historial

64

Deshacer Cosas

66

Deshacer un Archivo Preparado

67

Deshacer un Archivo Modificado

68

Trabajar con Remotos

xiv

38

69

Table of Contents

Ver Tus Remotos

69

Añadir Repositorios Remotos

71

Traer y Combinar Remotos

71

Enviar a Tus Remotos

72

Inspeccionar un Remoto

73

Eliminar y Renombrar Remotos

74

Etiquetado

74

Listar Tus Etiquetas

75

Crear Etiquetas

75

Etiquetas Anotadas

76

Etiquetas Ligeras

76

Etiquetado Tardío

77

Compartir Etiquetas

78

Sacar una Etiqueta

79

Git Aliases

79

Resumen

81

CHAPTER 3: Ramificaciones en Git

83

¿Qué es una rama?

83

Crear una Rama Nueva

86

Cambiar de Rama

87

Procedimientos Básicos para Ramificar y Fusionar

91

Procedimientos Básicos de Ramificación

91

Procedimientos Básicos de Fusión

96

Principales Conflictos que Pueden Surgir en las Fusiones

98

Gestión de Ramas

101

Flujos de Trabajo Ramificados

103

Ramas de Largo Recorrido

103

Ramas Puntuales

104

Ramas Remotas
Publicar

107
112

xv

Table of Contents

Hacer Seguimiento a las Ramas

114

Traer y Fusionar

116

Eliminar Ramas Remotas

116

Reorganizar el Trabajo Realizado
Reorganización Básica

117

Algunas Reorganizaciones Interesantes

120

Los Peligros de Reorganizar

122

Reorganizar una Reorganización

125

Reorganizar vs. Fusionar

127

Recapitulación

127

CHAPTER 4: Git en el Servidor

129

The Protocols

130

Local Protocol

130

The HTTP Protocols

131

The SSH Protocol

134

The Git Protocol

134

Configurando Git en un servidor

135

Colocando un Repositorio Vacío en un Servidor

136

Pequeñas configuraciones

137

Generating Your SSH Public Key

138

Setting Up the Server

139

Git Daemon

142

Smart HTTP

144

GitWeb

145

GitLab

148

Installation

148

Administration

149

Basic Usage

152

Working Together

152

Third Party Hosted Options

xvi

117

153

Table of Contents

Resumen

153

CHAPTER 5: Distributed Git

155

Distributed Workflows

155

Centralized Workflow

155

Integration-Manager Workflow

156

Dictator and Lieutenants Workflow

157

Workflows Summary

158

Contributing to a Project

159

Commit Guidelines

159

Private Small Team

161

Private Managed Team

168

Forked Public Project

174

Public Project over E-Mail

178

Summary

181

Maintaining a Project

181

Working in Topic Branches

182

Applying Patches from E-mail

182

Checking Out Remote Branches

186

Determining What Is Introduced

187

Integrating Contributed Work

188

Tagging Your Releases

195

Generating a Build Number

196

Preparing a Release

197

The Shortlog

197

Summary

198

CHAPTER 6: GitHub

199

Account Setup and Configuration

199

SSH Access

200

Your Avatar

202

xvii

Table of Contents

Your Email Addresses

203

Two Factor Authentication

204

Contributing to a Project
Forking Projects

205

The GitHub Flow

206

Advanced Pull Requests

214

Markdown

219

Maintaining a Project

224

Creating a New Repository

224

Adding Collaborators

226

Managing Pull Requests

228

Mentions and Notifications

233

Special Files

237

README

237

CONTRIBUTING

238

Project Administration

238

Managing an organization

240

Organization Basics

240

Teams

241

Audit Log

243

Scripting GitHub

xviii

205

244

Hooks

245

The GitHub API

249

Basic Usage

250

Commenting on an Issue

251

Changing the Status of a Pull Request

252

Octokit

254

Summary

255

CHAPTER 7: Git Tools

257

Revision Selection

257

Table of Contents

Single Revisions

257

Short SHA-1

257

Branch References

259

RefLog Shortnames

260

Ancestry References

261

Commit Ranges

263

Interactive Staging

266

Staging and Unstaging Files

266

Staging Patches

269

Stashing and Cleaning

270

Stashing Your Work

270

Creative Stashing

273

Creating a Branch from a Stash

274

Cleaning your Working Directory

275

Signing Your Work

276

GPG Introduction

277

Signing Tags

277

Verifying Tags

278

Signing Commits

279

Everyone Must Sign

281

Searching

281

Git Grep

281

Git Log Searching

283

Rewriting History

284

Changing the Last Commit

285

Changing Multiple Commit Messages

285

Reordering Commits

288

Squashing Commits

288

Splitting a Commit

290

The Nuclear Option: filter-branch

291

Reset Demystified

293

xix

Table of Contents

The Three Trees

293

The Workflow

295

The Role of Reset

301

Reset With a Path

306

Squashing

309

Check It Out

312

Summary

314

Advanced Merging

315

Merge Conflicts

315

Undoing Merges

327

Other Types of Merges

330

Rerere

335

Debugging with Git

341

File Annotation

341

Binary Search

343

Submodules
Starting with Submodules

345

Cloning a Project with Submodules

347

Working on a Project with Submodules

349

Submodule Tips

360

Issues with Submodules

362

Bundling

364

Replace

368

Credential Storage

377

Under the Hood

378

A Custom Credential Cache

381

Summary

383

CHAPTER 8: Customizing Git

385

Git Configuration

385

Basic Client Configuration

xx

345

386

Table of Contents

Colors in Git

389

External Merge and Diff Tools

390

Formatting and Whitespace

394

Server Configuration

396

Git Attributes

397

Binary Files

397

Keyword Expansion

400

Exporting Your Repository

403

Merge Strategies

404

Git Hooks

405

Installing a Hook

405

Client-Side Hooks

406

Server-Side Hooks

408

An Example Git-Enforced Policy

409

Server-Side Hook

409

Client-Side Hooks

415

Summary

419

CHAPTER 9: Git and Other Systems

421

Git as a Client

421

Git and Subversion

421

Git and Mercurial

433

Git and Perforce

442

Git and TFS

458

Migrating to Git

467

Subversion

468

Mercurial

470

Perforce

472

TFS

475

A Custom Importer

476

xxi

Table of Contents

Summary

483

CHAPTER 10: Git Internals

485

Plumbing and Porcelain

485

Git Objects

486

Tree Objects

489

Commit Objects

492

Object Storage

495

Git References

497

The HEAD

498

Tags

499

Remotes

501

Packfiles

501

The Refspec

505

Pushing Refspecs

507

Deleting References

507

Transfer Protocols
The Dumb Protocol

508

The Smart Protocol

510

Protocols Summary

513

Maintenance and Data Recovery

xxii

508

514

Maintenance

514

Data Recovery

515

Removing Objects

518

Environment Variables

522

Global Behavior

522

Repository Locations

522

Pathspecs

523

Committing

523

Networking

524

Diffing and Merging

524

Table of Contents

Debugging

525

Miscellaneous

527

Summary

527

Git in Other Environments

529

Embedding Git in your Applications

545

Git Commands

557

Index

575

xxiii

Inicio - Sobre el Control de
Versiones

1

Este capítulo habla de cómo comenzar a utilizar Git. Empezaremos describiendo algunos conceptos básicos sobre las herramientas de control de versiones;
después, trataremos sobre cómo hacer que Git funcione en tu sistema; finalmente, exploraremos cómo configurarlo para empezar a trabajar con él. Al final
de este capítulo deberás entender las razones por las cuales Git existe y conviene que lo uses, y deberás tener todo preparado para comenzar.

Acerca del Control de Versiones
¿Qué es control de versiones, y por qué debería importarte? Control de versiones es un sistema que registra los cambios realizados en un archivo o conjunto de archivos a lo largo del tiempo, de modo que puedas recuperar versiones específicas más adelante. Aunque en los ejemplos de este libro usarás
archivos de código fuente como aquellos cuya versión está siendo controlada,
en realidad puedes hacer lo mismo con casi cualquier tipo de archivo que encuentres en una computadora.
Si eres diseñador gráfico o de web y quieres mantener cada versión de una
imagen o diseño (algo que sin duda vas a querer), usar un sistema de control de
versiones (VCS por sus siglas en inglés) es una muy decisión muy acertada. Dicho sistema te permite regresar a versiones anteriores de tus archivos, regresar
a una versión anterior del proyecto completo, comparar cambios a lo largo del
tiempo, ver quién modificó por última vez algo que pueda estar causando problemas, ver quién introdujo un problema y cuándo, y mucho más. Usar un VCS
también significa generalmente que si arruinas o pierdes archivos, será posible
recuperarlos fácilmente. Adicionalmente, obtendrás todos estos beneficios a
un costo muy bajo.

25

CHAPTER 1: Inicio - Sobre el Control de Versiones

Sistemas de Control de Versiones Locales
Un método de control de versiones usado por muchas personas es copiar los
archivos a otro directorio (quizás indicando la fecha y hora en que lo hicieron, si
son ingeniosos). Este método es muy común porque es muy sencillo, pero también es tremendamente propenso a errores. Es fácil olvidar en qué directorio te
encuentras, y guardar accidentalmente en el archivo equivocado o sobrescribir
archivos que no querías.
Para afrontar este problema los programadores desarrollaron hace tiempo
VCS locales que contenían una simple base de datos en la que se llevaba el registro de todos los cambios realizados a los archivos.

FIGURE 1-1
Local version
control.

Una de las herramientas de control de versiones más popular fue un sistema
llamado RCS, que todavía podemos encontrar en muchas de las computadoras
actuales. Incluso el famoso sistema operativo Mac OS X incluye el comando rcs
cuando instalas las herramientas de desarrollo. Esta herramienta funciona
guardando conjuntos de parches (es decir, las diferencias entre archivos) en un

26

Acerca del Control de Versiones

formato especial en disco, y es capaz de recrear cómo era un archivo en cualquier momento a partir de dichos parches.

Sistemas de Control de Versiones Centralizados
El siguiente gran problema con el que se encuentran las personas es que necesitan colaborar con desarrolladores en otros sistemas. Los sistemas de Control
de Versiones Centralizados (CVCS por sus siglas en inglés) fueron desarrollados
para solucionar este problema. Estos sistemas, como CVS, Subversion, y Perforce, tienen un único servidor que contiene todos los archivos versionados, y
varios clientes que descargan los archivos desde ese lugar central. Este ha sido
el estándar para el control de versiones por muchos años.

FIGURE 1-2
Centralized version
control.

Esta configuración ofrece muchas ventajas, especialmente frente a VCS locales. Por ejemplo, todas las personas saben hasta cierto punto en qué están
trabajando los otros colaboradores del proyecto. Los administradores tienen
control detallado sobre qué puede hacer cada usuario, y es mucho más fácil administrar un CVCS que tener que lidiar con bases de datos locales en cada cliente.
Sin embargo, esta configuración también tiene serias desventajas. La más
obvia es el punto único de fallo que representa el servidor centralizado. Si ese

27

CHAPTER 1: Inicio - Sobre el Control de Versiones

servidor se cae durante una hora, entonces durante esa hora nadie podrá colaborar o guardar cambios en archivos en los que hayan estado trabajando. Si el
disco duro en el que se encuentra la base de datos central se corrompe, y no se
han realizado copias de seguridad adecuadamente, se perderá toda la información del proyecto, con excepción de las copias instantáneas que las personas
tengan en sus máquinas locales. Los VCS locales sufren de este mismo problema: Cuando tienes toda la historia del proyecto en un mismo lugar, te arriesgas a perderlo todo.

Sistemas de Control de Versiones Distribuidos
Los sistemas de Control de Versiones Distribuidos (DVCS por sus siglas en inglés) ofrecen soluciones para los problemas que han sido mencionados. En un
DVCS (como Git, Mercurial, Bazaar o Darcs), los clientes no solo descargan la última copia instantánea de los archivos, sino que se replica completamente el
repositorio. De esta manera, si un servidor deja de funcionar y estos sistemas
estaban colaborando a través de él, cualquiera de los repositorios disponibles
en los clientes puede ser copiado al servidor con el fin de restaurarlo. Cada clon
es realmente una copia completa de todos los datos.

28

Acerca del Control de Versiones

FIGURE 1-3
Distributed version
control.

Además, muchos de estos sistemas se encargan de manejar numerosos repositorios remotos con los cuales pueden trabajar, de tal forma que puedes colaborar simultáneamente con diferentes grupos de personas en distintas maneras dentro del mismo proyecto. Esto permite establecer varios flujos de trabajo
que no son posibles en sistemas centralizados, como pueden ser los modelos
jerárquicos.

29

CHAPTER 1: Inicio - Sobre el Control de Versiones

Una breve historia de Git
Como muchas de las grandes cosas en esta vida, Git comenzó con un poco de
destrucción creativa y una gran polémica.
El kernel de Linux es un proyecto de software de código abierto con un alcance bastante amplio. Durante la mayor parte del mantenimiento del kernel
de Linux (1991-2002), los cambios en el software se realizaban a través de
parches y archivos. En el 2002, el proyecto del kernel de Linux empezó a usar un
DVCS propietario llamado BitKeeper.
En el 2005, la relación entre la comunidad que desarrollaba el kernel de Linux y la compañía que desarrollaba BitKeeper se vino abajo, y la herramienta
dejó de ser ofrecida de manera gratuita. Esto impulsó a la comunidad de desarrollo de Linux (y en particular a Linus Torvalds, el creador de Linux) a desarrollar su propia herramienta basada en algunas de las lecciones que aprendieron
mientras usaban BitKeeper. Algunos de los objetivos del nuevo sistema fueron
los siguientes:
• Velocidad
• Diseño sencillo
• Gran soporte para desarrollo no lineal (miles de ramas paralelas)
• Completamente distribuido
• Capaz de manejar grandes proyectos (como el kernel de Linux) eficientement (velocidad y tamaño de los datos)
Desde su nacimiento en el 2005, Git ha evolucionado y madurado para ser
fácil de usar y conservar sus características iniciales. Es tremendamente rápido,
muy eficiente con grandes proyectos, y tiene un increíble sistema de ramificación (branching) para desarrollo no lineal (véase Chapter 3) (véase el Capítulo
3). FIXME

Fundamentos de Git
Entonces, ¿qué es Git en pocas palabras? Es muy importante entender bien esta
sección, porque si entiendes lo que es Git y los fundamentos de cómo funciona,
probablemente te será mucho más fácil usar Git efectivamente. A medida que
aprendas Git, intenta olvidar todo lo que posiblemente conoces acerca de otros
VCS como Subversion y Perforce. Hacer esto te ayudará a evitar confusiones sutiles a la hora de utilizar la herramienta. Git almacena y maneja la información
de forma muy diferente a esos otros sistemas, a pesar de que su interfaz de
usuario es bastante similar. Comprender esas diferencias evitará que te confundas a la hora de usarlo.

30

Fundamentos de Git

Copias instantáneas, no diferencias
La principal diferencia entre Git y cualquier otro VCS (incluyendo Subversion y
sus amigos) es la forma en la que manejan sus datos. Conceptualmente, la
mayoría de los otros sistemas almacenan la información como una lista de
cambios en los archivos. Estos sistemas (CVS, Subversion, Perforce, Bazaar,
etc.) manejan la información que almacenan como un conjunto de archivos y
las modificaciones hechas a cada uno de ellos a través del tiempo.

FIGURE 1-4
Storing data as
changes to a base
version of each file.

Git no maneja ni almacena sus datos de esta forma. Git maneja sus datos
como un conjunto de copias instantáneas de un sistema de archivos miniatura.
Cada vez que confirmas un cambio, o guardas el estado de tu proyecto en Git,
él básicamente toma una foto del aspecto de todos tus archivos en ese momento, y guarda una referencia a esa copia instantánea. Para ser eficiente, si los archivos no se han modificado Git no almacena el archivo de nuevo, sino un enlace al archivo anterior idéntico que ya tiene almacenado. Git maneja sus datos
como una secuencia de copias instantáneas.

FIGURE 1-5
Storing data as
snapshots of the
project over time.

31

CHAPTER 1: Inicio - Sobre el Control de Versiones

Esta es una diferencia importante entre Git y prácticamente todos los demás
VCS. Hace que Git reconsidere casi todos los aspectos del control de versiones
que muchos de los demás sistemas copiaron de la generación anterior. Esto
hace que Git se parezca más a un sistema de archivos miniatura con algunas
herramientas tremendamente poderosas desarrolladas sobre él, que a un VCS.
Exploraremos algunos de los beneficios que obtienes al modelar tus datos de
esta manera cuando veamos ramificación (branching) en Git en el (véase Chapter 3) (véase el Capítulo 3). FIXME

Casi todas las operaciones son locales
La mayoría de las operaciones en Git sólo necesitan archivos y recursos locales
para funcionar. Por lo general no se necesita información de ningún otro ordenador de tu red. Si estás acostumbrado a un CVCS donde la mayoría de las operaciones tienen el costo adicional del retardo de la red, este aspecto de Git te
va a hacer pensar que los dioses de la velocidad han bendecido Git con poderes
sobrenaturales. Debido a que tienes toda la historia del proyecto ahí mismo, en
tu disco local, la mayoría de las operaciones parecen prácticamente inmediatas.
Por ejemplo, para navegar por la historia del proyecto, Git no necesita conectarse al servidor para obtener la historia y mostrártela - simplemente la lee
directamente de tu base de datos local. Esto significa que ves la historia del
proyecto casi instantáneamente. Si quieres ver los cambios introducidos en un
archivo entre la versión actual y la de hace un mes, Git puede buscar el archivo
hace un mes y hacer un cálculo de diferencias localmente, en lugar de tener
que pedirle a un servidor remoto que lo haga u obtener una versión antigua
desde la red y hacerlo de manera local.
Esto también significa que hay muy poco que no puedes hacer si estás desconectado o sin VPN. Si te subes a un avión o a un tren y quieres trabajar un
poco, puedes confirmar tus cambios felizmente hasta que consigas una conexión de red para subirlos. Si te vas a casa y no consigues que tu cliente VPN funcione correctamente, puedes seguir trabajando. En muchos otros sistemas, esto es imposible o muy engorroso. En Perforce, por ejemplo, no puedes hacer
mucho cuando no estás conectado al servidor. En Subversion y CVS, puedes editar archivos, pero no puedes confirmar los cambios a tu base de datos (porque
tu base de datos no tiene conexión). Esto puede no parecer gran cosa, pero te
sorprendería la diferencia que puede suponer.

32

Fundamentos de Git

Git tiene integridad
Todo en Git es verificado mediante una suma de comprobación (checksum en
inglés) antes de ser almacenado, y es identificado a partir de ese momento mediante dicha suma. Esto significa que es imposible cambiar los contenidos de
cualquier archivo o directorio sin que Git lo sepa. Esta funcionalidad está integrada en Git al más bajo nivel y es parte integral de su filosofía. No puedes
perder información durante su transmisión o sufrir corrupción de archivos sin
que Git sea capaz de detectarlo.
El mecanismo que usa Git para generar esta suma de comprobación se conoce como hash SHA-1. Se trata de una cadena de 40 caracteres hexadecimales
(0-9 y a-f), y se calcula en base a los contenidos del archivo o estructura del directorio en Git. Un hash SHA-1 se ve de la siguiente forma:
24b9da6552252987aa493b52f8696cd6d3b00373

Verás estos valores hash por todos lados en Git porque son usados con mucha frecuencia. De hecho, Git guarda todo no por nombre de archivo, sino por
el valor hash de sus contenidos.

Git generalmente solo añade información
Cuando realizas acciones en Git, casi todas ellas solo añaden información a la
base de datos de Git. Es muy difícil conseguir que el sistema haga algo que no
se pueda enmendar, o que de algún modo borre información. Como en cualquier VCS, puedes perder o estropear cambios que no has confirmado todavía.
Pero después de confirmar una copia instantánea en Git es muy difícil de perderla, especialmente si envías tu base de datos a otro repositorio con regularidad.
Esto hace que usar Git sea un placer, porque sabemos que podemos experimentar sin peligro de estropear gravemente las cosas. Para un análisis más exhaustivo de cómo almacena Git su información y cómo puedes recuperar datos
aparentemente perdidos, ver “Deshacer Cosas” Capítulo 2. FIXME

Los Tres Estados
Ahora presta atención. Esto es lo más importante qu debes recordar acerca de
Git si quieres que el resto de tu proceso de aprendizaje prosiga sin problemas.
Git tiene tres estados principales en los que se pueden encontrar tus archivos:
confirmado (committed), modificado (modified), y preparado (staged). Confirmado significa que los datos están almacenados de manera segura en tu base
de datos local. Modificado significa que has modificado el archivo pero todavía

33

CHAPTER 1: Inicio - Sobre el Control de Versiones

no lo has confirmado a tu base de datos. Preparado significa que has marcado
un archivo modificado en su versión actual para que vaya en tu próxima confirmación.
Esto nos lleva a las tres secciones principales de un proyecto de Git: El directorio de Git (Git directory), el directorio de trabajo (working directory), y el área
de preparación (staging area).

FIGURE 1-6
Working directory,
staging area, and Git
directory.

El directorio de Git es donde se almacenan los metadatos y la base de datos
de objetos para tu proyecto. Es la parte más importante de Git, y es lo que se
copia cuando clonas un repositorio desde otra computadora.
El directorio de trabajo es una copia de una versión del proyecto. Estos archivos se sacan de la base de datos comprimida en el directorio de Git, y se colocan en disco para que los puedas usar o modificar.
El área de preparación es un archivo, generalmente contenido en tu directorio de Git, que almacena información acerca de lo que va a ir en tu próxima confirmación. A veces se le denomina índice (“index”), pero se está convirtiendo en
estándar el referirse a ella como el área de preparación.
El flujo de trabajo básico en Git es algo así:
1. Modificas una serie de archivos en tu directorio de trabajo.
2. Preparas los archivos, añadiéndolos a tu área de preparación.
3. Confirmas los cambios, lo que toma los archivos tal y como están en el
área de preparación y almacena esa copia instantánea de manera permanente en tu directorio de Git.

34

La Línea de Comandos

Si una versión concreta de un archivo está en el directorio de Git, se considera confirmada (committed). Si ha sufrido cambios desde que se obtuvo del repositorio, pero ha sido añadida al área de preparación, está preparada (staged).
Y si ha sufrido cambios desde que se obtuvo del repositorio, pero no se ha preparado, está modificada (modified). En el Chapter 2 Capítulo 2 aprenderás
más acerca de estos estados y de cómo puedes aprovecharlos o saltarte toda la
parte de preparación.

La Línea de Comandos
Existen muchas formas de usar Git. Por un lado tenemos las herramientas originales de línea de comandos, y por otro lado tenemos una gran variedad de interfaces de usuario con distintas capacidades. En ese libro vamos a utilizar Git
desde la línea de comandos. La línea de comandos en el único lugar en donde
puedes ejecutar todos los comandos de Git - la mayoría de interfaces gráficas
de usuario solo implementan una parte de las características de Git por motivos de simplicidad. Si tú sabes cómo realizar algo desde la línea de comandos,
seguramente serás capaz de averiguar cómo hacer lo mismo desde una interfaz
gráfica. Sin embargo, la relación opuesta no es necesariamente cierta. Así mismo, la decisión de qué cliente gráfico utilizar depende totalmente de tu gusto,
pero todos los usuarios tendrán las herramientas de línea de comandos instaladas y disponibles.
Nosotros esperamos que sepas cómo abrir el Terminal en Mac, o el “Command Prompt” o “Powershell” en Windows. Si no entiendes de lo que estamos
hablando aquí, te recomendamos que hagas una pausa para investigar acerca
de esto de tal forma que puedas entender el resto de las explicaciones y descripciones que siguen en este libro.

Instalación de Git
Antes de empezar a utilizar Git, tienes que instalarlo en tu computadora. Incluso si ya está instalado, este es posiblemente un buen momento para actualizarlo a su última versión. Puedes instalarlo como un paquete, a partir de un archivo instalador, o bajando el código fuente y compilándolo tú mismo.
Este libro fue escrito utilizando la versión 2.0.0 de Git. Aun cuando la
mayoría de comandos que usaremos deben funcionar en versiones más
antiguas de Git, es posible que algunos de ellos no funcionen o funcionen
ligeramente diferente si estás utilizando una versión anterior de Git. Debido a que Git es particularmente bueno en preservar compatibilidad hacia atrás, cualquier versión posterior a 2.0 debe funcionar bien.

35

CHAPTER 1: Inicio - Sobre el Control de Versiones

Instalación en Linux
Si quieres instalar Git en Linux a través de un instalador binario, en general
puedes hacerlo mediante la herramienta básica de administración de paquetes
que trae tu distribución. Si estás en Fedora por ejemplo, puedes usar yum:
$ yum install git

Si estás en una distribución basada en Debian como Ubuntu, puedes usar
apt-get:
If you’re on a Debian-based distribution like Ubuntu, try apt-get:
$ apt-get install git

Para opciones adicionales, la página web de Git tiene instrucciones para la
instalación en diferentes tipos de Unix. Puedes encontrar esta información en
http://git-scm.com/download/linux.

Instalación en Mac
Hay varias maneras de instalar Git en un Mac. Probablemente la más sencilla es
instalando las herramientas Xcode de Línea de Comandos. En Mavericks (10.9)
o superior puedes hacer esto desde el Terminal si intentas ejecutar git por primera vez. Si no lo tienes instalado, te preguntará si deseas instalarlo.
Si deseas una versión más actualizada, puedes hacerlo partir de un instalador binario. Un instalador de Git para OSX es mantenido en la página web de
Git. Lo puedes descargar en http://git-scm.com/download/mac.

36

Instalación de Git

FIGURE 1-7
Git OS X Installer.

También puedes instalarlo como parte del instalador de Github para Mac. Su
interfaz gráfica de usuario tiene la opción de instalar las herramientas de línea
de comandos. Puedes descargar esa herramienta desde el sitio web de Github
para Mar en http://mac.github.com.

Instalación en Windows
También hay varias maneras de instalar Git en Windows. La forma más oficial
está disponible para ser descargada en el sitio web de Git. Solo tienes que visitar http://git-scm.com/download/win y la descarga empezará automáticamente. Fíjate que éste es un proyecto conocido como Git para Windows (también llamado msysGit), el cual es diferente de Git. Para más información acerca
de este proyecto visita http://msysgit.github.io/.
Otra forma de obtener Git fácilmente es mediante la instalación de GitHub
para Windows. El instalador incluye la versión de línea de comandos y la interfaz de usuario de Git. Además funciona bien con Powershell y establece correctamente “caching” de credenciales y configuración CRLF adecuada. Aprenderemos acerca de todas estas cosas un poco más adelante, pero por ahora es suficiente mencionar que éstas son cosas que deseas. Puedes descargar este instalador del sitio web de GitHub para Windows en http://windows.github.com.

37

CHAPTER 1: Inicio - Sobre el Control de Versiones

Instalación a partir del Código Fuente
Algunas personas desean instalar Git a partir de su código fuente debido a que
obtendrás una versión más reciente. Los instaladores binarios tienen a estar un
poco atrasados. Sin embargo, esto ha hecho muy poca diferencia a medida que
Git ha madurado en los últimos años.
Para instalar Git desde el código fuente necesitas tener las siguientes librerías de las que Git depende: curl, zlib, openssl, expat y libiconv. Por ejemplo, si
estás en un sistema que tiene yum (como Fedora) o apt-get (como un sistema
basado en Debian), puedes usar estos comandos para instalar todas las dependencias:
$ yum install curl-devel expat-devel gettext-devel \
openssl-devel zlib-devel
$ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \
libz-dev libssl-dev

Cuando tengas todas las dependencias necesarias, puedes descargar la versión más reciente de Git en diferentes sitios. Puedes obtenerlo a partir del sitio
Kernel.org en https://www.kernel.org/pub/software/scm/git, o su “mirror” en
el sitio web de GitHub en https://github.com/git/git/releases. Generalmente la
más reciente versión en la página web de GitHub es un poco mejor, pero la página de kernel.org también tiene ediciones con firma en caso de que desees verificar tu descarga.
Luego tienes que compilar e instalar de la siguiente manera:
$
$
$
$
$
$

tar -zxf git-2.0.0.tar.gz
cd git-2.0.0
make configure
./configure --prefix=/usr
make all doc info
sudo make install install-doc install-html install-info

Una vez hecho esto, también puedes obtener Git, a través del propio Git,
para futuras actualizaciones:
$ git clone git://git.kernel.org/pub/scm/git/git.git

Configurando Git por primera vez
Ahora que tienes Git en tu sistema, vas a querer hacer algunas cosas para personalizar tu entorno de Git. Es necesario hacer estas cosas solamente una vez
en tu computadora, y se mantendrán entre actualizaciones. También puedes

38

Configurando Git por primera vez

cambiarlas en cualquier momento volviendo a ejecutar los comandos correspondientes.
Git trae una herramienta llamada git config que te permite obtener y establecer variables de configuración que controlan el aspecto y funcionamiento
de Git. Estas variables pueden almacenarse en tres sitios distintos:
1. Archivo /etc/gitconfig: Contiene valores para todos los usuarios del
sistema y todos sus repositorios. Si pasas la opción --system a git
config, lee y escribe específicamente en este archivo.
2. Archivo ~/.gitconfig o ~/.config/git/config: Este archivo es específico a tu usuario. Puedes hacer que Git lea y escriba específicamente en
este archivo pasando la opción --global.
3. Archivo config en el directorio de Git (es decir, .git/config) del repositorio que estés utilizando actualmente: Este archivo es específico al repositorio actual.
Cada nivel sobrescribe los valores del nivel anterior, por lo que los valores
de .git/config tienen preferencia sobre los de /etc/gitconfig.
En sistemas Windows, Git busca el archivo .gitconfig en el directorio
$HOME (para mucha gente será (C:\Users\$USER). También busca el archivo /etc/gitconfig, aunque esta ruta es relativa a la raíz MSys, que es donde
decidiste instalar Git en tu sistema Windows cuando ejecutaste el instalador.

Tu Identidad
Lo primero que deberás hacer cuando instales Git es establecer tu nombre de
usuario y dirección de correo electrónico. Esto es importante porque los “commits” de Git usan esta información, y es introducida de manera inmutable en
los commits que envías:
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

De nuevo, solo necesitas hacer esto una vez si especificas la opción -global, ya que Git siempre usará esta información para todo lo que hagas en
ese sistema. Si quieres sobrescribir esta información con otro nombre o dirección de correo para proyectos específicos, puedes ejecutar el comando sin la
opción --global cuando estés en ese proyecto.
Muchas de las herramientas de interfaz gráfica te ayudarán a hacer esto la
primera vez que las uses.

39

CHAPTER 1: Inicio - Sobre el Control de Versiones

Tu Editor
Ahora que tu identidad está configurada, puedes elegir el editor de texto por
defecto que se utilizará cuando Git necesite que introduzcas un mensaje. Si no
indicas nada, Git usa el editor por defecto de tu sistema, que generalmente es
Vim. Si quieres usar otro editor de texto como Emacs, puedes hacer lo siguiente:
$ git config --global core.editor emacs
EXAMPLE 1-1.

Vim y Emacs son editores de texto frecuentemente usados por desarrolladores en sistemas basados en Unix como Linux y Mac. Si no estás familiarizado
con ninguno de estos editores o estás en un sistema Windows, es posible que
necesites buscar instrucciones acerca de cómo configurar tu editor favorito con
Git. Si no configuras un editor así y no conoces acerca de Vim o Emacs, es muy
factible que termines en un estado bastante confuso en el momento en que
sean ejecutados.

Comprobando tu Configuración
Si quieres comprobar tu configuración, puedes usar el comando git config
--list para mostrar todas las propiedades que Git ha configurado:
$ git config --list
user.name=John Doe
user.email=johndoe@example.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
...

Puede que veas claves repetidas, porque Git lee la misma clave de distintos
archivos (/etc/gitconfig y ~/.gitconfig, por ejemplo). En ese caso, Git
usa el último valor para cada clave única que ve.
También puedes comprobar qué valor que Git utilizará para una clave específica ejecutando git config :

40

¿Cómo obtener ayuda?

$ git config user.name
John Doe

¿Cómo obtener ayuda?
Si alguna vez necesitas ayuda usando Git, existen tres formas de ver la página
del manual (manpage) para cualquier comando de Git:
$ git help 
$ git  --help
$ man git-

Por ejemplo, puedes ver la página del manual para el comando config ejecutando
$ git help config

Estos comandos son muy útiles porque puedes acceder a ellos desde cualquier sitio, incluso sin conexión. Si las páginas del manual y este libro no son
suficientes y necesitas que te ayude una persona, puedes probar en los canales
#git o #github del servidor de IRC Freenode (irc.freenode.net). Estos canales están llenos de cientos de personas que conocen muy bien Git y suelen estar dispuestos a ayudar.

Resumen
Para este momento debes tener una comprensión básica de lo que es Git, y de
cómo se diferencia de cualquier otro sistema de control de versiones centralizado que pudieras haber utilizado previamente. De igual manera, Git debe estar
funcionando en tu sistema y configurado con tu identidad personal. Es hora de
aprender los fundamentos de Git.

41

Fundamentos de Git

2

Si pudieras leer solo un capítulo para empezar a trabajar con Git, este es el capítulo que debes leer. Este capítulo cubre todos los comandos básicos que necesitas para hacer la gran mayoría de cosas a las que eventualmente vas a dedicar tu tiempo mientras trabajas con Git. Al final del capítulo, deberás ser capaz
de configurar e inicializar un repositorio, comenzar y detener el seguimiento de
archivos, y preparar (stage) y confirmar (commit) cambios. También te enseñaremos a configurar Git para que ignore ciertos archivos y patrones, cómo enmendar errores rápida y fácilmente, cómo navegar por la historia de tu proyecto y ver cambios entre confirmaciones, y cómo enviar (push) y recibir (pull) de
repositorios remotos.

Obteniendo un repositorio Git
Puedes obtener un proyecto Git de dos maneras. La primera es tomar un
proyecto o directorio existente e importarlo en Git. La segunda es clonar un repositorio existente en Git desde otro servidor.

Inicializando un repositorio en un directorio existente
Si estás empezando a seguir un proyecto existente en Git, debes ir al directorio
del proyecto y usar el siguiente comando:
$ git init

Esto crea un subdirectorio nuevo llamado .git, el cual contiene todos los
archivos necesarios del repositorio – un esqueleto de un repositorio de Git. Todavía no hay nada en tu proyecto que esté bajo seguimiento. Puedes revisar
Chapter 10 para obtener más información acerca de los archivos presentes en
el directorio .git que acaba de ser creado.

43

CHAPTER 2: Fundamentos de Git

Si deseas empezar a controlar versiones de archivos existentes (a diferencia
de un directorio vacío), probablemente deberías comenzar el seguimiento de
esos archivos y hacer una confirmación inicial. Puedes conseguirlo con unos
pocos comandos git add para especificar qué archivos quieres controlar, seguidos de un git commit para confirmar los cambios:
$ git add *.c
$ git add LICENSE
$ git commit -m 'initial project version'

Veremos lo que hacen estos comandos más adelante. En este momento,
tienes un repositorio de Git con archivos bajo seguimiento y una confirmación
inicial.

Clonando un repositorio existente
Si deseas obtener una copia de un repositorio Git existente — por ejemplo, un
proyecto en el que te gustaría contribuir — el comando que necesitas es git
clone. Si estás familizarizado con otros sistemas de control de versiones como
Subversion, verás que el comando es “clone” en vez de “checkout”. Es una distinción importante, ya que Git recibe una copia de casi todos los datos que
tiene el servidor. Cada versión de cada archivo de la historia del proyecto es
descargada por defecto cuando ejecutas git clone. De hecho, si el disco de tu
servidor se corrompe, puedes usar cualquiera de los clones en cualquiera de
los clientes para devolver al servidor al estado en el que estaba cuando fue clonado (puede que pierdas algunos hooks del lado del servidor y demás, pero toda la información acerca de las versiones estará ahí) — véase “Configurando
Git en un servidor” para más detalles.
Puedes clonar un repositorio con git clone [url]. Por ejemplo, si
quieres clonar la librería de Git llamada libgit2 puedes hacer algo así:
$ git clone https://github.com/libgit2/libgit2

Esto crea un directorio llamado libgit2, inicializa un directorio .git en su
interior, descarga toda la información de ese repositorio y saca una copia de
trabajo de la última versión. Si te metes en el directorio libgit2, verás que están los archivos del proyecto listos para ser utilizados. Si quieres clonar el repositorio a un directorio con otro nombre que no sea libgit2, puedes especificarlo con la siguiente opción de línea de comandos:

44

Guardando cambios en el Repositorio

$ git clone https://github.com/libgit2/libgit2 mylibgit

Ese comando hace lo mismo que el anterior, pero el directorio de destino se
llamará mylibgit.
Git te permite usar distintos protocolos de transferencia. El ejemplo anterior
usa el protocolo https://, pero también puedes utilizar git:// o usuario@servidor:ruta/del/repositorio.git que utiliza el protocolo de transferencia SSH. En “Configurando Git en un servidor” se explicarán todas las
opciones disponibles a la hora de configurar el acceso a tu repositorio de Git, y
las ventajas e inconvenientes de cada una.

Guardando cambios en el Repositorio
Ya tienes un repositorio Git y un checkout o copia de trabajo de los archivos de
dicho proyecto. El siguiente paso es realizar algunos cambios y confirmar instantáneas de esos cambios en el repositorio cada vez que el proyecto alcance
un estado que quieras conservar.
Recuerda que cada archivo de tu repositorio puede tener dos estados: rastreados y sin rastrear. Los archivos rastreados (tracked files en inglés) son todos
aquellos archivos que estaban en la última instantánea del proyecto; pueden
ser archivos sin modificar, modificados o preparados. Los archivos sin rastrear
son todos los demás - cualquier otro archivo en tu directorio de trabajo que no
estaba en tu última instantánea y que no están en el área de preparación (staging area). Cuando clonas por primera vez un repositorio, todos tus archivos estarán rastreados y sin modificar pues acabas de sacarlos y aun no han sido editados.
Mientras editas archivos, Git los ve como modificados, pues han sido cambiados desde su último commit. Luego preparas estos archivos modificados y
finalmente confirmas todos los cambios preparados, y repites el ciclo.

45

CHAPTER 2: Fundamentos de Git

FIGURE 2-1
El ciclo de vida del
estado de tus
archivos.

Revisando el Estado de tus Archivos
La herramienta principal para determinar qué archivos están en qué estado es
el comando git status. Si ejecutas este comando inmediatamente después
de clonar un repositorio, deberías ver algo como esto:
$ git status
On branch master
nothing to commit, working directory clean

Esto significa que tienes un directorio de trabajo limpio - en otras palabras,
que no hay archivos rastreados y modificados. Además, Git no encuentra ningún archivo sin rastrear, de lo contrario aparecerían listados aquí. Finalmente,
el comando te indica en cuál rama estás y te informa que no ha variado con
respecto a la misma rama en el servidor. Por ahora, la rama siempre será “master”, que es la rama por defecto; no le prestaremos atención ahora. Chapter 3
tratará en detalle las ramas y las referencias.
Supongamos que añades un nuevo archivo a tu proyecto, un simple README. Si el archivo no existía antes, y ejecutas git status, verás el archivo sin
rastrear de la siguiente manera:
$ echo 'My Project' > README
$ git status
On branch master
Untracked files:
(use "git add ..." to include in what will be committed)
README

46

Guardando cambios en el Repositorio

nothing added to commit but untracked files present (use "git add" to track)

Puedes ver que el archivo README está sin rastrear porque aparece debajo
del encabezado “Untracked files” (“Archivos no rastreados” en inglés) en la salida. Sin rastrear significa que Git ve archivos que no tenías en el commit anterior.
Git no los incluirá en tu próximo commit a menos que se lo indiques explícitamente. Se comporta así para evitar incluir accidentalmente archivos binarios o
cualquier otro archivo que no quieras incluir. Como tú sí quieres incluir README, debes comenzar a rastrearlo.

Rastrear Archivos Nuevos
Para comenzar a rastrear un archivo debes usar el comando git add. Para comenzar a rastrear el archivo README, puedes ejecutar lo siguiente:
$ git add README

Ahora si vuelves a ver el estado del proyecto, verás que el archivo README
está siendo rastreado y está preparado para ser confirmado:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file:

README

Puedes ver que está siendo rastreado porque aparece luego del encabezado
“Changes to be committed” (“Cambios a ser confirmados” en inglés). Si confirmas en este punto, se guardará en el historial la versión del archivo correspondiente al instante en que ejecutaste git add. Anteriormente cuando ejecutaste git init, ejecutaste luego git add (files) - lo cual inició el rastreo
de archivos en tu directorio. El comando git add puede recibir tanto una ruta
de archivo como de un directorio; si es de un directorio, el comando añade recursivamente los archivos que están dentro de él.

47

CHAPTER 2: Fundamentos de Git

Preparar Archivos Modificados
Vamos a cambiar un archivo que esté rastreado. Si cambias el archivo rastreado
llamado “CONTRIBUTING.md” y luego ejecutas el comando git status , verás
algo parecido a esto:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file:

README

Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified:

CONTRIBUTING.md

El archivo “CONTRIBUTING.md” aparece en una sección llamada “Changes
not staged for commit” (“Cambios no preparado para confirmar” en inglés) - lo
que significa que existe un archivo rastreado que ha sido modificado en el directorio de trabajo pero que aun no está preparado. Para prepararlo, ejecutas el
comando git add . git add es un comando que cumple varios propósitos - lo
usas para empezar a rastrear archivos nuevos, preparar archivos, y hacer otras
cosas como marcar como resuelto archivos en conflicto por combinación. Es
más útil que lo veas como un comando para “añadir este contenido a la próxima confirmación” mas que para “añadir este archivo al proyecto”. Ejecutemos
git add para preparar el archivo “CONTRIBUTING.md” y luego ejecutemos
git status:
$ git add CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file:
modified:

README
CONTRIBUTING.md

Ambos archivos están preparados y formarán parte de tu próxima confirmación. En este momento, supongamos que recuerdas que debes hacer un pequeño cambio en CONTRIBUTING.md antes de confirmarlo. Abres de nuevo el archi-

48

Guardando cambios en el Repositorio

vo, lo cambias y ahora estás listos para confirmar. Sin embargo, ejecutemos
git status una vez más:
$ vim CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file:
modified:

README
CONTRIBUTING.md

Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified:

CONTRIBUTING.md

¡¿Pero qué…?! Ahora CONTRIBUTING.md aparece como preparado y como
no preparado. ¿Cómo es posible? Resulta que Git prepara un archivo de acuerdo al estado que tenía cuando ejecutas el comando git add. Si confirmas
ahora, se confirmará la versión de CONTRIBUTING.md que tenías la última vez
que ejecutaste git add y no la versión que ves ahora en tu directorio de trabajo al ejecutar git commit. Si modificas un archivo luego de ejecutar git add,
deberás ejecutar git add de nuevo para preparar la última versión del archivo:
$ git add CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file:
modified:

README
CONTRIBUTING.md

Estatus Abreviado
Si bien es cierto que la salida de git status es bastante explícita, también es
verdad que es muy extensa. Git ofrece una opción para obtener un estatus
abreviado, de manera que puedas ver tus cambios de una forma más compacta. Si ejecutas git status -s o git status --short obtendrás una salida
mucho más simplificada.

49

CHAPTER 2: Fundamentos de Git

$ git status -s
M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt

Los archivos nuevos que no están rastreados tienen un ?? a su lado, los archivos que están preparados tienen una A y los modificados una M. El estado
aparece en dos columnas - la columna de la izquierda indica el estado preparado y la columna de la derecha indica el estado sin preparar. Por ejemplo, en esa
salida, el archivo README está modificado en el directorio de trabajo pero no
está preparado, mientras que lib/simplegit.rb está modificado y preparado. El archivo Rakefile fue modificado, preparado y modificado otra vez por
lo que existen cambios preparados y sin preparar.

Ignorar Archivos
A veces, tendrás algún tipo de archivo que no quieres que Git añada automáticamente o más aun, que ni siquiera quieras que aparezca como no rastreado.
Este suele ser el caso de archivos generados automáticamente como trazas o
archivos creados por tu sistema de construcción. En estos casos, puedes crear
un archivo llamado .gitignore que liste patrones a considerar. Este es un
ejemplo de un archivo .gitignore:
$ cat .gitignore
*.[oa]
*~

La primera línea le indica a Git que ignore cualquier archivo que termine en
“.o” o “.a” - archivos de objeto o librerías que pueden ser producto de compilar
tu código. La segunda línea le indica a Git que ignore todos los archivos que termine con una tilde (~), lo cual es usado por varios editores de texto como
Emacs para marcar archivos temporales. También puedes incluir cosas como
trazas, temporales, o pid directamente; documentación generada automáticamente; etc. Crear un archivo .gitignore antes de comenzar a trabajar es generalmente una buena idea pues así evitas confirmar accidentalmente archivos
que en realidad no quieres incluir en tu repositorio Git.
Las reglas sobre los patrones que puedes incluir en el archivo .gitignore
son las siguientes:

50

Guardando cambios en el Repositorio

• Ignorar las líneas en blanco y aquellas que comiencen con #.
• Aceptar patrones glob estándar.
• Los patrones pueden terminar en barra (/) para especificar un directorio.
• Los patrones pueden negarse si se añade al principio el signo de exclamación (!).
Los patrones glob son una especia de expresión regular simplificada usada
por los terminales. Un asterisco (*) corresponde a cero o más caracteres; [abc]
corresponde a cualquier carácter dentro de los corchetes (en este caso a, b o c);
el signo de interrogación (?) corresponde a un carácter cualquier; y los corchetes sobre caracteres separados por un guión ([0-9]) corresponde a cualquier
carácter entre ellos (en este caso del 0 al 9). También puedes usar dos asteriscos para indicar directorios anidados; a/**/z coincide con a/z, a/b/z,
a/b/c/z, etc.
Aquí puedes ver otro ejemplo de un archivo .gitignore:
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the root TODO file, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .txt files in the doc/ directory
doc/**/*.txt
GitHub mantiene una extensa lista de archivos .gitignore adecuados a
docenas de proyectos y lenguajes en https://github.com/github/gitignore en
caso de que quieras tener un punto de partida para tu proyecto.

Ver los Cambios Preparados y No Preparados
Si el comando git status es muy impreciso para ti - quieres ver exactamente
que ha cambiado, no solo cuáles archivos lo han hecho - puedes usar el comando git diff. Hablaremos sobre git diff más adelante, pero lo usarás pro-

51

CHAPTER 2: Fundamentos de Git

bablemente para responder estas dos preguntas: ¿Qué has cambiado pero aun
no has preparado? y ¿Qué has preparado y está listo para confirmar? A pesar de
que git status responde a estas preguntas de forma muy general listando el
nombre de los archivos, git diff te muestra las líneas exactas que fueron
añadidas y eliminadas, es decir, el parche.
Supongamos que editas y preparas el archivo README de nuevo y luego editas CONTRIBUTING.md pero no lo preparas. Si ejecutas el comando git status, verás algo como esto:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file:

README

Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified:

CONTRIBUTING.md

Para ver qué has cambiado pero aun no has preparado, escribe git diff
sin más parámetros:
$ git diff
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 8ebb991..643e24f 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -65,7 +65,8 @@ branch directly, things can get messy.
Please include a nice description of your changes when you submit your PR;
if we have to read the whole diff to figure out why you're contributing
in the first place, you're less likely to get feedback and have your change
-merged in.
+merged in. Also, split your changes into comprehensive chunks if you patch is
+longer than a dozen lines.
If you are starting to work on a particular area, feel free to submit a PR
that highlights your work in progress (and note in the PR title that it's

Este comando compara lo que tienes en tu directorio de trabajo con lo que
está en el área de preparación. El resultado te indica los cambios que has hecho
pero que aun no has preparado.

52

Guardando cambios en el Repositorio

Si quieres ver lo que has preparado y será incluido en la próxima confirmación, puedes usar git diff --staged. Este comando compara tus cambios
preparados con la última instantánea confirmada.
$ git diff --staged
diff --git a/README b/README
new file mode 100644
index 0000000..03902a1
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+My Project

Es importante resaltar que al llamar a git diff sin parámetros no verás los
cambios desde tu última confirmación - solo verás los cambios que aun no están preparados. Esto puede ser confuso porque si preparas todos tus cambios,
git diff no te devolverá ninguna salida.
Pasemos a otro ejemplo, si preparas el archivo CONTRIBUTING.md y luego lo
editas, puedes usar git diff para ver los cambios en el archivo que están preparados y los cambios que no lo están. Si nuestro ambiente es como este:
$ git add CONTRIBUTING.md
$ echo 'test line' >> CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
modified:

CONTRIBUTING.md

Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified:

CONTRIBUTING.md

Puedes usar git diff para ver qué está sin preparar
$ git diff
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 643e24f..87f08c8 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md

53

CHAPTER 2: Fundamentos de Git

@@ -119,3 +119,4 @@ at the
## Starter Projects

See our [projects list](https://github.com/libgit2/libgit2/blob/development/PROJE
+# test line

y git diff --cached para ver que has preparado hasta ahora (--staged y
--cached son sinónimos):
$ git diff --cached
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 8ebb991..643e24f 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -65,7 +65,8 @@ branch directly, things can get messy.
Please include a nice description of your changes when you submit your PR;
if we have to read the whole diff to figure out why you're contributing
in the first place, you're less likely to get feedback and have your change
-merged in.
+merged in. Also, split your changes into comprehensive chunks if you patch is
+longer than a dozen lines.
If you are starting to work on a particular area, feel free to submit a PR
that highlights your work in progress (and note in the PR title that it's

GIT DIFF COMO HERRAMIENTA EXTERNA
A lo largo del libro, continuaremos usando el comando git diff de distintas maneras. Existe otra forma de ver estas diferencias si prefieres
utilizar una interfaz gráfica u otro programa externo. Si ejecutas git
difftool en vez de git diff, podrás ver los cambios con programas de
este tipo como Araxis, emerge, vimdiff y más. Ejecuta git difftool -tool-help para ver qué tienes disponible en tu sistema.

Confirmar tus Cambios
Ahora que tu área de preparación está como quieres, puedes confirmar tus
cambios. Recuerda que cualquier cosa que no esté preparada - cualquier archivo que hayas creado o modificado y que no hayas agregado con git add desde
su edición - no será confirmado. Se mantendrán como archivos modificados en
tu disco. En este caso, digamos que la última vez que ejecutaste git status
verificaste que todo estaba preparado y que estás listos para confirmar tus
cambios. La forma más sencilla de confirmar es escribiendo git commit:

54

Guardando cambios en el Repositorio

$ git commit

Al hacerlo, arrancará el editor de tu preferencia. (El editor se establece a
través de la variable de ambiente $EDITOR de tu terminal - usualmente es vim o
emacs, aunque puedes configurarlo con el editor que quieras usando el comando git config --global core.editor tal como viste en Chapter 1).
El editor mostrará el siguiente texto (este ejemplo corresponde a una pantalla de Vim):
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#
new file:
README
#
modified:
CONTRIBUTING.md
#
~
~
~
".git/COMMIT_EDITMSG" 9L, 283C

Puedes ver que el mensaje de confirmación por defecto contiene la última
salida del comando git status comentada y una línea vacía encima de ella.
Puedes eliminar estos comentarios y escribir tu mensaje de confirmación, o
puedes dejarlos allí para ayudarte a recordar qué estás confirmando. (Para obtener una forma más explícita de recordar qué has modificado, puedes pasar la
opción -v a git commit. Al hacerlo se incluirá en el editor el diff de tus cambios para que veas exactamente qué cambios estás confirmando.) Cuando
sales del editor, Git crea tu confirmación con tu mensaje (eliminando el texto
comentado y el diff).
Otra alternativa es escribir el mensaje de confirmación directamente en el
comando commit utilizando la opción -m:
$ git commit -m "Story 182: Fix benchmarks for speed"
[master 463dc4f] Story 182: Fix benchmarks for speed
2 files changed, 2 insertions(+)
create mode 100644 README

¡Has creado tu primera confirmación (o commit)! Puedes ver que la confirmación te devuelve una salida descriptiva: indica cuál rama as confirmado
(master), que checksum SHA-1 tiene el commit (463dc4f), cuántos archivos

55

CHAPTER 2: Fundamentos de Git

han cambiado y estadísticas sobre las líneas añadidas y eliminadas en el commit.
Recuerda que la confirmación guarda una instantánea de tu área de preparación. Todo lo que no hayas preparado sigue allí modificado; puedes hacer una
nueva confirmación para añadirlo a tu historial. Cada vez que realizas un commit, guardas una instantánea de tu proyecto la cual puedes usar para comparar
o volver a ella luego.

Saltar el Área de Preparación
A pesar de que puede resultar muy útil para ajustar los commits tal como
quieres, el área de preparación es a veces un paso más complejo a lo que necesitas para tu flujo de trabajo. Si quieres saltarte el área de preparación, Git te
ofrece un atajo sencillo. Añadiendo la opción -a al comando git commit harás
que Git prepare automáticamente todos los archivos rastreados antes de confirmarlos, ahorrándote el paso de git add:
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified:

CONTRIBUTING.md

no changes added to commit (use "git add" and/or "git commit -a")
$ git commit -a -m 'added new benchmarks'
[master 83e38c7] added new benchmarks
1 file changed, 5 insertions(+), 0 deletions(-)

Fíjate que en este caso no fue necesario ejecutar git add sobre el archivo

CONTRIBUTING.md antes de confirmar.

Eliminar Archivos
Para eliminar archivos de Git, debes eliminarlos de tus archivos rastreados (o
mejor dicho, eliminarlos del área de preparación) y luego confirmar. Para ello
existe el comando git rm, que además elimina el archivo de tu directorio de
trabajo de manera que no aparezca la próxima vez como un archivo no rastreado.

56

Guardando cambios en el Repositorio

Si simplemente eliminas el archivo de tu directorio de trabajo, aparecerá en
la sección “Changes not staged for commit” (esto es, sin preparar) en la salida
de git status:
$ rm PROJECTS.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted:

PROJECTS.md

no changes added to commit (use "git add" and/or "git commit -a")

Ahora, si ejecutas git rm, entonces se prepara la eliminación del archivo:
$ git rm PROJECTS.md
rm 'PROJECTS.md'
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
deleted:

PROJECTS.md

Con la próxima confirmación, el archivo habrá desaparecido y no volverá a
ser rastreado. Si modificaste el archivo y ya lo habías añadido al índice, tendrás
que forzar su eliminación con la opción -f. Esta propiedad existe por seguridad, para prevenir que elimines accidentalmente datos que aun no han sido
guardados como una instantánea y que por lo tanto no podrás recuperar luego
con Git.
Otra cosa que puedas querer hacer es mantener el archivo en tu directorio
de trabajo pero eliminarlo del área de preparación. En otras palabras, quisieras
mantener el archivo en tu disco duro pero sin que Git lo siga rastreando. Esto
puede ser particularmente útil si olvidaste añadir algo en tu archivo .gitignore y lo preparaste accidentalmente, algo como un gran archivo de trazas
a un montón de archivos compilados .a. Para hacerlo, utiliza la opción -cached:
$ git rm --cached README

57

CHAPTER 2: Fundamentos de Git

Al comando git rm puedes pasarle archivos, directorios y patrones glob. Lo
que significa que puedes hacer cosas como
$ git rm log/\*.log

Fíjate en la barra invertida (\) antes del asterisco *. Esto es necesario porque
Git hace su propia expansión de nombres de archivo, aparte de la expansión
hecha por tu terminal. Este comando elimina todos los archivo que tengan la
extensión .log dentro del directorio log/. O también puedes hacer algo como:
$ git rm \*~

Este comando elimina todos los archivos que acaben con ~.

Cambiar el Nombre de los Archivos
Al contrario que muchos sistemas VCS, Git no rastrea explícitamente los cambios de nombre en archivos. Si renombras un archivo en Git, no se guardará
ningún metadato que indique que renombraste el archivo. Sin embargo, Git es
bastante listo como para detectar estos cambios luego que los has hecho - más
adelante, veremos cómo se detecta el cambio de nombre.
Por esto, resulta confuso que Git tenga un comando mv. Si quieres renombrar un archivo en Git, puedes ejecutar algo como
$ git mv file_from file_to

y funcionará bien. De hecho, si ejecutas algo como eso y ves el estatus, verás
que Git lo considera como un renombramiento de archivo:
$ git mv README.md README
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
renamed:

README.md -> README

Sin embargo, eso es equivalente a ejecutar algo como esto:

58

Ver el Historial de Confirmaciones

$ mv README.md README
$ git rm README.md
$ git add README

Git se da cuenta que es un renombramiento implícito, así que no importa si
renombras el archivo de esa manera o a través del comando mv. La única diferencia real es que mv es un solo comando en vez de tres - existe por conveniencia. De hecho, puedes usar la herramienta que quieras para renombrar un archivo y luego realizar el proceso rm/add antes de confirmar.

Ver el Historial de Confirmaciones
Después de haber hecho varias confirmaciones, o si has clonado un repositorio
que ya tenía un histórico de confirmaciones, probablemente quieras mirar atrás
para ver qué modificaciones se han llevado a cabo. La herramienta más básica
y potente para hacer esto es el comando git log.
Estos ejemplos usan un proyecto muy sencillo llamado “simplegit”. Para clonar el proyecto, ejecuta:
git clone https://github.com/schacon/simplegit-progit

Cuando ejecutes git log sobre este proyecto, deberías ver una salida similar a esta:
$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon 
Date:
Mon Mar 17 21:52:11 2008 -0700
changed the version number
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon 
Date:
Sat Mar 15 16:40:33 2008 -0700
removed unnecessary test
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon 
Date:
Sat Mar 15 10:31:28 2008 -0700

59

CHAPTER 2: Fundamentos de Git

first commit

Por defecto, si no pasas ningún parámetro, git log lista las confirmaciones
hechas sobre ese repositorio en orden cronológico inverso. Es decir, las confirmaciones más recientes se muestran al principio. Como puedes ver, este comando lista cada confirmación con su suma de comprobación SHA-1, el nombre y dirección de correo del autor, la fecha y el mensaje de confirmación.
El comando git log proporciona gran cantidad de opciones para mostrarte exactamente lo que buscas. Aquí veremos algunas de las más usadas.
Una de las opciones más útiles es -p, que muestra las diferencias introducidas en cada confirmación. También puedes usar la opción -2, que hace que se
muestren únicamente las dos últimas entradas del historial:
$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon 
Date:
Mon Mar 17 21:52:11 2008 -0700
changed the version number
diff --git a/Rakefile b/Rakefile
index a874b73..8f94139 100644
--- a/Rakefile
+++ b/Rakefile
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
spec = Gem::Specification.new do |s|
s.platform =
Gem::Platform::RUBY
s.name
=
"simplegit"
s.version
=
"0.1.0"
+
s.version
=
"0.1.1"
s.author
=
"Scott Chacon"
s.email
=
"schacon@gee-mail.com"
s.summary
=
"A simple gem for using Git in Ruby code."
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon 
Date:
Sat Mar 15 16:40:33 2008 -0700
removed unnecessary test
diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index a0a60ae..47c6340 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -18,8 +18,3 @@ class SimpleGit

60

Ver el Historial de Confirmaciones

end
end
-if $0 == __FILE__
- git = SimpleGit.new
- puts git.show
-end
\ No newline at end of file

Esta opción muestra la misma información, pero añadiendo tras cada entrada las diferencias que le corresponden. Esto resulta muy útil para revisiones de
código, o para visualizar rápidamente lo que ha pasado en las confirmaciones
enviadas por un colaborador. También puedes usar con git log una serie de
opciones de resumen. Por ejemplo, si quieres ver algunas estadísticas de cada
confirmación, puedes usar la opción --stat:
$ git log --stat
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon 
Date:
Mon Mar 17 21:52:11 2008 -0700
changed the version number
Rakefile | 2 +1 file changed, 1 insertion(+), 1 deletion(-)
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon 
Date:
Sat Mar 15 16:40:33 2008 -0700
removed unnecessary test
lib/simplegit.rb | 5 ----1 file changed, 5 deletions(-)
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon 
Date:
Sat Mar 15 10:31:28 2008 -0700
first commit
README
Rakefile
lib/simplegit.rb
3 files changed,

| 6 ++++++
| 23 +++++++++++++++++++++++
| 25 +++++++++++++++++++++++++
54 insertions(+)

61

CHAPTER 2: Fundamentos de Git

Como puedes ver, la opción --stat imprime tras cada confirmación una lista de archivos modificados, indicando cuántos han sido modificados y cuántas
líneas han sido añadidas y eliminadas para cada uno de ellos, y un resumen de
toda esta información.
Otra opción realmente útil es --pretty, que modifica el formato de la salida. Tienes unos cuantos estilos disponibles. La opción oneline imprime cada
confirmación en una única línea, lo que puede resultar útil si estás analizando
gran cantidad de confirmaciones. Otras opciones son short, full y fuller,
que muestran la salida en un formato parecido, pero añadiendo menos o más
información, respectivamente:
$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 changed the version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test
a11bef06a3f659402fe7563abf99ad00de2209e6 first commit

La opción más interesante es format, que te permite especificar tu propio
formato. Esto resulta especialmente útil si estás generando una salida para que
sea analizada por otro programa —como especificas el formato explícitamente,
sabes que no cambiará en futuras actualizaciones de Git—:
$ git log
ca82a6d 085bb3b a11bef0 -

--pretty=format:"%h Scott Chacon, 6 years
Scott Chacon, 6 years
Scott Chacon, 6 years

%an, %ar : %s"
ago : changed the version number
ago : removed unnecessary test
ago : first commit

Table 2-1 lista algunas de las opciones más útiles aceptadas por format.
TABLE 2-1. Opciones útiles de git log --pretty=format

62

Opción

Descripción de la salida

%H

Hash de la confirmación

%h

Hash de la confirmación abreviado

%T

Hash del árbol

%t

Hash del árbol abreviado

%P

Hashes de las confirmaciones padre

%p

Hashes de las confirmaciones padre abreviados

Ver el Historial de Confirmaciones

Opción

Descripción de la salida

%an

Nombre del autor

%ae

Dirección de correo del autor

%ad

Fecha de autoría (el formato respeta la opción -–date)

%ar

Fecha de autoría, relativa

%cn

Nombre del confirmador

%ce

Dirección de correo del confirmador

%cd

Fecha de confirmación

%cr

Fecha de confirmación, relativa

%s

Asunto

Puede que te estés preguntando la diferencia entre autor (author) y confirmador (committer). El autor es la persona que escribió originalmente el trabajo,
mientras que el confirmador es quien lo aplicó. Por tanto, si mandas un parche
a un proyecto, y uno de sus miembros lo aplica, ambos recibiréis reconocimiento —tú como autor, y el miembro del proyecto como confirmador—. Veremos
esta distinción en mayor profundidad en Chapter 5.
Las opciones oneline y format son especialmente útiles combinadas con
otra opción llamada --graph. Ésta añade un pequeño gráfico ASCII mostrando
tu historial de ramificaciones y uniones:
$ git log --pretty=format:"%h %s" --graph
* 2d3acf9 ignore errors from SIGCHLD on trap
* 5e3ee11 Merge branch 'master' of git://github.com/dustin/grit
|\
| * 420eac9 Added a method for getting the current branch.
* | 30e367c timeout code and tests
* | 5a09431 add timeout protection to grit
* | e1193f8 support for heads with slashes in them
|/
* d6016bc require time for xmlschema
* 11d191e Merge branch 'defunkt' into local

Este tipo de salidas serán más interesantes cuando empecemos a hablar sobre ramificaciones y combinaciones en el próximo capítulo.
Éstas son sólo algunas de las opciones para formatear la salida de git log
—existen muchas más. Table 2-2 lista las opciones vistas hasta ahora, y algu-

63

CHAPTER 2: Fundamentos de Git

nas otras opciones de formateo que pueden resultarte útiles, así como su efecto sobre la salida.
TABLE 2-2. Opciones típicas de git log

Opción

Descripción

-p

Muestra el parche introducido en cada confirmación.

--stat

Muestra estadísticas sobre los archivos modificados en cada confirmación.

--shortstat

Muestra solamente la línea de resumen de la opción -stat.

--name-only

Muestra la lista de archivos afectados.

--name-status

Muestra la lista de archivos afectados, indicando además si
fueron añadidos, modificados o eliminados.
Muestra solamente los primeros caracteres de la suma

--abbrev-commit SHA-1, en vez de los 40 caracteres de que se compone.

Muestra la fecha en formato relativo (por ejemplo, “2 weeks

--relative-date ago” (“hace 2 semanas”)) en lugar del formato completo.
--graph

Muestra un gráfico ASCII con la historia de ramificaciones y
uniones.

--pretty

Muestra las confirmaciones usando un formato alternativo.
Posibles opciones son oneline, short, full, fuller y format
(mediante el cual puedes especificar tu propio formato).

Limitar la Salida del Historial
Además de las opciones de formateo, git log acepta una serie de opciones
para limitar su salida —es decir, opciones que te permiten mostrar únicamente
parte de las confirmaciones—. Ya has visto una de ellas, la opción -2, que
muestra sólo las dos últimas confirmaciones. De hecho, puedes hacer -,
siendo n cualquier entero, para mostrar las últimas n confirmaciones. En realidad es poco probable que uses esto con frecuencia, ya que Git por defecto pagina su salida para que veas cada página del historial por separado.
Sin embargo, las opciones temporales como --since (desde) y --until
(hasta) sí que resultan muy útiles. Por ejemplo, este comando lista todas las
confirmaciones hechas durante las dos últimas semanas:
$ git log --since=2.weeks

64

Ver el Historial de Confirmaciones

Este comando acepta muchos formatos. Puedes indicar una fecha concreta
("2008-01-15"), o relativa, como "2 years 1 day 3 minutes ago" ("hace
2 años, 1 día y 3 minutos").
También puedes filtrar la lista para que muestre sólo aquellas confirmaciones que cumplen ciertos criterios. La opción --author te permite filtrar por
autor, y --grep te permite buscar palabras clave entre los mensajes de confirmación. (Ten en cuenta que si quieres aplicar ambas opciones simultáneamente, tienes que añadir --all-match, o el comando mostrará las confirmaciones que cumplan cualquiera de las dos, no necesariamente las dos a la vez.)
Otra opción útil es -S, la cual recibe una cadena y solo muestra las confirmaciones que cambiaron el código añadiendo o eliminando la cadena. Por ejemplo, si quieres encontrar la última confirmación que añadió o eliminó una referencia a una función específica, puede ejecutar:
$ git log -Sfunction_name

La última opción verdaderamente útil para filtrar la salida de git log es especificar una ruta. Si especificas la ruta de un directorio o archivo, puedes limitar la salida a aquellas confirmaciones que introdujeron un cambio en dichos
archivos. Ésta debe ser siempre la última opción, y suele ir precedida de dos
guiones (--) para separar la ruta del resto de opciones.
En Table 2-3 se listan estas opciones, y algunas otras bastante comunes, a
modo de referencia.
TABLE 2-3. Opciones para limitar la salida de git log

Opción

Descripción

-(n)

Muestra solamente las últimas n confirmaciones

--since, --after

Muestra aquellas confirmaciones hechas después
de la fecha especificada.

--until, --before

Muestra aquellas confirmaciones hechas antes de
la fecha especificada.

--author

Muestra solo aquellas confirmaciones cuyo autor
coincide con la cadena especificada.

--committer

Muestra solo aquellas confirmaciones cuyo confirmador coincide con la cadena especificada.

-S

Muestra solo aquellas confirmaciones que añadan
o eliminen código que corresponda con la cadena
especificada.

65

CHAPTER 2: Fundamentos de Git

Por ejemplo, si quieres ver cuáles de las confirmaciones hechas sobre archivos de prueba del código fuente de Git fueron enviadas por Junio Hamano, y no
fueron uniones, en el mes de octubre de 2008, ejecutarías algo así:
$ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
--before="2008-11-01" --no-merges -- t/
5610e3b - Fix testcase failure when extended attributes are in use
acd3b9e - Enhance hold_lock_file_for_{update,append}() API
f563754 - demonstrate breakage of detached checkout with symbolic link HEAD
d1a43f2 - reset --hard/read-tree --reset -u: remove unmerged new paths
51a94af - Fix "checkout --track -b newbranch" on detached HEAD
b0ad11e - pull: allow "git pull origin $something:$current_branch" into an unborn

De las casi 40.000 confirmaciones en la historia del código fuente de Git, este
comando muestra las 6 que cumplen estas condiciones.

Deshacer Cosas
En cualquier momento puede que quieras deshacer algo. Aquí repasaremos algunas herramientas básicas usadas para deshacer cambios que hayas hecho.
Ten cuidado, a veces no es posible recuperar algo luego que lo has deshecho.
Esta es una de las pocas áreas en las que Git puede perder parte de tu trabajo si
cometes un error.
Uno de las acciones más comunes a deshacer es cuando confirmas un cambio antes de tiempo y olvidas agregar algún archivo, o te equivocas en el mensaje de confirmación. Si quieres rehacer la confirmación, puedes reconfirmar
con la opción --amend:
$ git commit --amend

Este comando utiliza tu área de preparación para la confirmación. Si no has
hecho cambios desde tu última confirmación (por ejemplo, ejecutas este comando justo después de tu confirmación anterior), entonces la instantánea lucirá exactamente igual, y lo único que cambiarás será el mensaje de confirmación.
Se lanzará el mismo editor de confirmación, pero verás que ya incluye el
mensaje de tu confirmación anterior. Puedes editar el mensaje como siempre y
se sobreescribirá tu confirmación anterior.
Por ejemplo, si confirmas y luego te das cuenta que olvidaste preparar los
cambios de un archivo que querías incluir en esta confirmación, puedes hacer
lo siguiente:

66

Deshacer Cosas

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend

Al final terminarás con una sola confirmación - la segunda confirmación reemplaza el resultado de la primera.

Deshacer un Archivo Preparado
Las siguientes dos secciones demuestran cómo lidiar con los cambios de tu
área de preparación y tú directorio de trabajo. Afortunadamente, el comando
que usas para determinar el estado de esas dos áreas también te recuerda cómo deshacer los cambios en ellas. Por ejemplo, supongamos que has cambiado
dos archivos y que quieres confirmarlos como dos cambios separados, pero accidentalmente has escrito git add * y has preparado ambos. ¿Cómo puedes
sacar del área de preparación uno de ellos? El comando git status te recuerda cómo:
$ git add .
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
renamed:
modified:

README.md -> README
CONTRIBUTING.md

Justo debajo del texto “Changes to be committed” (“Cambios a ser confirmados”, en inglés), verás que dice que uses git reset HEAD ... para
deshacer la preparación. Por lo tanto, usemos el consejo para deshacer la preparación del archivo CONTRIBUTING.md:
$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M
CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
renamed:

README.md -> README

67

CHAPTER 2: Fundamentos de Git

Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified:

CONTRIBUTING.md

El comando es un poco raro, pero funciona. El archivo CONTRIBUTING.md
esta modificado y, nuevamente, no preparado.
A pesar de que git reset puede ser un comando peligroso si lo llamas con

--hard, en este caso el archivo que está en tu directorio de trabajo no se
toca. Ejecutar git reset sin opciones no es peligroso - solo toca el área de
preparación.

Por ahora lo único que necesitas saber sobre el comando git reset es esta
invocación mágica. Entraremos en mucho más detalle sobre qué hace reset y
como dominarlo para que haga cosas realmente interesantes en “Reset Demystified”.

Deshacer un Archivo Modificado
¿Qué tal si te das cuenta que no quieres mantener los cambios del archivo CONTRIBUTING.md? ¿Cómo puedes restaurarlo fácilmente - volver al estado en el
que estaba en la última confirmación (o cuando estaba recién clonado, o como
sea que haya llegado a tu directorio de trabajo)? Afortunadamente, git status también te dice cómo hacerlo. En la salida anterior, el área no preparada
lucía así:
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified:

CONTRIBUTING.md

Allí se te indica explícitamente como descartar los cambios que has hecho.
Hagamos lo que nos dice:
$ git checkout -- CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)

68

Trabajar con Remotos

renamed:

README.md -> README

Ahora puedes ver que los cambios se han revertido.
Es importante entender que git checkout -- [archivo] es un comando
peligroso. Cualquier cambio que le hayas hecho a ese archivo desaparecerá - acabas de sobreescribirlo con otro archivo. Nunca utilices este comando a menos que estés absolutamente seguro de que ya no quieres el
archivo.

Para mantener los cambios que has hecho y a la vez deshacerte del archivo
temporalmente, hablaremos sobre cómo esconder archivos (stashing, en inglés) y sobre ramas en Chapter 3; normalmente, estas son las mejores maneras
de hacerlo.
Recuerda, todo lo que esté confirmado en Git puede recuperarse. Incluso
commits que estuvieron en ramas que han sido eliminadas o commits que fueron sobreescritos con --amend pueden recuperarse (véase “Data Recovery”
para recuperación de datos). Sin embargo, es posible que no vuelvas a ver jamás cualquier cosa que pierdas y que nunca haya sido confirmada.

Trabajar con Remotos
Para poder colaborar en cualquier proyecto Git, necesitas saber cómo gestionar
repositorios remotos. Los repositorios remotos son versiones de tu proyecto
que están hospedadas en Internet en cualquier otra red. Puedes tener varios de
ellos, y en cada uno tendrás generalmente permisos de solo lectura o de lectura
y escritura. Colaborar con otras personas implica gestionar estos repositorios
remotos y enviar y traer datos de ellos cada vez que necesites compartir tu trabajo. Gestionar repositorios remotos incluye saber cómo añadir un repositorio
remoto, eliminar los remotos que ya no son válidos, gestionar varias ramas remotas y definir si deben rastrearse o no, y más. En esta sección, trataremos algunas de estas habilidades de gestión de remotos.

Ver Tus Remotos
Para ver los remotos que tienes configurados, debes ejecutar el comando git
remote. Mostrará los nombres de cada uno de los remotos que tienes especificados. Si has clonado tu repositorio, deberías ver al menos origin (origen, en
inglés) - este es el nombre que por defecto Git le da al servidor del que has clonado:

69

CHAPTER 2: Fundamentos de Git

$ git clone https://github.com/schacon/ticgit
Cloning into 'ticgit'...
remote: Reusing existing pack: 1857, done.
remote: Total 1857 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1857/1857), 374.35 KiB | 268.00 KiB/s, done.
Resolving deltas: 100% (772/772), done.
Checking connectivity... done.
$ cd ticgit
$ git remote
origin

También puedes pasar la opción -v, la cual muestra las URLs que Git ha asociado al nombre y que serán usadas al leer y escribir en ese remoto:
$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)

Si tienes más de un remoto, el comando los listará todos. Por ejemplo, un
repositorio con múltiples remotos para trabajar con distintos colaboradores
podría verse de la siguiente manera.
$ cd grit
$ git remote -v
bakkdoor https://github.com/bakkdoor/grit (fetch)
bakkdoor https://github.com/bakkdoor/grit (push)
cho45
https://github.com/cho45/grit (fetch)
cho45
https://github.com/cho45/grit (push)
defunkt
https://github.com/defunkt/grit (fetch)
defunkt
https://github.com/defunkt/grit (push)
koke
git://github.com/koke/grit.git (fetch)
koke
git://github.com/koke/grit.git (push)
origin
git@github.com:mojombo/grit.git (fetch)
origin
git@github.com:mojombo/grit.git (push)

Esto significa que podemos traer contribuciones de cualquiera de estos
usuarios fácilmente. Es posible que también tengamos permisos para enviar
datos a algunos, aunque no podemos saberlo desde aquí.
Fíjate que estos remotos usan distintos protocolos; hablaremos sobre ello
más adelante, en “Configurando Git en un servidor”.

70

Trabajar con Remotos

Añadir Repositorios Remotos
En secciones anteriores hemos mencionado y dado alguna demostración de
cómo añadir repositorios remotos. Ahora veremos explícitamente cómo hacerlo. Para añadir un remoto nuevo y asociarlo a un nombre que puedas referenciar fácilmente, ejecuta git remote add [nombre] [url]:
$ git remote
origin
$ git remote add pb https://github.com/paulboone/ticgit
$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)
pb
https://github.com/paulboone/ticgit (fetch)
pb
https://github.com/paulboone/ticgit (push)

A partir de ahora puedes usar el nombre pb en la línea de comandos en lugar
de la URL entera. Por ejemplo, si quieres traer toda la información que tiene
Paul pero tú aun no tienes en tu repositorio, puedes ejecutar git fetch pb:
$ git fetch pb
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 43 (delta 10), reused 31 (delta 5)
Unpacking objects: 100% (43/43), done.
From https://github.com/paulboone/ticgit
* [new branch]
master
-> pb/master
* [new branch]
ticgit
-> pb/ticgit

La rama maestra de Paul ahora es accesible localmente con el nombre pb/
master - puedes combinarla con alguna de tus ramas, o puedes crear una rama
local en ese punto si quieres inspeccionarla. (Hablaremos con más detalle acerca de qué son las ramas y cómo utilizarlas en Chapter 3.)

Traer y Combinar Remotos
Como hemos visto hasta ahora, para obtener datos de tus proyectos remotos
puedes ejecutar:
$ git fetch [remote-name]

71

CHAPTER 2: Fundamentos de Git

El comando irá al proyecto remoto y se traerá todos los datos que aun no
tienes de dicho remoto. Luego de hacer esto, tendrás referencias a todas las
ramas del remoto, las cuales puedes combinar e inspeccionar cuando quieras.
Si clonas un repositorio, el comando de clonar automáticamente añade ese
repositorio remoto con el nombre “origin”. Por lo tanto, git fetch origin se
trae todo el trabajo nuevo que ha sido enviado a ese servidor desde que lo clonaste (o desde la última vez que trajiste datos). Es importante destacar que el
comando git fetch solo trae datos a tu repositorio local - ni lo combina automáticamente con tu trabajo ni modifica el trabajo que llevas hecho. La combinación con tu trabajo debes hacerla manualmente cuando estés listo.
Si has configurado una rama para que rastree una rama remota (más información en la siguiente sección y en Chapter 3), puedes usar el comando git
pull para traer y combinar automáticamente la rama remota con tu rama actual. Es posible que este sea un flujo de trabajo mucho más cómodo y fácil para
ti; y por defecto, el comando git clone le indica automáticamente a tu rama
maestra local que rastree la rama maestra remota (o como se llame la rama por
defecto) del servidor del que has clonado. Generalmente, al ejecutar git pull
traerás datos del servidor del que clonaste originalmente y se intentará combinar automáticamente la información con el código en el que estás trabajando.

Enviar a Tus Remotos
Cuando tienes un proyecto que quieres compartir, debes enviarlo a un servidor.
El comando para hacerlo es simple: git push [nombre-remoto] [nombrerama]. Si quieres enviar tu rama master a tu servidor origin (recuerda, clonar
un repositorio establece esos nombres automáticamente), entonces puedes
ejecutar el siguiente comando y se enviarán todos los commits que hayas hecho
al servidor:
$ git push origin master

Este comando solo funciona si clonaste de un servidor sobre el que tienes
permisos de escritura y si nadie más ha enviado datos por el medio. Si alguien
más clona el mismo repositorio que tú y envía información antes que tú, tu envío será rechazado. Tendrás que traerte su trabajo y combinarlo con el tuyo antes de que puedas enviar datos al servidor. Para información más detallada sobre cómo enviar datos a servidores remotos, véase Chapter 3.

72

Trabajar con Remotos

Inspeccionar un Remoto
Si quieres ver más información acerca de un remoto en particular, puedes ejecutar el comando git remote show [nombre-remoto]. Si ejecutas el comando con un nombre en particular, como origin, verás algo como lo siguiente:
$ git remote show origin
* remote origin
Fetch URL: https://github.com/schacon/ticgit
Push URL: https://github.com/schacon/ticgit
HEAD branch: master
Remote branches:
master
tracked
dev-branch
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)

El comando lista la URL del repositorio remoto y la información del rastreo
de ramas. El comando te indica claramente que si estás en la rama maestra y
ejecutas el comando git pull, automáticamente combinará la rama maestra
remota luego de haber traído toda la información de ella. También lista todas
las referencias remotas de las que ha traído datos.
Ejemplos como este son los que te encontrarás normalmente. Sin embargo,
si usas Git de forma más avanzada, puede que obtengas mucha más información de un git remote show:
$ git remote show origin
* remote origin
URL: https://github.com/my-org/complex-project
Fetch URL: https://github.com/my-org/complex-project
Push URL: https://github.com/my-org/complex-project
HEAD branch: master
Remote branches:
master
tracked
dev-branch
tracked
markdown-strip
tracked
issue-43
new (next fetch will store in remotes/origin)
issue-45
new (next fetch will store in remotes/origin)
refs/remotes/origin/issue-11
stale (use 'git remote prune' to remove)
Local branches configured for 'git pull':
dev-branch merges with remote dev-branch
master
merges with remote master

73

CHAPTER 2: Fundamentos de Git

Local refs configured for 'git push':
dev-branch
pushes to dev-branch
markdown-strip
pushes to markdown-strip
master
pushes to master

Este comando te indica a cuál rama enviarás información automáticamente
cada vez que ejecutas git push, dependiendo de la rama en la que estés. También te muestra cuáles ramas remotas no tienes aun, cuáles ramas remotas
tienes que han sido eliminadas del servidor, y varias ramas que serán combinadas automáticamente cuando ejecutes git pull.

Eliminar y Renombrar Remotos
Si quieres cambiar el nombre de la referencia de un remoto puedes ejecutar

git remote rename . Por ejemplo, si quieres cambiar el nombre de pb a paul,
puedes hacerlo con git remote rename:
$ git remote rename pb paul
$ git remote
origin
paul

Es importante destacar que al hacer esto también cambias el nombre de las
ramas remotas. Por lo tanto, lo que antes estaba referenciado como pb/
master ahora lo está como paul/master.
Si por alguna razón quieres eliminar un remoto - has cambiado de servidor o
no quieres seguir utilizando un mirror, o quizás un colaborador a dejado de trabajar en el proyecto - puedes usar git remote rm:
$ git remote rm paul
$ git remote
origin

Etiquetado
Como muchos VCS, Git tiene la posibilidad de etiquetar puntos específicos del
historial como importantes. Esta funcionalidad se usa típicamente para marcar
versiones de lanzamiento (v1.0, por ejemplo). En esta sección, aprenderás cómo listar las etiquetas disponibles, cómo crear nuevas etiquetas y cuáles son
los distintos tipos de etiquetas.

74

(up to
(up to
(up to

Etiquetado

Listar Tus Etiquetas
Listar las etiquetas disponibles en Git es sencillo. Simplemente escribe git

tag:
$ git tag
v0.1
v1.3

Este comando lista las etiquetas en orden alfabético; el orden en el que
aparecen no tiene mayor importancia.
También puedes buscar etiquetas con un patrón particular. El repositorio del
código fuente de Git, por ejemplo, contiene más de 500 etiquetas. Si solo te interesa ver la serie 1.8.5, puedes ejecutar:
$ git tag -l 'v1.8.5*'
v1.8.5
v1.8.5-rc0
v1.8.5-rc1
v1.8.5-rc2
v1.8.5-rc3
v1.8.5.1
v1.8.5.2
v1.8.5.3
v1.8.5.4
v1.8.5.5

Crear Etiquetas
Git utiliza dos tipos principales de etiquetas: ligeras y anotadas.
Una etiqueta ligera es muy parecido a una rama que no cambia - simplemente es un puntero a un commit específico.
Sin embargo, las etiquetas anotadas se guardan en la base de datos de Git
como objetos enteros. Tienen un checksum; contienen el nombre del etiquetador, correo electrónico y fecha; tienen un mensaje asociado; y pueden ser firmadas y verificadas con GNU Privacy Guard (GPG). Normalmente se recomienda
que crees etiquetas anotadas, de manera que tengas toda esta información;
pero si quieres una etiqueta temporal o por alguna razón no estás interesado
en esa información, entonces puedes usar las etiquetas ligeras.

75

CHAPTER 2: Fundamentos de Git

Etiquetas Anotadas
Crear una etiqueta anotada en Git es sencillo. La forma más fácil de hacer es
especificar la opción -a cuando ejecutas el comando tag:
$ git tag -a v1.4 -m 'my version 1.4'
$ git tag
v0.1
v1.3
v1.4

La opción -m especifica el mensaje de la etiqueta, el cual es guardado junto
con ella. Si no especificas el mensaje de una etiqueta anotada, Git abrirá el editor de texto para que lo escribas.
Puedes ver la información de la etiqueta junto con el commit que está etiquetado al usar el comando git show:
$ git show v1.4
tag v1.4
Tagger: Ben Straub 
Date:
Sat May 3 20:19:12 2014 -0700
my version 1.4
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon 
Date:
Mon Mar 17 21:52:11 2008 -0700
changed the version number

El comando muestra la información del etiquetador, la fecha en la que el
commit fue etiquetado y el mensaje de la etiquetar, antes de mostrar la información del commit.

Etiquetas Ligeras
La otra forma de etiquetar un commit es mediante una etiqueta ligera. Una etiqueta ligera no es más que el checksum de un commit guardado en un archivo no incluye más información. Para crear una etiqueta ligera, no pases las opciones -a, -s ni -m:

76

Etiquetado

$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw
v1.5

Esta vez, si ejecutas git show sobre la etiqueta, no verás la información
adicional. El comando solo mostrará el commit:
$ git show v1.4-lw
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon 
Date:
Mon Mar 17 21:52:11 2008 -0700
changed the version number

Etiquetado Tardío
También puedes etiquetar commits mucho tiempo después de haberlos hecho.
Supongamos que tu historial luce como el siguiente:
$ git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6
a6b4c97498bd301d84096da251c98a07c7723e65
0d52aaab4479697da7686c15f77a3d64d9165190
6d52a271eda8725415634dd79daabbc4d9b6008e
0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc
4682c3261057305bdd616e23b64b0857d832627b
166ae0c4d3f420721acbb115cc33848dfcc2121a
9fceb02d0ae598e95dc970b74767f19372d61af8
964f16d36dfccde844893cac5b347e7b3d44abbc
8a5cbc430f1a9c3d00faaeffd07798508422908a

Merge branch 'experiment'
beginning write support
one more thing
Merge branch 'experiment'
added a commit function
added a todo file
started write support
updated rakefile
commit the todo
updated readme

Ahora, supongamos que olvidaste etiquetar el proyecto en su versión v1.2, la
cual corresponde al commit “updated rakefile”. Igual puedes etiquetarlo. Para
etiquetar un commit, debes especificar el checksum del commit (o parte de él)
al final del comando:
$ git tag -a v1.2 9fceb02

77

CHAPTER 2: Fundamentos de Git

Puedes ver que has etiquetado el commit:
$ git tag
v0.1
v1.2
v1.3
v1.4
v1.4-lw
v1.5
$ git show v1.2
tag v1.2
Tagger: Scott Chacon 
Date:
Mon Feb 9 15:32:16 2009 -0800
version 1.2
commit 9fceb02d0ae598e95dc970b74767f19372d61af8
Author: Magnus Chacon 
Date:
Sun Apr 27 20:43:35 2008 -0700
updated rakefile
...

Compartir Etiquetas
Por defecto, el comando git push no transfiere las etiquetas a los servidores
remotos. Debes enviar las etiquetas de forma explícita al servidor luego de que
las hayas creado. Este proceso es similar al de compartir ramas remotas puede ejecutar git push origin [etiqueta].
$ git push origin v1.5
Counting objects: 14, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.05 KiB | 0 bytes/s, done.
Total 14 (delta 3), reused 0 (delta 0)
To git@github.com:schacon/simplegit.git
* [new tag]
v1.5 -> v1.5

Si quieres enviar varias etiquetas a la vez, puedes usar la opción --tags del
comando git push. Esto enviará al servidor remoto todas las etiquetas que
aun no existen en él.

78

Git Aliases

$ git push origin --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 160 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@github.com:schacon/simplegit.git
* [new tag]
v1.4 -> v1.4
* [new tag]
v1.4-lw -> v1.4-lw

Por lo tanto, cuando alguien clone o traiga información de tu repositorio,
también obtendrá todas las etiquetas.

Sacar una Etiqueta
En Git, no puedes sacar (check out) una etiqueta, pues no es algo que puedas
mover. Si quieres colocar en tu directorio de trabajo una versión de tu repositorio que coincida con alguna etiqueta, debes crear una rama nueva en esa etiqueta:
$ git checkout -b version2 v2.0.0
Switched to a new branch 'version2'

Obviamente, si haces esto y luego confirmas tus cambios, tu rama version2
será ligeramente distinta a tu etiqueta v2.0.0 puesto que incluirá tus nuevos
cambios; así que ten cuidado.

Git Aliases
Before we finish this chapter on basic Git, there’s just one little tip that can
make your Git experience simpler, easier, and more familiar: aliases. We won’t
refer to them or assume you’ve used them later in the book, but you should
probably know how to use them.
Git doesn’t automatically infer your command if you type it in partially. If
you don’t want to type the entire text of each of the Git commands, you can
easily set up an alias for each command using git config. Here are a couple
of examples you may want to set up:
$
$
$
$

git
git
git
git

config
config
config
config

--global
--global
--global
--global

alias.co
alias.br
alias.ci
alias.st

checkout
branch
commit
status

79

CHAPTER 2: Fundamentos de Git

This means that, for example, instead of typing git commit, you just need
to type git ci. As you go on using Git, you’ll probably use other commands
frequently as well; don’t hesitate to create new aliases.
This technique can also be very useful in creating commands that you think
should exist. For example, to correct the usability problem you encountered
with unstaging a file, you can add your own unstage alias to Git:
$ git config --global alias.unstage 'reset HEAD --'

This makes the following two commands equivalent:
$ git unstage fileA
$ git reset HEAD fileA

This seems a bit clearer. It’s also common to add a last command, like this:
$ git config --global alias.last 'log -1 HEAD'

This way, you can see the last commit easily:
$ git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel 
Date:
Tue Aug 26 19:48:51 2008 +0800
test for current head
Signed-off-by: Scott Chacon 

As you can tell, Git simply replaces the new command with whatever you
alias it for. However, maybe you want to run an external command, rather than
a Git subcommand. In that case, you start the command with a ! character. This
is useful if you write your own tools that work with a Git repository. We can
demonstrate by aliasing git visual to run gitk:
$ git config --global alias.visual "!gitk"

80

Resumen

Resumen
En este momento puedes hacer todas las operaciones básicas de Git a nivel local: Crear o clonar un repositorio, hacer cambios, preparar y confirmar esos
cambios y ver la historia de los cambios en el repositorio. A continuación cubriremos la mejor característica de Git: Su modelo de ramas.

81

Ramificaciones en Git

3

Cualquier sistema de control de versiones moderno tiene algún mecanismo
para soportar distintos ramales. Cuando hablamos de ramificaciones, significa
que tú has tomado la rama principal de desarrollo (master) y a partir de ahí has
continuado trabajando sin seguir la rama principal de desarrollo. En muchas
sistemas de control de versiones este proceso es costoso, pues a menudo requiere crear una nueva copia del código, lo cual puede tomar mucho tiempo
cuando se trata de proyectos grandes.
Algunas personas resaltan que uno de los puntos más fuertes de Git es su
sistema de ramificaciones y lo cierto es que esto le hace resaltar sobre los otros
sistemas de control de versiones. ¿Por qué esto es tan importante? La forma en
la que Git maneja las ramificaciones es increíblemente rápida, haciendo así de
las operaciones de ramificación algo casi instantáneo, al igual que el avance o
el retroceso entre distintas ramas, lo cual también es tremendamente rápido. A
diferencia de otros sistemas de control de versiones, Git promueve un ciclo de
desarrollo donde las ramas se crean y se unen ramas entre sí, incluso varias veces en el mismo día. Entender y manejar esta opción te proporciona una poderosa y exclusiva herramienta que puede, literalmente, cambiar la forma en la
que desarrollas.

¿Qué es una rama?
Para entender realmente cómo ramifica Git, previamente hemos de examinar la
forma en que almacena sus datos.
Recordando lo citado en Chapter 1, Git no los almacena de forma incremental (guardando solo diferencias), sino que los almacena como una serie de instantáneas (copias puntuales de los archivos completos, tal y como se encuentran en ese momento).
En cada confirmación de cambios (commit), Git almacena una instantánea
de tu trabajo preparado. Dicha instantánea contiene además unos metadatos
con el autor y el mensaje explicativo, y uno o varios apuntadores a las confir-

83

CHAPTER 3: Ramificaciones en Git

maciones (commit) que sean padres directos de esta (un padre en los casos de
confirmación normal, y múltiples padres en los casos de estar confirmando una
fusión (merge) de dos o más ramas).
Para ilustrar esto, vamos a suponer, por ejemplo, que tienes una carpeta con
tres archivos, que preparas (stage) todos ellos y los confirmas (commit). Al preparar los archivos, Git realiza una suma de control de cada uno de ellos (un resumen SHA-1, tal y como se mencionaba en Chapter 1), almacena una copia de
cada uno en el repositorio (estas copias se denominan “blobs”), y guarda cada
suma de control en el área de preparación (staging area):
$ git add README test.rb LICENSE
$ git commit -m 'initial commit of my project'

Cuando creas una confirmación con el comando git commit, Git realiza sumas de control de cada subdirectorio (en el ejemplo, solamente tenemos el directorio principal del proyecto), y las guarda como objetos árbol en el repositorio Git. Después, Git crea un objeto de confirmación con los metadatos pertinentes y un apuntador al objeto árbol raiz del proyecto.
En este momento, el repositorio de Git contendrá cinco objetos: un “blob”
para cada uno de los tres archivos, un árbol con la lista de contenidos del directorio (más sus respectivas relaciones con los “blobs”), y una confirmación de
cambios (commit) apuntando a la raiz de ese árbol y conteniendo el resto de
metadatos pertinentes.

FIGURE 3-1
Una confirmación y
sus árboles

84

¿Qué es una rama?

Si haces más cambios y vuelves a confirmar, la siguiente confirmación guardará un apuntador su confirmación precedente.

FIGURE 3-2
Confirmaciones y
sus predecesoras

Una rama Git es simplemente un apuntador móvil apuntando a una de esas
confirmaciones. La rama por defecto de Git es la rama master. Con la primera
confirmación de cambios que realicemos, se creará esta rama principal master
apuntando a dicha confirmación. En cada confirmación de cambios que realicemos, la rama irá avanzando automáticamente.
La rama “master” en Git no es una rama especial. Es como cualquier otra
rama. La única razón por la cual aparece en casi todos los repositorioes es
porque es la que crea por defecto el comando git init y la gente no se
molesta en cambiarle el nombre.

FIGURE 3-3
Una rama y su
historial de
confirmaciones

85

CHAPTER 3: Ramificaciones en Git

Crear una Rama Nueva
¿Qué sucede cuando creas una nueva rama? Bueno…, simplemente se crea un
nuevo apuntador para que lo puedas mover libremente. Por ejemplo, supongamos que quieres crear una rama nueva denominada “testing”. Para ello, usarás
el comando git branch:
$ git branch testing

Esto creará un nuevo apuntador apuntando a la misma confirmación donde
estés actualmente.

FIGURE 3-4
Dos ramas
apuntando al mismo
grupo de
confirmaciones

Y, ¿cómo sabe Git en qué rama estás en este momento? Pues…, mediante un
apuntador especial denominado HEAD. Aunque es preciso comentar que este
HEAD es totalmente distinto al concepto de HEAD en otros sistemas de control
de cambios como Subversion o CVS. En Git, es simplemente el apuntador a la
rama local en la que tú estés en ese momento, en este caso la rama master;
pues el comando git branch solamente crea una nueva rama, y no salta a dicha rama.

86

¿Qué es una rama?

FIGURE 3-5
Apuntador HEAD a
la rama donde estás
actualmente

Esto puedes verlo fácilmente al ejecutar el comando git log para que te
muestre a dónde apunta cada rama. Esta opción se llama --decorate.
$ git
f30ab
34ac2
98ca9

log --oneline --decorate
(HEAD, master, testing) add feature #32 - ability to add new
fixed bug #1328 - stack overflow under certain conditions
initial commit of my project

Puedes ver que las ramas “master” y “testing” están junto a la confirmación
f30ab.

Cambiar de Rama
Para saltar de una rama a otra, tienes que utilizar el comando git checkout.
Hagamos una prueba, saltando a la rama testing recién creada:
$ git checkout testing

Esto mueve el apuntador HEAD a la rama testing.

87

CHAPTER 3: Ramificaciones en Git

FIGURE 3-6
El apuntador HEAD
apunta a la rama
actual

¿Cuál es el significado de todo esto? Bueno… lo veremos tras realizar otra
confirmación de cambios:
$ vim test.rb
$ git commit -a -m 'made a change'

FIGURE 3-7
La rama apuntada
por HEAD avanza
con cada
confirmación de
cambios

Observamos algo interesante: la rama testing avanza, mientras que la
rama master permanece en la confirmación donde estaba cuando lanzaste el
comando git checkout para saltar. Volvamos ahora a la rama master:

88

¿Qué es una rama?

$ git checkout master

FIGURE 3-8
HEAD apunta a otra
rama cuando
hacemos un salto

Este comando realiza dos acciones: Mueve el apuntador HEAD de nuevo a la
rama master, y revierte los archivos de tu directorio de trabajo; dejándolos tal
y como estaban en la última instantánea confirmada en dicha rama master. Esto supone que los cambios que hagas desde este momento en adelante divergirán de la antigua versión del proyecto. Básicamente, lo que se está haciendo
es rebobinar el trabajo que habías hecho temporalmente en la rama testing;
de tal forma que puedas avanzar en otra dirección diferente.
SALTAR ENTRE RAMAS CAMBIA ARCHIVOS EN TU DIRECTORIO
DE TRABAJO
Es importante destacar que cuando saltas a una rama en Git, los archivos
de tu directorio de trabajo cambian. Si saltas a una rama antigua, tu directorio de trabajo retrocederá para verse como lo hacía la última vez que
confirmaste un cambio en dicha rama. Si Git no puede hacer el cambio
limpiamente, no te dejará saltar.

Haz algunos cambios más y confírmalos:
$ vim test.rb
$ git commit -a -m 'made other changes'

Ahora el historial de tu proyecto diverge (ver Figure 3-9). Has creado una
rama y saltado a ella, has trabajado sobre ella; has vuelto a la rama original, y
has trabajado también sobre ella. Los cambios realizados en ambas sesiones de
trabajo están aislados en ramas independientes: puedes saltar libremente de

89

CHAPTER 3: Ramificaciones en Git

una a otra según estimes oportuno. Y todo ello simplemente con tres comandos: git branch, git checkout y git commit.

FIGURE 3-9
Los registros de las
ramas divergen

También puedes ver esto fácilmente utilizando el comando git log. Si ejecutas git log --oneline --decorate --graph --all te mostrará el historial de tus confirmaciones, indicando dónde están los apuntadores de tus
ramas y como ha divergido tu historial.
$ git log --oneline --decorate --graph --all
* c2b9e (HEAD, master) made other changes
| * 87ab2 (testing) made a change
|/
* f30ab add feature #32 - ability to add new formats to the
* 34ac2 fixed bug #1328 - stack overflow under certain conditions
* 98ca9 initial commit of my project

Debido a que una rama Git es realmente un simple archivo que contiene los
40 caracteres de una suma de control SHA-1, (representando la confirmación de
cambios a la que apunta), no cuesta nada el crear y destruir ramas en Git. Crear
una nueva rama es tan rápido y simple como escribir 41 bytes en un archivo, (40
caracteres y un retorno de carro).
Esto contrasta fuertemente con los métodos de ramificación usados por
otros sistemas de control de versiones, en los que crear una rama nueva su-

90

Procedimientos Básicos para Ramificar y Fusionar

pone el copiar todos los archivos del proyecto a un directorio adicional nuevo.
Esto puede llevar segundos o incluso minutos, dependiendo del tamaño del
proyecto; mientras que en Git el proceso es siempre instantáneo. Y, además, debido a que se almacenan también los nodos padre para cada confirmación, el
encontrar las bases adecuadas para realizar una fusión entre ramas es un proceso automático y generalmente sencillo de realizar. Animando así a los desarrolladores a utilizar ramificaciones frecuentemente.
Vamos a ver el por qué merece la pena hacerlo así.

Procedimientos Básicos para Ramificar y Fusionar
Vamos a presentar un ejemplo simple de ramificar y de fusionar, con un flujo de
trabajo que se podría presentar en la realidad. Imagina que sigues los siquientes pasos:
1. Trabajas en un sitio web.
2. Creas una rama para un nuevo tema sobre el que quieres trabajar.
3. Realizas algo de trabajo en esa rama.
En este momento, recibes una llamada avisándote de un problema crítico
que has de resolver. Y sigues los siguientes pasos:
1. Vuelves a la rama de producción original.
2. Creas una nueva rama para el problema crítico y lo resuelves trabajando
en ella.
3. Tras las pertinentes pruebas, fusionas (merge) esa rama y la envías (push)
a la rama de producción.
4. Vuelves a la rama del tema en que andabas antes de la llamada y continuas tu trabajo.

Procedimientos Básicos de Ramificación
Imagina que estas trabajando en un proyecto y tienes un par de confirmaciones
(commit) ya realizadas.

91

CHAPTER 3: Ramificaciones en Git

FIGURE 3-10
Un registro de
confirmaciones corto
y sencillo

Decides trabajar en el problema #53, según el sistema que tu compañía utiliza para llevar seguimiento de los problemas. Para crear una nueva rama y saltar a ella, en un solo paso, puedes utilizar el comando git checkout con la
opción -b:
$ git checkout -b iss53
Switched to a new branch "iss53"

Esto es un atajo a:
$ git branch iss53
$ git checkout iss53

FIGURE 3-11
Crear un apuntador
a la rama nueva

92

Procedimientos Básicos para Ramificar y Fusionar

Trabajas en el sitio web y haces algunas confirmaciones de cambios (commits). Con ello avanzas la rama iss53, que es la que tienes activada (checked
out) en este momento (es decir, a la que apunta HEAD):
$ vim index.html
$ git commit -a -m 'added a new footer [issue 53]'

FIGURE 3-12
La rama iss53 ha
avanzado con tu
trabajo

Entonces, recibes una llamada avisándote de otro problema urgente en el
sitio web y debes resolverlo inmediatamente. Al usar Git, no necesitas mezclar
el nuevo problema con los cambios que ya habías realizado sobre el problema
#53; ni tampoco perder tiempo revirtiendo esos cambios para poder trabajar
sobre el contenido que está en producción. Basta con saltar de nuevo a la rama
master y continuar trabajando a partir de allí.
Pero, antes de poder hacer eso, hemos de tener en cuenta que si tenenmos
cambios aún no confirmados en el directorio de trabajo o en el área de preparación, Git no nos permitirá saltar a otra rama con la que podríamos tener conflictos. Lo mejor es tener siempre un estado de trabajo limpio y despejado antes
de saltar entre ramas. Y, para ello, tenemos algunos procedimientos (stash y
corregir confirmaciones), que vamos a ver más adelante en “Stashing and
Cleaning”. Por ahora, como tenemos confirmados todos los cambios, podemos saltar a la rama master sin problemas:
$ git checkout master
Switched to branch 'master'

Tras esto, tendrás el directorio de trabajo exactamente igual a como estaba
antes de comenzar a trabajar sobre el problema #53 y podrás concentrarte en el
nuevo problema urgente. Es importante recordar que Git revierte el directorio

93

CHAPTER 3: Ramificaciones en Git

de trabajo exactamente al estado en que estaba en la confirmación (commit)
apuntada por la rama que activamos (checkout) en cada momento. Git añade,
quita y modifica archivos automáticamente para asegurar que tu copia de trabajo luce exactamente como lucía la rama en la última confirmación de cambios realizada sobre ella.
A continuación, es momento de resolver el problema urgente. Vamos a crear
una nueva rama hotfix, sobre la que trabajar hasta resolverlo:
$ git checkout -b hotfix
Switched to a new branch 'hotfix'
$ vim index.html
$ git commit -a -m 'fixed the broken email address'
[hotfix 1fb7853] fixed the broken email address
1 file changed, 2 insertions(+)

FIGURE 3-13
Rama hotfix
basada en la rama
master original

Puedes realizar las pruebas oportunas, asegurarte que la solución es correcta, e incorporar los cambios a la rama master para ponerlos en producción. Esto se hace con el comando git merge:
$ git checkout master
$ git merge hotfix
Updating f42c576..3a0874c
Fast-forward
index.html | 2 ++
1 file changed, 2 insertions(+)

94

Procedimientos Básicos para Ramificar y Fusionar

Notarás la frase “Fast forward” (“Avance rápido”, en inglés) que aparece en la
salida del comando. Git ha movido el apuntador hacia adelante, ya que la confirmación apuntada en la rama donde has fusionado estaba directamente arriba respecto a la confirmación actual. Dicho de otro modo: cuando intentas fusionar una confirmación con otra confirmación accesible siguiendo directamente el historial de la primera; Git simplifica las cosas avanzando el puntero,
ya que no hay ningún otro trabajo divergente a fusionar. Esto es lo que se denomina “avance rápido” (“fast forward”).
Ahora, los cambios realizados están ya en la instantánea (snapshot) de la
confirmación (commit) apuntada por la rama master. Y puedes desplegarlos.

FIGURE 3-14
Tras la fusión
(merge), la rama
master apunta al
mismo sitio que la
rama hotfix.

Tras haber resuelto el problema urgente que había interrumpido tu trabajo,
puedes volver a donde estabas. Pero antes, es importante borrar la rama hotfix, ya que no la vamos a necesitar más, puesto que apunta exactamente al
mismo sitio que la rama master. Esto lo puedes hacer con la opción -d del comando git branch:
$ git branch -d hotfix
Deleted branch hotfix (3a0874c).

Y, con esto, ya estás listo para regresar al trabajo sobre el problema #53.

95

CHAPTER 3: Ramificaciones en Git

$ git checkout iss53
Switched to branch "iss53"
$ vim index.html
$ git commit -a -m 'finished the new footer [issue 53]'
[iss53 ad82d7a] finished the new footer [issue 53]
1 file changed, 1 insertion(+)

FIGURE 3-15
La rama iss53
puede avanzar
independientemente

Cabe destacar que todo el trabajo realizado en la rama hotfix no está en
los archivos de la rama iss53. Si fuera necesario agregarlos, puedes fusionar
(merge) la rama master sobre la rama iss53 utilizando el comando git
merge master, o puedes esperar hasta que decidas fusionar (merge) la rama
iss53 a la rama master.

Procedimientos Básicos de Fusión
Supongamos que tu trabajo con el problema #53 ya está completo y listo para
fusionarlo (merge) con la rama master. Para ello, de forma similar a como antes has hecho con la rama hotfix, vas a fusionar la rama iss53. Simplemente,
activa (checkout) la rama donde deseas fusionar y lanza el comando git
merge:
$ git checkout master
Switched to branch 'master'
$ git merge iss53
Merge made by the 'recursive' strategy.

96

Procedimientos Básicos para Ramificar y Fusionar

index.html |
1 +
1 file changed, 1 insertion(+)

Es algo diferente de la fusión realizada anteriormente con hotfix. En este
caso, el registro de desarrollo había divergido en un punto anterior. Debido a
que la confirmación en la rama actual no es ancestro directo de la rama que
pretendes fusionar, Git tiene cierto trabajo extra que hacer. Git realizará una fusión a tres bandas, utilizando las dos instantáneas apuntadas por el extremo de
cada una de las ramas y por el ancestro común a ambas.

FIGURE 3-16
Git identifica
automáticamente el
mejor ancestro
común para realizar
la fusión de las
ramas

En lugar de simplemente avanzar el apuntador de la rama, Git crea una nueva instantánea (snapshot) resultante de la fusión a tres bandas; y crea automáticamente una nueva confirmación de cambios (commit) que apunta a ella. Nos
referimos a este proceso como “fusión confirmada” y su particularidad es que
tiene más de un padre.

97

CHAPTER 3: Ramificaciones en Git

FIGURE 3-17
Git crea
automáticamente
una nueva
confirmación para la
fusión

Vale la pena destacar el hecho de que es el propio Git quien determina automáticamente el mejor ancestro común para realizar la fusión; a diferencia de
otros sistemas tales como CVS o Subversion, donde es el desarrollador quien
ha de determinar cuál puede ser dicho mejor ancestro común. Esto hace que en
Git sea mucho más fácil realizar fusiones.
Ahora que todo tu trabajo ya está fusionado con la rama principal, no tienes
necesidad de la rama iss53. Por lo que puedes borrarla y cerrar manualmente
el problema en el sistema de seguimiento de problemas de tu empresa.
$ git branch -d iss53

Principales Conflictos que Pueden Surgir en las Fusiones
En algunas ocasiones, los procesos de fusión no suelen ser fluidos. Si hay modificaciones dispares en una misma porción de un mismo archivo en las dos
ramas distintas que pretendes fusionar, Git no será capaz de fusionarlas directamente. Por ejemplo, si en tu trabajo del problema #53 has modificado una
misma porción que también ha sido modificada en el problema hotfix, verás
un conflicto como este:
$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

Git no crea automáticamente una nueva fusión confirmada (merge commit),
sino que hace una pausa en el proceso, esperando a que tú resuelvas el conflic-

98

Procedimientos Básicos para Ramificar y Fusionar

to. Para ver qué archivos permanecen sin fusionar en un determinado momento conflictivo de una fusión, puedes usar el comando git status:
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add ..." to mark resolution)
both modified:

index.html

no changes added to commit (use "git add" and/or "git commit -a")

Todo aquello que sea conflictivo y no se haya podido resolver, se marca como “sin fusionar” (unmerged). Git añade a los archivos conflictivos unos marcadores especiales de resolución de conflictos que te guiarán cuando abras manualmente los archivos implicados y los edites para corregirlos. El archivo conflictivo contendrá algo como:
<<<<<<< HEAD:index.html

=======

>>>>>>> iss53:index.html

Donde nos dice que la versión en HEAD (la rama master, la que habias activado antes de lanzar el comando de fusión) contiene lo indicado en la parte superior del bloque (todo lo que está encima de =======) y que la versión en
iss53 contiene el resto, lo indicado en la parte inferior del bloque. Para resolver el conflicto, has de elegir manualmente el contenido de uno o de otro lado.
Por ejemplo, puedes optar por cambiar el bloque, dejándolo así:


Esta corrección contiene un poco de ambas partes y se han eliminado completamente las líneas <<<<<<< , ======= y >>>>>>>. Tras resolver todos los
bloques conflictivos, has de lanzar comandos git add para marcar cada archi-

99

CHAPTER 3: Ramificaciones en Git

vo modificado. Marcar archivos como preparados (staged) indica a Git que sus
conflictos han sido resueltos.
Si en lugar de resolver directamente prefieres utilizar una herramienta gráfica, puedes usar el comando git mergetool, el cual arrancará la correspondiente herramienta de visualización y te permitirá ir resolviendo conflictos con
ella:
$ git mergetool

This message is displayed because 'merge.tool' is not configured.
See 'git mergetool --tool-help' or 'git help config' for more details.
'git mergetool' will now attempt to use one of the following tools:
opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse diffmerge ecmerg
Merging:
index.html
Normal merge conflict for 'index.html':
{local}: modified file
{remote}: modified file
Hit return to start merge resolution tool (opendiff):

Si deseas usar una herramienta distinta de la escogida por defecto (en mi
caso opendiff, porque estoy lanzando el comando en Mac), puedes escogerla
entre la lista de herramientas soportadas mostradas al principio (“merge tool
candidates”) tecleando el nombre de dicha herramienta.
Si necesitas herramientas más avanzadas para resolver conflictos de fusión más complicados, revisa la sección de fusionado en “Advanced
Merging”.

Tras salir de la herramienta de fusionado, Git preguntará si hemos resuelto
todos los conflictos y la fusión ha sido satisfactoria. Si le indicas que así ha sido,
Git marca como preparado (staged) el archivo que acabamos de modificar. En
cualquier momento, puedes lanzar el comando git status para ver si ya has
resuelto todos los conflictos:
$ git status
On branch master
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:

100

Gestión de Ramas

modified:

index.html

Si todo ha ido correctamente, y ves que todos los archivos conflictivos están
marcados como preparados, puedes lanzar el comando git commit para terminar de confirmar la fusión. El mensaje de confirmación por defecto será algo
parecido a:
Merge branch 'iss53'
Conflicts:
index.html
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#
.git/MERGE_HEAD
# and try again.

#
#
#
#
#
#
#
#

Please enter the commit message for your changes. Lines starting
with '#' will be ignored, and an empty message aborts the commit.
On branch master
All conflicts fixed but you are still merging.
Changes to be committed:
modified:
index.html

Puedes modificar este mensaje añadiendo detalles sobre cómo has resuelto
la fusión, si lo consideras útil para que otros entiendan esta fusión en un futuro.
Se trata de indicar por qué has hecho lo que has hecho; a no ser que resulte
obvio, claro está.

Gestión de Ramas
Ahora que ya has creado, fusionado y borrado algunas ramas, vamos a dar un
vistazo a algunas herramientas de gestión muy útiles cuando comienzas a utilizar ramas de manera avanzada.
El comando git branch tiene más funciones que las de crear y borrar
ramas. Si lo lanzas sin parámetros, obtienes una lista de las ramas presentes en
tu proyecto:

101

CHAPTER 3: Ramificaciones en Git

$ git branch
iss53
* master
testing

Fijate en el carácter * delante de la rama master: nos indica la rama activa
en este momento (la rama a la que apunta HEAD). Si hacemos una confirmación
de cambios (commit), esa será la rama que avance. Para ver la última confirmación de cambios en cada rama, puedes usar el comando git branch -v:
$ git branch -v
iss53
93b412c fix javascript issue
* master 7a98805 Merge branch 'iss53'
testing 782fd34 add scott to the author list in the readmes

Otra opción útil para averiguar el estado de las ramas, es filtrarlas y mostrar
solo aquellas que han sido fusionadas (o que no lo han sido) con la rama actualmente activa. Para ello, Git dispone de las opciones --merged y --nomerged. Si deseas ver las ramas que han sido fusionadas en la rama activa,
puedes lanzar el comando git branch --merged:
$ git branch --merged
iss53
* master

Aparece la rama iss53 porque ya ha sido fusionada. Las ramas que no llevan por delante el caracter * pueden ser eliminadas sin problemas, porque todo su contenido ya ha sido incorporado a otras ramas.
Para mostrar todas las ramas que contienen trabajos sin fusionar, puedes
utilizar el comando git branch --no-merged:
$ git branch --no-merged
testing

Esto nos muestra la otra rama del proyecto. Debido a que contiene trabajos
sin fusionar, al intentarla borrarla con git branch -d, el comando nos dará
un error:

102

Flujos de Trabajo Ramificados

$ git branch -d testing
error: The branch 'testing' is not fully merged.
If you are sure you want to delete it, run 'git branch -D testing'.

Si realmente deseas borrar la rama, y perder el trabajo contenido en ella,
puedes forzar el borrado con la opción -D; tal y como indica el mensaje de ayuda.

Flujos de Trabajo Ramificados
Ahora que ya has visto los procedimientos básicos de ramificación y fusión,
¿qué puedes o qué debes hacer con ellos? En este apartado vamos a ver algunos de los flujos de trabajo más comunes, de tal forma que puedas decidir si te
gustaría incorporar alguno de ellos a tu ciclo de desarrollo.

Ramas de Largo Recorrido
Por la sencillez de la fusión a tres bandas de Git, el fusionar una rama a otra
varias veces a lo largo del tiempo es fácil de hacer. Esto te posibilita tener varias
ramas siempre abiertas, e irlas usando en diferentes etapas del ciclo de desarrollo; realizando fusiones frecuentes entre ellas.
Muchos desarrolladores que usan Git llevan un flujo de trabajo de esta naturaleza, manteniendo en la rama master únicamente el código totalmente estable (el código que ha sido o que va a ser liberado) y teniendo otras ramas paralelas denominadas desarrollo o siguiente, en las que trabajan y realizan
pruebas. Estas ramas paralelas no suele estar siempre en un estado estable;
pero cada vez que sí lo están, pueden ser fusionadas con la rama master. También es habitual el incorporarle (pull) ramas puntuales (ramas temporales, como la rama iss53 del ejemplo anterior) cuando las completamos y estamos seguros de que no van a introducir errores.
En realidad, en todo momento estamos hablando simplemente de apuntadores moviéndose por la línea temporal de confirmaciones de cambio (commit
history). Las ramas estables apuntan hacia posiciones más antiguas en el historial de confirmaciones, mientras que las ramas avanzadas, las que van
abriendo camino, apuntan hacia posiciones más recientes.

103

CHAPTER 3: Ramificaciones en Git

FIGURE 3-18
Una vista lineal del
ramificado
progresivo estable

Podría ser más sencillo pensar en las ramas como si fueran silos de almacenamiento, donde grupos de confirmaciones de cambio (commits) van siendo
promocionados hacia silos más estables a medida que son probados y depurados.

FIGURE 3-19
Una vista tipo “silo”
del ramificado
progresivo estable

Este sistema de trabajo se puede ampliar para diversos grados de estabilidad. Algunos proyectos muy grandes suelen tener una rama denominada propuestas o pu (del inglés “proposed updates”, propuesta de actualización),
donde suele estar todo aquello integrado desde otras ramas, pero que aún no
está listo para ser incorporado a las ramas siguiente o master. La idea es
mantener siempre diversas ramas en diversos grados de estabilidad; pero
cuando alguna alcanza un estado más estable, la fusionamos con la rama inmediatamente superior a ella. Aunque no es obligatorio el trabajar con ramas
de larga duración, realmente es práctico y útil, sobre todo en proyectos largos o
complejos.

Ramas Puntuales
Las ramas puntuales, en cambio, son útiles en proyectos de cualquier tamaño.
Una rama puntual es aquella rama de corta duración que abres para un tema o
para una funcionalidad determinada. Es algo que nunca habrías hecho en otro
sistema VCS, debido a los altos costos de crear y fusionar ramas en esos siste-

104

Flujos de Trabajo Ramificados

mas. Pero en Git, por el contrario, es muy habitual el crear, trabajar con, fusionar y eliminar ramas varias veces al día.
Tal y como has visto con las ramas iss53 y hotfix que has creado en la sección anterior. Has hecho algunas confirmaciones de cambio en ellas, y luego las
has borrado tras fusionarlas con la rama principal. Esta técnica te posibilita realizar cambios de contexto rápidos y completos y, debido a que el trabajo está
claramente separado en silos, con todos los cambios de cada tema en su propia
rama, te será mucho más sencillo revisar el código y seguir su evolución.
Puedes mantener los cambios ahí durante minutos, dias o meses; y fusionarlos
cuando realmente estén listos, sin importar el orden en el que fueron creados o
en el que comenzaste a trabajar en ellos.
Por ejemplo, puedes realizar cierto trabajo en la rama master, ramificar
para un problema concreto (rama iss91), trabajar en él un rato, ramificar una
segunda vez para probar otra manera de resolverlo (rama iss92v2), volver a la
rama master y trabajar un poco más, y, por último, ramificar temporalmente
para probar algo de lo que no estás seguro (rama dumbidea). El historial de
confirmaciones (commit history) será algo parecido esto:

FIGURE 3-20
Múltiples ramas
puntuales

105

CHAPTER 3: Ramificaciones en Git

En este momento, supongamos que te decides por la segunda solución al
problema (rama iss92v2); y que, tras mostrar la rama dumbidea a tus compañeros, resulta que les parece una idea genial. Puedes descartar la rama iss91
(perdiendo las confirmaciones C5 y C6), y fusionar las otras dos. El historial será
algo parecido a esto:

FIGURE 3-21
El historial tras
fusionar dumbidea e

iss91v2

Hablaremos un poco más sobre los distintos flujos de trabajo de tu proyecto
Git en Chapter 5, así que antes de decidir qué estilo de ramificación usará tu
próximo proyecto, asegúrate de haber leído ese capítulo.
Es importante recordar que, mientras estás haciendo todo esto, todas las
ramas son completamente locales. Cuando ramificas y fusionas, todo se realiza

106

Ramas Remotas

en tu propio repositorio Git. No hay nigún tipo de comunicación con ningún servidor.

Ramas Remotas
Las ramas remotas son referencias al estado de las ramas en tus repositorios
remotos. Son ramas locales que no puedes mover; se mueven automáticamente cuando estableces comunicaciones en la red. Las ramas remotas funcionan como marcadores, para recordarte en qué estado se encontraban tus repositorios remotos la última vez que conectaste con ellos.
Suelen referenciarse como (remoto)/(rama). Por ejemplo, si quieres saber
cómo estaba la rama master en el remoto origin, puedes revisar la rama
origin/master. O si estás trabajando en un problema con un compañero y
este envía (push) una rama iss53, tú tendrás tu propia rama de trabajo local
iss53; pero la rama en el servidor apuntará a la última confirmación (commit)
en la rama origin/iss53.
Esto puede ser un tanto confuso, pero intentemos aclararlo con un ejemplo.
Supongamos que tienes un sevidor Git en tu red, en git.ourcompany.com. Si
haces un clón desde ahí, Git automáticamente lo denominará origin, traerá
(pull) sus datos, creará un apuntador hacia donde esté en ese momento su
rama master y denominará la copia local origin/master. Git te proporcionará también tu propia rama master, apuntando al mismo lugar que la rama
master de origin; de manera que tengas donde trabajar.
“ORIGIN” NO ES ESPECIAL
Así como la rama “master” no tiene ningún significado especial en Git,
tampoco lo tiene “origin”. “master” es un nombre muy usado solo porque es el nombre por defecto que Git le da a la rama inicial cuando ejecutas git init. De la misma manera, “origin” es el nombre por defecto que
Git le da a un remoto cuando ejecutas git clone. Si en cambio ejecutases

git clone -o booyah, tendrías una rama booyah/master como rama remota por defecto.

107

CHAPTER 3: Ramificaciones en Git

FIGURE 3-22
Servidor y
repositorio local
luego de ser clonado

Si haces algún trabajo en tu rama master local, y al mismo tiempo, alguien
más lleva (push) su trabajo al servidor git.ourcompany.com, actualizando la
rama master de allí, te encontrarás con que ambos registros avanzan de forma
diferente. Además, mientras no tengas contacto con el servidor, tu apuntador a
tu rama origin/master no se moverá.

108

Ramas Remotas

FIGURE 3-23
El trabajo remoto y
el local pueden
diverger

Para sincronizarte, puedes utilizar el comando git fetch origin. Este comando localiza en qué servidor está el origen (en este caso git.ourcompany.com), recupera cualquier dato presente allí que tú no tengas, y actualiza tu
base de datos local, moviendo tu rama origin/master para que apunte a la
posición más reciente.

109

CHAPTER 3: Ramificaciones en Git

FIGURE 3-24
git fetch actualiza
las referencias de tu
remoto

Para ilustrar mejor el caso de tener múltiples servidores y cómo van las
ramas remotas para esos proyectos remotos, supongamos que tienes otro servidor Git; utilizado por uno de tus equipos sprint, solamente para desarrollo.
Este servidor se encuentra en git.team1.ourcompany.com. Puedes incluirlo
como una nueva referencia remota a tu proyecto actual, mediante el comando
git remote add, tal y como vimos en Chapter 2. Puedes denominar teamone
a este remoto al asignarle este nombre a la URL.

110

Ramas Remotas

FIGURE 3-25
Añadiendo otro
servidor como
remoto

Ahora, puedes usar el comando git fetch teamone para recuperar todo
el contenido del remoto teamone que tú no tenias. Debido a que dicho servidor
es un subconjunto de los datos del servidor origin que tienes actualmente, Git
no recupera (fetch) ningún dato; simplemente prepara una rama remota llamada teamone/master para apuntar a la confirmación (commit) que teamone
tiene en su rama master.

111

CHAPTER 3: Ramificaciones en Git

FIGURE 3-26
Seguimiento de la
rama remota a
través de teamone/

master

Publicar
Cuando quieres compartir una rama con el resto del mundo, debes llevarla
(push) a un remoto donde tengas permisos de escritura. Tus ramas locales no
se sincronizan automáticamente con los remotos en los que escribes, sino que
tienes que enviar (push) expresamente las ramas que desees compartir. De esta
forma, puedes usar ramas privadas para el trabajo que no deseas compartir, llevando a un remoto tan solo aquellas partes que deseas aportar a los demás.
Si tienes una rama llamada serverfix, con la que vas a trabajar en colaboración; puedes llevarla al remoto de la misma forma que llevaste tu primera
rama. Con el comando git push (remoto) (rama):
$ git push origin serverfix
Counting objects: 24, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done.
Total 24 (delta 2), reused 0 (delta 0)
To https://github.com/schacon/simplegit
* [new branch]
serverfix -> serverfix

112

Ramas Remotas

Esto es un atajo. Git expande automáticamente el nombre de rama serverfix a refs/heads/serverfix:refs/heads/serverfix, que significa: “coge
mi rama local serverfix y actualiza con ella la rama serverfix del remoto”.
Volveremos más tarde sobre el tema de refs/heads/, viéndolo en detalle en
Chapter 10; por ahora, puedes ignorarlo. También puedes hacer git push
origin serverfix:serverfix , que hace lo mismo; es decir: “coge mi serverfix y hazlo el serverfix remoto”. Puedes utilizar este último formato para
llevar una rama local a una rama remota con un nombre distinto. Si no quieres
que se llame serverfix en el remoto, puedes lanzar, por ejemplo, git push
origin serverfix:awesomebranch; para llevar tu rama serverfix local a
la rama awesomebranch en el proyecto remoto.
NO ESCRIBAS TU CONTRASEÑA TODO EL TIEMPO
Si utilizas una dirección URL con HTTPS para enviar datos, el servidor Git
te preguntará tu usuario y contraseña para autenticarte. Por defecto, te
pedirá esta información a través del terminal, para determinar si estás
autorizado a enviar datos.
Si no quieres escribir tu contraseña cada vez que haces un envío, puedes
establecer un “cache de credenciales”. La manera más sencilla de hacerlo
es estableciéndolo en memoria por unos minutos, lo que puedes lograr
fácilmente al ejecutar git config --global credential.helper cache
Para más información sobre las distintas opciones de cache de credenciales, véase “Credential Storage”.

La próxima vez que tus colaboradores recuperen desde el servidor, obtendrán bajo la rama remota origin/serverfix una referencia a donde esté la
versión de serverfix en el servidor:
$ git fetch origin
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/schacon/simplegit
* [new branch]
serverfix
-> origin/serverfix

Es importante destacar que cuando recuperas (fetch) nuevas ramas remotas, no obtienes automáticamente una copia local editable de las mismas. En
otras palabras, en este caso, no tienes una nueva rama serverfix. Sino que
únicamente tienes un puntero no editable a origin/serverfix.

113

CHAPTER 3: Ramificaciones en Git

Para integrar (merge) esto en tu rama de trabajo actual, puedes usar el comando git merge origin/serverfix. Y si quieres tener tu propia rama
serverfix para trabajar, puedes crearla directamente basandote en la rama
remota:
$ git checkout -b serverfix origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'

Esto sí te da una rama local donde puedes trabajar, que comienza donde

origin/serverfix estaba en ese momento.

Hacer Seguimiento a las Ramas
Al activar (checkout) una rama local a partir de una rama remota, se crea automáticamente lo que podríamos denominar una “rama de seguimiento” (tracking branch). Las ramas de seguimiento son ramas locales que tienen una relación directa con alguna rama remota. Si estás en una rama de seguimiento y
tecleas el comando git pull, Git sabe de cuál servidor recuperar (fetch) y fusionar (merge) datos.
Cuando clonas un repositorio, este suele crear automáticamente una rama
master que hace seguimiento de origin/master. Sin embargo, puedes preparar otras ramas de seguimiento si deseas tener unas que sigan ramas de
otros remotos o no seguir la rama master. El ejemplo más simple es el que acabas de ver al lanzar el comando git checkout -b [rama] [nombreremoto]/[rama]. Esta operación es tan común que git ofrece el parámetro -track:
$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'

Para preparar una rama local con un nombre distinto a la del remoto,
puedes utilizar la primera versión con un nombre de rama local diferente:
$ git checkout -b sf origin/serverfix
Branch sf set up to track remote branch serverfix from origin.
Switched to a new branch 'sf'

114

Ramas Remotas

Así, tu rama local sf traerá (pull) información automáticamente desde origin/serverfix.
Si ya tienes una rama local y quieres asignarla a una rama remota que acabas de traerte, o quieres cambiar la rama a la que le haces seguimiento, puedes
usar en cualquier momento las opciones -u o --set-upstream-to del comando git branch.
$ git branch -u origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.

ATAJO AL UPSTREAM
Cuando tienes asignada una rama de seguimiento, puedes hacer referencia a ella mediante @{upstream} o mediante el atajo @{u}. De esta manera,
si estás en la rama master y esta sigue a la rama origin/master, puedes
hacer algo como git merge @{u} en vez de git merge origin/master.

Si quieres ver las ramas de seguimiento que tienes asignado, puedes usar la
opción -vv con git branch. Esto listará tus ramas locales con más información, incluyendo a qué sigue cada rama y si tu rama local está por delante, por
detrás o ambas.
$ git branch -vv
iss53
7e424c3
master
1ae2a45
* serverfix f8674d9
testing
5ea463a

[origin/iss53: ahead 2] forgot the brackets
[origin/master] deploying index fix
[teamone/server-fix-good: ahead 3, behind 1] this should do it
trying something new

Aquí podemos ver que nuestra rama iss53 sigue origin/iss53 y está
“ahead” (delante) por dos, es decir, que tenemos dos confirmaciones locales
que no han sido enviadas al servidor. También podemos ver que nuestra rama
master sigue a origin/master y está actualizada. Luego podemos ver que
nuestra rama serverfix sigue la rama server-fix-good de nuestro servidor
teamone y que está tres cambios por delante (ahead) y uno por detrás (behind),
lo que significa que existe una confirmación en el servidor que no hemos fusionado y que tenemos tres confirmaciones locales que no hemos enviado. Por último, podemos ver que nuestra rama testing no sigue a ninguna rama remota.
Es importante destacar que estos números se refieren a la última vez que
trajiste (fetch) datos de cada servidor. Este comando no se comunica con los
servidores, solo te indica lo que sabe de ellos localmente. Si quieres tener los

115

CHAPTER 3: Ramificaciones en Git

cambios por delante y por detrás actualizados, debes traertelos (fetch) de cada
servidor antes de ejecutar el comando. Puedes hacerlo de esta manera: $ git

fetch --all; git branch -vv

Traer y Fusionar
A pesar de que el comando git fetch trae todos los cambios del servidor que
no tienes, este no modifica tu directorio de trabajo. Simplemente obtendrá los
datos y dejará que tú mismo los fusiones. Sin embargo, existe un comando llamado git pull, el cuál básicamente hace git fetch seguido por git merge
en la mayoría de los casos. Si tienes una rama de seguimiento configurada como vimos en la última sección, bien sea asignándola explícitamente o creándola mediante los comandos clone o checkout, git pull identificará a qué servidor y rama remota sigue tu rama actual, traerá los datos de dicho servidor e
intentará fusionar dicha rama remota.
Normalmente es mejor usar los comandos fetch y merge de manera explícita pues la magia de git pull puede resultar confusa.

Eliminar Ramas Remotas
Imagina que ya has terminado con una rama remota, es decir, tanto tú como
tus colaboradores habéis completado una determinada funcionalidad y la habéis incorporado (merge) a la rama master en el remoto (o donde quiera que
tengáis la rama de código estable). Puedes borrar la rama remota utilizando la
opción --delete de git push. Por ejemplo, si quieres borrar la rama serverfix del servidor, puedes utilizar:
$ git push origin --delete serverfix
To https://github.com/schacon/simplegit
- [deleted]
serverfix

Básicamente lo que hace es eliminar el apuntador del servidor. El servidor
Git suele mantener los datos por un tiempo hasta que el recolector de basura se
ejecute, de manera que si la has borrado accidentalmente, suele ser fácil recuperarla.

116

Reorganizar el Trabajo Realizado

Reorganizar el Trabajo Realizado
En Git tenemos dos formas de integrar cambios de una rama en otra: la fusión
(merge) y la reorganización (rebase). En esta sección vas a aprender en qué consiste la reorganización, cómo utilizarla, por qué es una herramienta sorprendente y en qué casos no es conveniente utilizarla.

Reorganización Básica
Volviendo al ejemplo anterior, en la sección sobre fusiones “Procedimientos
Básicos de Fusión” puedes ver que has separado tu trabajo y realizado confirmaciones (commit) en dos ramas diferentes.

FIGURE 3-27
El registro de
confirmaciones
inicial

La manera más sencilla de integrar ramas, tal y como hemos visto, es el comando git merge. Realiza una fusión a tres bandas entre las dos últimas instantáneas de cada rama (C3 y C4) y el ancestro común a ambas (C2); creando
una nueva instantánea (snapshot) y la correspondiente confirmación (commit).

117

CHAPTER 3: Ramificaciones en Git

FIGURE 3-28
Fusionar una rama
para integrar el
registro de trabajos
divergentes

Sin embargo, también hay otra forma de hacerlo: puedes coger los cambios
introducidos en C3 y reaplicarlos encima de C4. Esto es lo que en Git llamamos
reorganizar (rebasing, en inglés). Con el comando git rebase, puedes coger
todos los cambios confirmados en una rama, y reaplicarlos sobre otra.
Por ejemplo, puedes lanzar los comandos:
$ git checkout experiment
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added staged command

Haciendo que Git vaya al ancestro común de ambas ramas (donde estás actualmente y de donde quieres reorganizar), saque las diferencias introducidas
por cada confirmación en la rama donde estás, guarde esas diferencias en archivos temporales, reinicie (reset) la rama actual hasta llevarla a la misma confirmación en la rama de donde quieres reorganizar, y, finalmente, vuelva a aplicar ordenadamente los cambios.

FIGURE 3-29
Reorganizando
sobre C3 los cambios
introducidos en C4

118

Reorganizar el Trabajo Realizado

En este momento, puedes volver a la rama master y hacer una fusión con
avance rápido (fast-forward merge).
$ git checkout master
$ git merge experiment

FIGURE 3-30
Avance rápido de la
rama master

Así, la instantánea apuntada por C4' es exactamente la misma apuntada
por C5 en el ejemplo de la fusión. No hay ninguna diferencia en el resultado final de la integración, pero el haberla hecho reorganizando nos deja un historial
más claro. Si examinas el historial de una rama reorganizada, este aparece
siempre como un historial lineal: como si todo el trabajo se hubiera realizado
en series, aunque realmente se haya hecho en paralelo.
Habitualmente, optarás por esta vía cuando quieras estar seguro de que tus
confirmaciones de cambio (commits) se pueden aplicar limpiamente sobre una
rama remota; posiblemente, en un proyecto donde estés intentando colaborar,
pero lleves tú el mantenimiento. En casos como esos, puedes trabajar sobre
una rama y luego reorganizar lo realizado en la rama origin/master cuando
lo tengas todo listo para enviarlo al proyecto principal. De esta forma, la persona que mantiene el proyecto no necesitará hacer ninguna integración con tu
trabajo; le bastará con un avance rápido o una incorporación limpia.
Cabe destacar que la instantánea (snapshot) apuntada por la confirmación
(commit) final, tanto si es producto de una reorganización (rebase) como si lo
es de una fusión (merge), es exactamente la misma instantánea; lo único diferente es el historial. La reorganización vuelve a aplicar cambios de una rama de
trabajo sobre otra rama, en el mismo orden en que fueron introducidos en la
primera, mientras que la fusión combina entre sí los dos puntos finales de ambas ramas.

119

CHAPTER 3: Ramificaciones en Git

Algunas Reorganizaciones Interesantes
También puedes aplicar una reorganización (rebase) sobre otra cosa además
de sobre la rama de reorganización. Por ejemplo, considera un historial como el
de Figure 3-31. Has ramificado a una rama puntual (server) para añadir algunas funcionalidades al proyecto, y luego has confirmado los cambios. Después,
vuelves a la rama original para hacer algunos cambios en la parte cliente (rama
client), y confirmas también esos cambios. Por último, vuelves sobre la rama
server y haces algunos cambios más.

FIGURE 3-31
Un historial con una
rama puntual sobre
otra rama puntual

Imagina que decides incorporar tus cambios del lado cliente sobre el
proyecto principal para hacer un lanzamiento de versión; pero no quieres lanzar aún los cambios del lado servidor porque no están aún suficientemente
probados. Puedes coger los cambios del cliente que no están en server (C8 y
C9) y reaplicarlos sobre tu rama principal usando la opción --onto del comando git rebase:
$ git rebase --onto master server client

Esto viene a decir: “Activa la rama client, averigua los cambios desde el
ancestro común entre las ramas client y server, y aplicalos en la rama master”. Puede parecer un poco complicado, pero los resultados son realmente interesantes.

120

Reorganizar el Trabajo Realizado

FIGURE 3-32
Reorganizando una
rama puntual fuera
de otra rama
puntual

Y, tras esto, ya puedes avanzar la rama principal (ver Figure 3-33):
$ git checkout master
$ git merge client

FIGURE 3-33
Avance rápido de tu
rama master, para
incluir los cambios
de la rama client

Ahora supongamos que decides traerlos (pull) también sobre tu rama server. Puedes reorganizar (rebase) la rama server sobre la rama master sin necesidad siquiera de comprobarlo previamente, usando el comando git rebase
[rama-base] [rama-puntual], el cual activa la rama puntual (server en
este caso) y la aplica sobre la rama base (master en este caso):
$ git rebase master server

Esto vuelca el trabajo de server sobre el de master, tal y como se muestra
en Figure 3-34.

121

CHAPTER 3: Ramificaciones en Git

FIGURE 3-34
Reorganizando la
rama server sobre
la rama master

Después, puedes avanzar rápidamente la rama base (master):
$ git checkout master
$ git merge server

Y por último puedes eliminar las ramas client y server porque ya todo su
contenido ha sido integrado y no las vas a necesitar más, dejando tu registro
tras todo este proceso tal y como se muestra en Figure 3-35:
$ git branch -d client
$ git branch -d server

FIGURE 3-35
Historial final de
confirmaciones de
cambio

Los Peligros de Reorganizar
Ahh…, pero la dicha de la reorganización no la alcanzamos sin sus contrapartidas, las cuales pueden resumirse en una línea:
Nunca reorganices confirmaciones de cambio (commits) que hayas enviado (push) a un repositorio público.
Si sigues esta recomendación, no tendrás problemas. Pero si no lo haces, la
gente te odiará y serás despreciado por tus familiares y amigos.
Cuando reorganizas algo, estás abandonando las confirmaciones de cambio
ya creadas y estás creando unas nuevas; que son similares, pero diferentes. Si
envias (push) confirmaciones (commits) a alguna parte, y otros las recogen
(pull) de allí; y después vas tú y las reescribes con git rebase y las vuelves a
enviar (push); tus colaboradores tendrán que refusionar (re-merge) su trabajo y

122

Reorganizar el Trabajo Realizado

todo se volverá tremendamente complicado cuando intentes recoger (pull) su
trabajo de vuelta sobre el tuyo.
Veamos con un ejemplo como reorganizar trabajo que has hecho público
puede causar problemas. Imagínate que haces un clon desde un servidor central, y luego trabajas sobre él. Tu historial de cambios puede ser algo como esto:

FIGURE 3-36
Clonar un
repositorio y
trabajar sobre él

Ahora, otra persona trabaja también sobre ello, realiza una fusión (merge) y
lleva (push) su trabajo al servidor central. Tú te traes (fetch) sus trabajos y los
fusionas (merge) sobre una nueva rama en tu trabajo, con lo que tu historial
quedaría parecido a esto:

123

CHAPTER 3: Ramificaciones en Git

FIGURE 3-37
Traer (fetch)
algunas
confirmaciones de
cambio (commits) y
fusionarlas (merge)
sobre tu trabajo

A continuación, la persona que había llevado cambios al servidor central decide retroceder y reorganizar su trabajo; haciendo un git push --force para
sobrescribir el registro en el servidor. Tu te traes (fetch) esos nuevos cambios
desde el servidor.

FIGURE 3-38
Alguien envií (push)
confirmaciones
(commits)
reorganizadas,
abandonando las
confirmaciones en
las que tu habías
basado tu trabajo

124

Reorganizar el Trabajo Realizado

Ahora los dos están en un aprieto. Si haces git pull crearás una fusión
confirmada, la cual incluirá ambas líneas del historial, y tu repositorio lucirá así:

FIGURE 3-39
Vuelves a fusionar el
mismo trabajo en
una nueva fusión
confirmada

Si ejecutas git log sobre un historial así, verás dos confirmaciones hechas
por el mismo autor y con la misma fecha y mensaje, lo cual será confuso. Es
más, si luego tu envías (push) ese registro de vuelta al servidor, vas a introducir
todas esas confirmaciones reorganizadas en el servidor central. Lo que puede
confundir aún más a la gente. Era más seguro asumir que el otro desarrollador
no quería que C4 y C6 estuviesen en el historial; por ello había reorganizado su
trabajo de esa manera.

Reorganizar una Reorganización
Si te encuentras en una situación como esta, Git tiene algunos trucos que pueden ayudarte. Si alguien de tu equipo sobreescribe cambios en los que se basaba tu trabajo, tu reto es descubrir qué han sobreescrito y qué te pertenece.
Además de la suma de control SHA-1, Git calcula una suma de control basada en el parche que introduce una confirmación. A esta se le conoce como
“patch-id”.
Si te traes el trabajo que ha sido sobreescrito y lo reorganizas sobre las nuevas confirmaciones de tu compañero, es posible que Git pueda identificar qué
parte correspondía específicamente a tu trabajo y aplicarla de vuelta en la
rama nueva.

125

CHAPTER 3: Ramificaciones en Git

Por ejemplo, en el caso anterior, si en vez de hacer una fusión cuando estábamos en Figure 3-38 ejecutamos git rebase teamone/master, Git hará lo
siguiente:
• Determinar el trabajo que es específico de nuestra rama (C2, C3, C4, C6,
C7)
• Determinar cuáles no son fusiones confirmadas (C2, C3, C4)
• Determinar cuáles no han sido sobreescritas en la rama destino (solo C2 y
C3, pues C4 corresponde al mismo parche que C4')
• Aplicar dichas confirmaciones encima de teamone/master
Así que en vez del resultado que vimos en Figure 3-39, terminaremos con
algo más parecido a Figure 3-40.

FIGURE 3-40
Reorganizar encima
de un trabajo
sobreescrito
reorganizado.

Esto solo funciona si C4 y el C4’ de tu compañero son parches muy similares.
De lo contrario, la reorganización no será capaz de identificar que se trata de un
duplicado y agregará otro parche similar a C4 (lo cual probablemente no podrá
aplicarse limpiamente, pues los cambios ya estarían allí en algún lugar).
También puedes simplificar el proceso si ejecutas git pull --rebase en
vez del tradicional git pull. O, en este caso, puedes hacerlo manualmente
con un git fetch primero, seguido de un git rebase teamone/master.
Si sueles utilizar git pull y quieres que la opción --rebase esté activada
por defecto, puedes asignar el valor de configuración pull.rebase haciendo
algo como esto git config --global pull.rebase true.

126

Recapitulación

Si consideras la reorganización como una manera de limpiar tu trabajo y tus
confirmaciones antes de enviarlas (push), y si solo reorganizas confirmaciones
(commits) que nunca han estado disponibles públicamente, no tendrás problemas. Si reorganizas (rebase) confirmaciones (commits) que ya estaban disponibles públicamente y la gente había basado su trabajo en ellas, entonces
prepárate para tener problemas, frustar a tu equipo y ser despreciado por tus
compañeros.
Si tu compañero o tú ven que aun así es necesario hacerlo en algún momento, asegúrense que todos sepan que deben ejecutar git pull --rebase para
intentar aliviar en lo posible la frustración.

Reorganizar vs. Fusionar
Ahora que has visto en acción la reorganización y la fusión, te preguntarás cuál
es mejor. Antes de responder, repasemos un poco qué representa el historial.
Para algunos, el historial de confirmaciones de tu repositorio es un registro
de todo lo que ha pasado. Un documento histórico, valioso por sí mismo y que
no debería ser alterado. Desde este punto de vista, cambiar el historial de confirmaciones es casi como blasfemar; estarías mintiendo sobre lo que en verdad
ocurrió. ¿Y qué pasa si hay una serie desastrosa de fusiones confirmadas? Nada.
Así fue como ocurrió y el repositorio debería tener un registro de esto para la
posteridad.
La otra forma de verlo es que el historial de confirmaciones es la historia de
cómo se hizo tu proyecto. Tú no publicarías el primer borrador de tu novela, y
el manual de cómo mantener tus programas también debe estar editado con
mucho cuidado. Esta es el área que utiliza herramientas como rebase y
filter-branch para contar la historia de la mejor manera para los futuros lectores.
Ahora, sobre qué es mejor si fusionar o reorganizar: verás que la respuesta
no es tan sencilla. Git es una herramienta poderosa que te permite hacer muchas cosas con tu historial, y cada equipo y cada proyecto es diferente. Ahora
que conoces cómo trabajan ambas herramientas, será cosa tuya decidir cuál de
las dos es mejor para tu situación en particular.
Normalmente, la manera de sacar lo mejor de ambas es reorganizar tu trabajo local, que aun no has compartido, antes de enviarlo a algún lugar; pero
nunca reorganizar nada que ya haya sido compartido.

Recapitulación
Hemos visto los procedimientos básicos de ramificación (branching) y fusión
(merging) en Git. A estas alturas, te sentirás cómodo creando nuevas ramas

127

CHAPTER 3: Ramificaciones en Git

(branch), saltando (checkout) entre ramas para trabajar y fusionando (merge)
ramas entre ellas. También conocerás cómo compartir tus ramas enviándolas
(push) a un servidor compartido, cómo trabajar colaborativamente en ramas
compartidas, y cómo reorganizar (rebase) tus ramas antes de compartirlas. A
continuación, hablaremos sobre lo que necesitas para tener tu propio servidor
de hospedaje Git.

128

Git en el Servidor

4

En este punto, deberías ser capaz de realizar la mayoría de las tareas diarias
para las cuales estarás usando Git. Sin embargo, para poder realizar cualquier
colaboración en Git, necesitarás tener un repositorio remoto Git. Aunque técnicamente puedes enviar y recibir cambios desde repositorios de otros individuos, no se recomienda hacerlo porque, si no tienes cuidado, fácilmente podrías
confudir en que es en lo que se está trabajando. Además, lo deseable es que tus
colaboradores sean capaces de acceder al repositorio incluso si tu computadora no está en línea – muchas veces es útil tener un repositorio confiable en
común. Por lo tanto, el método preferido para colaborar con otra persona es
configurar un repositorio intermedio al cual ambos tengan acceso, y enviar
(push) y recibir (pull) desde allí.
Poner en funcionamiento un servidor Git es un proceso bastante claro. Primero, eliges con qué protocolos ha de comunicarse tu servidor. La primera sección de este capítulo cubrirá los protocolos disponibles, así como los pros y los
contras de cada uno. Las siguientes secciones explicarán algunas configuraciones comunes utilizando dichos protocolos y como poner a funcionar tu servidor con alguno de ellos. Finalmente, revisaremos algunas de las opciones
hospedadas, si no te importa hospedar tu código en el servidor de alguien más
y no quieres tomarte la molestia de configurar y mantener tu propio servidor.
Si no tienes interés en tener tu propio servidor, puedes saltarte hasta la última sección de este capítulo para ver algunas de las opciones para configurar
una cuenta hospedada y seguir al siguiente capítulo, donde discutiremos los
varios pormenores de trabajar en un ambiente de control de fuente distribuído.
Un repositorio remoto es generalmente un repositorio básico – un repositorio Git que no tiene directorio de trabajo. Dado que el repositorio es solamente
utilizado como un punto de colaboración, no hay razín para tener una copia instantánea verificada en el disco; tan solo son datos Git. En los más simples términos, un repositorio básico es el contenido .git del directorio de tu proyecto
y nada más.

129

CHAPTER 4: Git en el Servidor

The Protocols
Git can use four major protocols to transfer data: Local, HTTP, Secure Shell
(SSH) and Git. Here we’ll discuss what they are and in what basic circumstances
you would want (or not want) to use them.

Local Protocol
The most basic is the Local protocol, in which the remote repository is in another directory on disk. This is often used if everyone on your team has access to a
shared filesystem such as an NFS mount, or in the less likely case that everyone
logs in to the same computer. The latter wouldn’t be ideal, because all your
code repository instances would reside on the same computer, making a catastrophic loss much more likely.
If you have a shared mounted filesystem, then you can clone, push to, and
pull from a local file-based repository. To clone a repository like this or to add
one as a remote to an existing project, use the path to the repository as the
URL. For example, to clone a local repository, you can run something like this:
$ git clone /opt/git/project.git

Or you can do this:
$ git clone file:///opt/git/project.git

Git operates slightly differently if you explicitly specify file:// at the beginning of the URL. If you just specify the path, Git tries to use hardlinks or directly
copy the files it needs. If you specify file://, Git fires up the processes that it
normally uses to transfer data over a network which is generally a lot less efficient method of transferring the data. The main reason to specify the file://
prefix is if you want a clean copy of the repository with extraneous references or
objects left out – generally after an import from another version-control system
or something similar (see Chapter 10 for maintenance tasks). We’ll use the normal path here because doing so is almost always faster.
To add a local repository to an existing Git project, you can run something
like this:
$ git remote add local_proj /opt/git/project.git

130

The Protocols

Then, you can push to and pull from that remote as though you were doing
so over a network.
THE PROS
The pros of file-based repositories are that they’re simple and they use existing
file permissions and network access. If you already have a shared filesystem to
which your whole team has access, setting up a repository is very easy. You
stick the bare repository copy somewhere everyone has shared access to and
set the read/write permissions as you would for any other shared directory.
We’ll discuss how to export a bare repository copy for this purpose in “Configurando Git en un servidor”.
This is also a nice option for quickly grabbing work from someone else’s
working repository. If you and a co-worker are working on the same project and
they want you to check something out, running a command like git pull /
home/john/project is often easier than them pushing to a remote server and
you pulling down.
THE CONS
The cons of this method are that shared access is generally more difficult to set
up and reach from multiple locations than basic network access. If you want to
push from your laptop when you’re at home, you have to mount the remote
disk, which can be difficult and slow compared to network-based access.
It’s also important to mention that this isn’t necessarily the fastest option if
you’re using a shared mount of some kind. A local repository is fast only if you
have fast access to the data. A repository on NFS is often slower than the repository over SSH on the same server, allowing Git to run off local disks on each system.

The HTTP Protocols
Git can communicate over HTTP in two different modes. Prior to Git 1.6.6 there
was only one way it could do this which was very simple and generally readonly. In version 1.6.6 a new, smarter protocol was introduced that involved Git
being able to intelligently negotiate data transfer in a manner similar to how it
does over SSH. In the last few years, this new HTTP protocol has become very
popular since it’s simpler for the user and smarter about how it communicates.
The newer version is often referred to as the “Smart” HTTP protocol and the
older way as “Dumb” HTTP. We’ll cover the newer “smart” HTTP protocol first.

131

CHAPTER 4: Git en el Servidor

SMART HTTP
The “smart” HTTP protocol operates very similarly to the SSH or Git protocols
but runs over standard HTTP/S ports and can use various HTTP authentication
mechanisms, meaning it’s often easier on the user than something like SSH,
since you can use things like username/password basic authentication rather
than having to set up SSH keys.
It has probably become the most popular way to use Git now, since it can be
set up to both serve anonymously like the git:// protocol, and can also be
pushed over with authentication and encryption like the SSH protocol. Instead
of having to set up different URLs for these things, you can now use a single URL
for both. If you try to push and the repository requires authentication (which it
normally should), the server can prompt for a username and password. The
same goes for read access.
In fact, for services like GitHub, the URL you use to view the repository online
(for example, “https://github.com/schacon/simplegit”) is the same URL you
can use to clone and, if you have access, push over.
DUMB HTTP
If the server does not respond with a Git HTTP smart service, the Git client will
try to fall back to the simpler “dumb” HTTP protocol. The Dumb protocol expects the bare Git repository to be served like normal files from the web server.
The beauty of the Dumb HTTP protocol is the simplicity of setting it up. Basically, all you have to do is put a bare Git repository under your HTTP document
root and set up a specific post-update hook, and you’re done (See “Git
Hooks”). At that point, anyone who can access the web server under which you
put the repository can also clone your repository. To allow read access to your
repository over HTTP, do something like this:
$
$
$
$
$

cd /var/www/htdocs/
git clone --bare /path/to/git_project gitproject.git
cd gitproject.git
mv hooks/post-update.sample hooks/post-update
chmod a+x hooks/post-update

That’s all. The post-update hook that comes with Git by default runs the
appropriate command (git update-server-info) to make HTTP fetching
and cloning work properly. This command is run when you push to this repository (over SSH perhaps); then, other people can clone via something like

132

The Protocols

$ git clone https://example.com/gitproject.git

In this particular case, we’re using the /var/www/htdocs path that is common for Apache setups, but you can use any static web server – just put the
bare repository in its path. The Git data is served as basic static files (see Chapter 10 for details about exactly how it’s served).
Generally you would either choose to run a read/write Smart HTTP server or
simply have the files accessible as read-only in the Dumb manner. It’s rare to
run a mix of the two services.
THE PROS
We’ll concentrate on the pros of the Smart version of the HTTP protocol.
The simplicity of having a single URL for all types of access and having the
server prompt only when authentication is needed makes things very easy for
the end user. Being able to authenticate with a username and password is also
a big advantage over SSH, since users don’t have to generate SSH keys locally
and upload their public key to the server before being able to interact with it.
For less sophisticated users, or users on systems where SSH is less common,
this is a major advantage in usability. It is also a very fast and efficient protocol,
similar to the SSH one.
You can also serve your repositories read-only over HTTPS, which means you
can encrypt the content transfer; or you can go so far as to make the clients use
specific signed SSL certificates.
Another nice thing is that HTTP/S are such commonly used protocols that
corporate firewalls are often set up to allow traffic through these ports.
THE CONS
Git over HTTP/S can be a little more tricky to set up compared to SSH on some
servers. Other than that, there is very little advantage that other protocols have
over the “Smart” HTTP protocol for serving Git.
If you’re using HTTP for authenticated pushing, providing your credentials is
sometimes more complicated than using keys over SSH. There are however several credential caching tools you can use, including Keychain access on OSX
and Credential Manager on Windows, to make this pretty painless. Read “Credential Storage” to see how to set up secure HTTP password caching on your
system.

133

CHAPTER 4: Git en el Servidor

The SSH Protocol
A common transport protocol for Git when self-hosting is over SSH. This is because SSH access to servers is already set up in most places – and if it isn’t, it’s
easy to do. SSH is also an authenticated network protocol; and because it’s
ubiquitous, it’s generally easy to set up and use.
To clone a Git repository over SSH, you can specify ssh:// URL like this:
$ git clone ssh://user@server/project.git

Or you can use the shorter scp-like syntax for the SSH protocol:
$ git clone user@server:project.git

You can also not specify a user, and Git assumes the user you’re currently
logged in as.
THE PROS
The pros of using SSH are many. First, SSH is relatively easy to set up – SSH daemons are commonplace, many network admins have experience with them,
and many OS distributions are set up with them or have tools to manage them.
Next, access over SSH is secure – all data transfer is encrypted and authenticated. Last, like the HTTP/S, Git and Local protocols, SSH is efficient, making the
data as compact as possible before transferring it.
THE CONS
The negative aspect of SSH is that you can’t serve anonymous access of your
repository over it. People must have access to your machine over SSH to access
it, even in a read-only capacity, which doesn’t make SSH access conducive to
open source projects. If you’re using it only within your corporate network, SSH
may be the only protocol you need to deal with. If you want to allow anonymous read-only access to your projects and also want to use SSH, you’ll have to
set up SSH for you to push over but something else for others to fetch over.

The Git Protocol
Next is the Git protocol. This is a special daemon that comes packaged with Git;
it listens on a dedicated port (9418) that provides a service similar to the SSH

134

Configurando Git en un servidor

protocol, but with absolutely no authentication. In order for a repository to be
served over the Git protocol, you must create the git-daemon-export-ok file
– the daemon won’t serve a repository without that file in it – but other than
that there is no security. Either the Git repository is available for everyone to
clone or it isn’t. This means that there is generally no pushing over this protocol. You can enable push access; but given the lack of authentication, if you turn
on push access, anyone on the internet who finds your project’s URL could push
to your project. Suffice it to say that this is rare.
THE PROS
The Git protocol is often the fastest network transfer protocol available. If
you’re serving a lot of traffic for a public project or serving a very large project
that doesn’t require user authentication for read access, it’s likely that you’ll
want to set up a Git daemon to serve your project. It uses the same datatransfer mechanism as the SSH protocol but without the encryption and authentication overhead.
THE CONS
The downside of the Git protocol is the lack of authentication. It’s generally undesirable for the Git protocol to be the only access to your project. Generally,
you’ll pair it with SSH or HTTPS access for the few developers who have push
(write) access and have everyone else use git:// for read-only access. It’s also
probably the most difficult protocol to set up. It must run its own daemon,
which requires xinetd configuration or the like, which isn’t always a walk in
the park. It also requires firewall access to port 9418, which isn’t a standard
port that corporate firewalls always allow. Behind big corporate firewalls, this
obscure port is commonly blocked.

Configurando Git en un servidor
Ahora vamos a cubrir la creación de un servicio de Git ejecutando estos protocolos en su propio servidor.
Aquí demostraremos los comandos y pasos necesarios para hacer las instalaciones básicas simplificadas en un servidor basado en Linux, aunque
también es posible ejecutar estos servicios en los servidores Mac o Windows. Configurar un servidor de producción dentro de tu infraestructura
sin duda supondrá diferencias en las medidas de seguridad o de las herramientas del sistema operativo, pero se espera que esto le de la idea general de lo que el proceso involucra.

135

CHAPTER 4: Git en el Servidor

Para configurar por primera vez un servidor de Git, hay que exportar un repositorio existente en un nuevo repositorio vacío - un repositorio que no contiene un directorio de trabajo. Esto es generalmente fácil de hacer. Para clonar
el repositorio con el fin de crear un nuevo repositorio vacío, se ejecuta el comando clone con la opción --bare. Por convención, los directorios del repositorio vacío terminan en .git , así:
$ git clone --bare my_project my_project.git
Cloning into bare repository 'my_project.git'...
done.

Deberías tener ahora una copia de los datos del directorio Git en tu directorio my_project.git. Esto es más o menos equivalente a algo así:
$ cp -Rf my_project/.git my_project.git

Hay un par de pequeñas diferencias en el archivo de configuración; pero
para tú propósito, esto es casi la misma cosa. Toma el repositorio Git en sí mismo, sin un directorio de trabajo, y crea un directorio específicamente para él
solo.

Colocando un Repositorio Vacío en un Servidor
Ahora que tienes una copia vacía de tú repositorio, todo lo que necesitas hacer
es ponerlo en un servidor y establecer sus protocolos. Digamos que has configurado un servidor llamado git.example.com que tiene acceso a SSH, y
quieres almacenar todos tus repositorios Git bajo el directorio / opt` / git`. Suponiendo que existe / opt / git en ese servidor, puedes configurar tu nuevo
repositorio copiando tu repositorio vacío a:
$ scp -r my_project.git user@git.example.com:/opt/git

En este punto, otros usuarios que con acceso SSH al mismo servidor que
tiene permisos de lectura-acceso al directorio / opt / git pueden clonar tu
repositorio mediante el comando
$ git clone user@git.example.com:/opt/git/my_project.git

136

Configurando Git en un servidor

Si un usuario accede por medio de SSH a un servidor y tiene permisos de
escritura en el directorio git my_project.git / opt / /, automáticamente
también tendrán acceso push.
Git automáticamente agrega permisos de grupo para la escritura en un repositorio apropiadamente si se ejecuta el comando git init con la opción` -shared`.
$ ssh user@git.example.com
$ cd /opt/git/my_project.git
$ git init --bare --shared

Puedes ver lo fácil que es tomar un repositorio Git, crear una versión vacía, y
colocarlo en un servidor al que tú y tus colaboradores tienen acceso SSH. Ahora
estan listos para colaborar en el mismo proyecto.
Es importante tener en cuenta que esto es literalmente todo lo que necesitas
hacer para ejecutar un útil servidor Git al cual varias personas tendrán acceso sólo tiene que añadir cuentas con acceso SSH a un servidor, y subir un repositorio vacío en alguna parte a la que todos los usuarios puedan leer y escribir.
Estás listo para trabajar. Nada más es necesario.
En las próximas secciones, verás cómo ampliar a configuraciones más sofisticadas. Esta sección incluirá no tener que crear cuentas para cada usuario,
añadiendo permisos de lectura pública a los repositorios, la creación de interfaces de usuario web y más. Sin embargo, ten en cuenta que para colaborar con
un par de personas en un proyecto privado, todo_lo_que_necesitas_es un servidor SSH y un repositorio vacío.

Pequeñas configuraciones
Si tienes un pequeño equipo o acabas de probar Git en tr organización y tienes
sólo unos pocos desarrolladores, las cosas pueden ser simples para tí. Uno de
los aspectos más complicados de configurar un servidor Git es la gestión de
usuarios. Si quieres que algunos repositorios sean de sólo lectura para ciertos
usuarios y lectura / escritura para los demás, el acceso y los permisos pueden
ser un poco más difíciles de organizar.
ACCESO SSH
Si tienes un servidor al que todos los desarrolladores ya tienen acceso SSH, es
generalmente más fácil de configurar el primer repositorio allí, porque no hay
que hacer casi ningún trabajo (como ya vimos en la sección anterior). Si quieres
permisos de acceso más complejas en tus repositorios, puedes manejarlos con

137

CHAPTER 4: Git en el Servidor

los permisos del sistema de archivos normales del sistema operativo donde tu
servidor se ejecuta.
Si deseas colocar los repositorios en un servidor que no tiene cuentas para
todo el mundo en su equipo para el que deseas tener acceso de escritura,
debes configurar el acceso SSH para ellos. Suponiendo que tienes un servidor
con el que hacer esto, ya tiene un servidor SSH instalado, y así es como estás
accediendo al servidor.
Hay algunas maneras con las cuales puedes dar acceso a todo tu equipo. La
primera es la creación de cuentas para todo el mundo, que es sencillo, pero
puede ser engorroso. Podrías no desear ejecutar adduser y establecer contraseñas temporales para cada usuario.
Un segundo método consiste en crear un solo usuario git en la máquina,
preguntar a cada usuario de quién se trata para otorgarle permisos de escritura
para que te envíe una llave SSH pública, y agregar esa llave al archivo
~ / .ssh / authorized_keys de tu nuevo usuario git. En ese momento, todo el mundo podrá acceder a esa máquina mediante el usuario git. Esto no
afecta a los datos commit de ninguna manera - el usuario SSH con el que te
conectas no puede modificar los commits que has registrado.
Otra manera de hacer que tu servidor SSH autentifique desde un servidor
LDAP o desde alguna otra fuente de autentificación centralizada que pudieras
tener ya configurada. Mientras que cada usuario sea capaz de tener acceso shell
a la máquina, cualquier mecanismo de autentificación SSH que se te ocurra debería de funcionar.

Generating Your SSH Public Key
That being said, many Git servers authenticate using SSH public keys. In order
to provide a public key, each user in your system must generate one if they
don’t already have one. This process is similar across all operating systems.
First, you should check to make sure you don’t already have a key. By default, a
user’s SSH keys are stored in that user’s ~/.ssh directory. You can easily check
to see if you have a key already by going to that directory and listing the contents:
$ cd ~/.ssh
$ ls
authorized_keys2
config

id_dsa
id_dsa.pub

known_hosts

You’re looking for a pair of files named something like id_dsa or id_rsa
and a matching file with a .pub extension. The .pub file is your public key, and

138

Setting Up the Server

the other file is your private key. If you don’t have these files (or you don’t even
have a .ssh directory), you can create them by running a program called sshkeygen, which is provided with the SSH package on Linux/Mac systems and
comes with the MSysGit package on Windows:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/schacon/.ssh/id_rsa):
Created directory '/home/schacon/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/schacon/.ssh/id_rsa.
Your public key has been saved in /home/schacon/.ssh/id_rsa.pub.
The key fingerprint is:
d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 schacon@mylaptop.local

First it confirms where you want to save the key (.ssh/id_rsa), and then it
asks twice for a passphrase, which you can leave empty if you don’t want to
type a password when you use the key.
Now, each user that does this has to send their public key to you or whoever
is administrating the Git server (assuming you’re using an SSH server setup that
requires public keys). All they have to do is copy the contents of the .pub file
and e-mail it. The public keys look something like this:
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU
GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3
Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA
t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En
mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx
NrRFi9wrf+M7Q== schacon@mylaptop.local

For a more in-depth tutorial on creating an SSH key on multiple operating
systems, see the GitHub guide on SSH keys at https://help.github.com/articles/
generating-ssh-keys.

Setting Up the Server
Let’s walk through setting up SSH access on the server side. In this example,
you’ll use the authorized_keys method for authenticating your users. We also assume you’re running a standard Linux distribution like Ubuntu. First, you
create a git user and a .ssh directory for that user.

139

CHAPTER 4: Git en el Servidor

$
$
$
$
$

sudo adduser git
su git
cd
mkdir .ssh && chmod 700 .ssh
touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

Next, you need to add some developer SSH public keys to the authorized_keys file for the git user. Let’s assume you have some trusted public
keys and have saved them to temporary files. Again, the public keys look something like this:
$ cat /tmp/id_rsa.john.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4L
ojG6rs6hPB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4k
Yjh6541NYsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9Ez
Sdfd8AcCIicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myiv
O7TCUSBdLQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPq
dAv8JggJICUvax2T9va5 gsg-keypair

You just append them to the git user’s authorized_keys file in its .ssh
directory:
$ cat /tmp/id_rsa.john.pub >> ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.josie.pub >> ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.jessica.pub >> ~/.ssh/authorized_keys

Now, you can set up an empty repository for them by running git init
with the --bare option, which initializes the repository without a working directory:
$ cd /opt/git
$ mkdir project.git
$ cd project.git
$ git init --bare
Initialized empty Git repository in /opt/git/project.git/

Then, John, Josie, or Jessica can push the first version of their project into
that repository by adding it as a remote and pushing up a branch. Note that
someone must shell onto the machine and create a bare repository every time
you want to add a project. Let’s use gitserver as the hostname of the server
on which you’ve set up your git user and repository. If you’re running it inter-

140

Setting Up the Server

nally, and you set up DNS for gitserver to point to that server, then you can
use the commands pretty much as is (assuming that myproject is an existing
project with files in it):
#
$
$
$
$
$
$

on Johns computer
cd myproject
git init
git add .
git commit -m 'initial commit'
git remote add origin git@gitserver:/opt/git/project.git
git push origin master

At this point, the others can clone it down and push changes back up just as
easily:
$
$
$
$
$

git clone git@gitserver:/opt/git/project.git
cd project
vim README
git commit -am 'fix for the README file'
git push origin master

With this method, you can quickly get a read/write Git server up and running
for a handful of developers.
You should note that currently all these users can also log into the server
and get a shell as the git user. If you want to restrict that, you will have to
change the shell to something else in the passwd file.
You can easily restrict the git user to only doing Git activities with a limited
shell tool called git-shell that comes with Git. If you set this as your git
user’s login shell, then the git user can’t have normal shell access to your server. To use this, specify git-shell instead of bash or csh for your user’s login
shell. To do so, you must first add git-shell to /etc/shells if it’s not already
there:
$ cat /etc/shells
# see if `git-shell` is already in there. If not...
$ which git-shell
# make sure git-shell is installed on your system.
$ sudo vim /etc/shells # and add the path to git-shell from last command

Now you can edit the shell for a user using chsh :
$ sudo chsh git

# and enter the path to git-shell, usually: /usr/bin/git-shell

141

CHAPTER 4: Git en el Servidor

Now, the git user can only use the SSH connection to push and pull Git repositories and can’t shell onto the machine. If you try, you’ll see a login rejection like this:
$ ssh git@gitserver
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to gitserver closed.

Now Git network commands will still work just fine but the users won’t be
able to get a shell. As the output states, you can also set up a directory in the
git user’s home directory that customizes the git-shell command a bit. For
instance, you can restrict the Git commands that the server will accept or you
can customize the message that users see if they try to SSH in like that. Run git
help shell for more information on customizing the shell.

Git Daemon
Next we’ll set up a daemon serving repositories over the “Git” protocol. This is
common choice for fast, unauthenticated access to your Git data. Remember
that since it’s not an authenticated service, anything you serve over this protocol is public within its network.
If you’re running this on a server outside your firewall, it should only be used
for projects that are publicly visible to the world. If the server you’re running it
on is inside your firewall, you might use it for projects that a large number of
people or computers (continuous integration or build servers) have read-only
access to, when you don’t want to have to add an SSH key for each.
In any case, the Git protocol is relatively easy to set up. Basically, you need to
run this command in a daemonized manner:
git daemon --reuseaddr --base-path=/opt/git/ /opt/git/

--reuseaddr allows the server to restart without waiting for old connections to time out, the --base-path option allows people to clone projects
without specifying the entire path, and the path at the end tells the Git daemon
where to look for repositories to export. If you’re running a firewall, you’ll also
need to punch a hole in it at port 9418 on the box you’re setting this up on.
You can daemonize this process a number of ways, depending on the operating system you’re running. On an Ubuntu machine, you can use an Upstart
script. So, in the following file

142

Git Daemon

/etc/event.d/local-git-daemon

you put this script:
start on startup
stop on shutdown
exec /usr/bin/git daemon \
--user=git --group=git \
--reuseaddr \
--base-path=/opt/git/ \
/opt/git/
respawn

For security reasons, it is strongly encouraged to have this daemon run as a
user with read-only permissions to the repositories – you can easily do this by
creating a new user git-ro and running the daemon as them. For the sake of
simplicity we’ll simply run it as the same git user that git-shell is running as.
When you restart your machine, your Git daemon will start automatically
and respawn if it goes down. To get it running without having to reboot, you
can run this:
initctl start local-git-daemon

On other systems, you may want to use xinetd, a script in your sysvinit
system, or something else – as long as you get that command daemonized and
watched somehow.
Next, you have to tell Git which repositories to allow unauthenticated Git
server-based access to. You can do this in each repository by creating a file
name git-daemon-export-ok.
$ cd /path/to/project.git
$ touch git-daemon-export-ok

The presence of that file tells Git that it’s OK to serve this project without authentication.

143

CHAPTER 4: Git en el Servidor

Smart HTTP
We now have authenticated access though SSH and unauthenticated access
through git://, but there is also a protocol that can do both at the same time.
Setting up Smart HTTP is basically just enabling a CGI script that is provided
with Git called git-http-backend on the server. This CGI will read the path
and headers sent by a git fetch or git push to an HTTP URL and determine
if the client can communicate over HTTP (which is true for any client since version 1.6.6). If the CGI sees that the client is smart, it will communicate smartly
with it, otherwise it will fall back to the dumb behavior (so it is backward compatible for reads with older clients).
Let’s walk through a very basic setup. We’ll set this up with Apache as the
CGI server. If you don’t have Apache setup, you can do so on a Linux box with
something like this:
$ sudo apt-get install apache2 apache2-utils
$ a2enmod cgi alias env

This also enables the mod_cgi, mod_alias, and mod_env modules, which
are all needed for this to work properly.
Next we need to add some things to the Apache configuration to run the
git-http-backend as the handler for anything coming into the /git path of
your web server.
SetEnv GIT_PROJECT_ROOT /opt/git
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/

If you leave out GIT_HTTP_EXPORT_ALL environment variable, then Git will
only serve to unauthenticated clients the repositories with the git-daemonexport-ok file in them, just like the Git daemon did.
Then you’ll have to tell Apache to allow requests to that path with something like this:

Options ExecCGI Indexes
Order allow,deny
Allow from all
Require all granted


144

GitWeb

Finally you’ll want to make writes be authenticated somehow, possibly with
an Auth block like this:

AuthType Basic
AuthName "Git Access"
AuthUserFile /opt/git/.htpasswd
Require valid-user


That will require you to create a .htaccess file containing the passwords of
all the valid users. Here is an example of adding a “schacon” user to the file:
$ htdigest -c /opt/git/.htpasswd "Git Access" schacon

There are tons of ways to have Apache authenticate users, you’ll have to
choose and implement one of them. This is just the simplest example we could
come up with. You’ll also almost certainly want to set this up over SSL so all this
data is encrypted.
We don’t want to go too far down the rabbit hole of Apache configuration
specifics, since you could well be using a different server or have different authentication needs. The idea is that Git comes with a CGI called git-httpbackend that when invoked will do all the negotiation to send and receive data
over HTTP. It does not implement any authentication itself, but that can easily
be controlled at the layer of the web server that invokes it. You can do this with
nearly any CGI-capable web server, so go with the one that you know best.
For more information on configuring authentication in Apache, check
out the Apache docs here: http://httpd.apache.org/docs/current/howto/
auth.html

GitWeb
Now that you have basic read/write and read-only access to your project, you
may want to set up a simple web-based visualizer. Git comes with a CGI script
called GitWeb that is sometimes used for this.

145

CHAPTER 4: Git en el Servidor

FIGURE 4-1
The GitWeb webbased user interface.

If you want to check out what GitWeb would look like for your project, Git
comes with a command to fire up a temporary instance if you have a lightweight server on your system like lighttpd or webrick. On Linux machines,
lighttpd is often installed, so you may be able to get it to run by typing git
instaweb in your project directory. If you’re running a Mac, Leopard comes
preinstalled with Ruby, so webrick may be your best bet. To start instaweb
with a non-lighttpd handler, you can run it with the --httpd option.
$ git instaweb --httpd=webrick
[2009-02-21 10:02:21] INFO WEBrick 1.3.1
[2009-02-21 10:02:21] INFO ruby 1.8.6 (2008-03-03) [universal-darwin9.0]

That starts up an HTTPD server on port 1234 and then automatically starts a
web browser that opens on that page. It’s pretty easy on your part. When you’re
done and want to shut down the server, you can run the same command with
the --stop option:
$ git instaweb --httpd=webrick --stop

146

GitWeb

If you want to run the web interface on a server all the time for your team or
for an open source project you’re hosting, you’ll need to set up the CGI script to
be served by your normal web server. Some Linux distributions have a gitweb
package that you may be able to install via apt or yum, so you may want to try
that first. We’ll walk through installing GitWeb manually very quickly. First, you
need to get the Git source code, which GitWeb comes with, and generate the
custom CGI script:
$ git clone git://git.kernel.org/pub/scm/git/git.git
$ cd git/
$ make GITWEB_PROJECTROOT="/opt/git" prefix=/usr gitweb
SUBDIR gitweb
SUBDIR ../
make[2]: `GIT-VERSION-FILE' is up to date.
GEN gitweb.cgi
GEN static/gitweb.js
$ sudo cp -Rf gitweb /var/www/

Notice that you have to tell the command where to find your Git repositories
with the GITWEB_PROJECTROOT variable. Now, you need to make Apache use
CGI for that script, for which you can add a VirtualHost:

ServerName gitserver
DocumentRoot /var/www/gitweb

Options ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch
AllowOverride All
order allow,deny
Allow from all
AddHandler cgi-script cgi
DirectoryIndex gitweb.cgi



Again, GitWeb can be served with any CGI or Perl capable web server; if you
prefer to use something else, it shouldn’t be difficult to set up. At this point, you
should be able to visit http://gitserver/ to view your repositories online.

147

CHAPTER 4: Git en el Servidor

GitLab
GitWeb is pretty simplistic though. If you’re looking for a more modern, fully
featured Git server, there are some several open source solutions out there that
you can install instead. As GitLab is one of the more popular ones, we’ll cover
installing and using it as an example. This is a bit more complex than the GitWeb option and likely requires more maintenance, but it is a much more fully
featured option.

Installation
GitLab is a database-backed web application, so its installation is a bit more involved than some other git servers. Fortunately, this process is very welldocumented and supported.
There are a few methods you can pursue to install GitLab. To get something
up and running quickly, you can download a virtual machine image or a oneclick installer from https://bitnami.com/stack/gitlab, and tweak the configuration to match your particular environment. One nice touch Bitnami has included is the login screen (accessed by typing alt-→); it tells you the IP address and
default username and password for the installed GitLab.

FIGURE 4-2
The Bitnami GitLab
virtual machine
login screen.

For anything else, follow the guidance in the GitLab Community Edition readme, which can be found at https://gitlab.com/gitlab-org/gitlab-ce/tree/
master. There you’ll find assistance for installing GitLab using Chef recipes, a

148

GitLab

virtual machine on Digital Ocean, and RPM and DEB packages (which, as of this
writing, are in beta). There’s also “unofficial” guidance on getting GitLab running with non-standard operating systems and databases, a fully-manual installation script, and many other topics.

Administration
GitLab’s administration interface is accessed over the web. Simply point your
browser to the hostname or IP address where GitLab is installed, and log in as
an admin user. The default username is admin@local.host, and the default
password is 5iveL!fe (which you will be prompted to change as soon as you
enter it). Once logged in, click the “Admin area” icon in the menu at the top
right.

FIGURE 4-3
The “Admin area”
item in the GitLab
menu.

USERS
Users in GitLab are accounts that correspond to people. User accounts don’t
have a lot of complexity; mainly it’s a collection of personal information attached to login data. Each user account comes with a namespace, which is a logical
grouping of projects that belong to that user. If the user jane had a project
named project, that project’s url would be http://server/jane/project.

149

CHAPTER 4: Git en el Servidor

FIGURE 4-4
The GitLab user
administration
screen.

Removing a user can be done in two ways. “Blocking” a user prevents them
from logging into the GitLab instance, but all of the data under that user’s
namespace will be preserved, and commits signed with that user’s email address will still link back to their profile.
“Destroying” a user, on the other hand, completely removes them from the
database and filesystem. All projects and data in their namespace is removed,
and any groups they own will also be removed. This is obviously a much more
permanent and destructive action, and its uses are rare.
GROUPS
A GitLab group is an assemblage of projects, along with data about how users
can access those projects. Each group has a project namespace (the same way
that users do), so if the group training has a project materials, its url would
be http://server/training/materials.

150

GitLab

FIGURE 4-5
The GitLab group
administration
screen.

Each group is associated with a number of users, each of which has a level of
permissions for the group’s projects and the group itself. These range from
“Guest” (issues and chat only) to “Owner” (full control of the group, its members, and its projects). The types of permissions are too numerous to list here,
but GitLab has a helpful link on the administration screen.
PROJECTS
A GitLab project roughly corresponds to a single git repository. Every project
belongs to a single namespace, either a user or a group. If the project belongs
to a user, the owner of the project has direct control over who has access to the
project; if the project belongs to a group, the group’s user-level permissions will
also take effect.
Every project also has a visibility level, which controls who has read access
to that project’s pages and repository. If a project is Private, the project’s owner
must explicitly grant access to specific users. An Internal project is visible to any
logged-in user, and a Public project is visible to anyone. Note that this controls
both git “fetch” access as well as access to the web UI for that project.
HOOKS
GitLab includes support for hooks, both at a project or system level. For either
of these, the GitLab server will perform an HTTP POST with some descriptive
JSON whenever relevant events occur. This is a great way to connect your git
repositories and GitLab instance to the rest of your development automation,
such as CI servers, chat rooms, or deployment tools.

151

CHAPTER 4: Git en el Servidor

Basic Usage
The first thing you’ll want to do with GitLab is create a new project. This is accomplished by clicking the “+” icon on the toolbar. You’ll be asked for the
project’s name, which namespace it should belong to, and what its visibility level should be. Most of what you specify here isn’t permanent, and can be readjusted later through the settings interface. Click “Create Project”, and you’re
done.
Once the project exists, you’ll probably want to connect it with a local Git
repository. Each project is accessible over HTTPS or SSH, either of which can be
used to configure a Git remote. The URLs are visible at the top of the project’s
home page. For an existing local repository, this command will create a remote
named gitlab to the hosted location:
$ git remote add gitlab https://server/namespace/project.git

If you don’t have a local copy of the repository, you can simply do this:
$ git clone https://server/namespace/project.git

The web UI provides access to several useful views of the repository itself.
Each project’s home page shows recent activity, and links along the top will
lead you to views of the project’s files and commit log.

Working Together
The simplest way of working together on a GitLab project is by giving another
user direct push access to the git repository. You can add a user to a project by
going to the “Members” section of that project’s settings, and associating the
new user with an access level (the different access levels are discussed a bit in
“Groups”). By giving a user an access level of “Developer” or above, that user
can push commits and branches directly to the repository with impunity.
Another, more decoupled way of collaboration is by using merge requests.
This feature enables any user that can see a project to contribute to it in a controlled way. Users with direct access can simply create a branch, push commits
to it, and open a merge request from their branch back into master or any other branch. Users who don’t have push permissions for a repository can “fork” it
(create their own copy), push commits to that copy, and open a merge request
from their fork back to the main project. This model allows the owner to be in

152

Third Party Hosted Options

full control of what goes into the repository and when, while allowing contributions from untrusted users.
Merge requests and issues are the main units of long-lived discussion in GitLab. Each merge request allows a line-by-line discussion of the proposed
change (which supports a lightweight kind of code review), as well as a general
overall discussion thread. Both can be assigned to users, or organized into milestones.
This section is focused mainly on the Git-related features of GitLab, but as a
mature project, it provides many other features to help your team work together, such as project wikis and system maintenance tools. One benefit to GitLab is
that, once the server is set up and running, you’ll rarely need to tweak a configuration file or access the server via SSH; most administration and general usage
can be accomplished through the in-browser interface.

Third Party Hosted Options
If you don’t want to go through all of the work involved in setting up your own
Git server, you have several options for hosting your Git projects on an external
dedicated hosting site. Doing so offers a number of advantages: a hosting site is
generally quick to set up and easy to start projects on, and no server maintenance or monitoring is involved. Even if you set up and run your own server internally, you may still want to use a public hosting site for your open source
code – it’s generally easier for the open source community to find and help you
with.
These days, you have a huge number of hosting options to choose from,
each with different advantages and disadvantages. To see an up-to-date list,
check out the GitHosting page on the main Git wiki at https://
git.wiki.kernel.org/index.php/GitHosting
We’ll cover using GitHub in detail in Chapter 6, as it is the largest Git host
out there and you may need to interact with projects hosted on it in any case,
but there are dozens more to choose from should you not want to set up your
own Git server.

Resumen
Tienes varias opciones para obtener un repositorio Git remoto y ponerlo a funcionar para que puedas colaborar con otras personas o compartir tu trabajo.
Mantener tu propio servidor te da control y te permite correr tu servidor
dentro de tu propio cortafuegos, pero tal servidor generalmente requiere una
importante cantidad de tu tiempo para configurar y mantener. Si almacenas tus
datos en un servidor hospedado, es fácil de configurar y mantener; sin embar-

153

CHAPTER 4: Git en el Servidor

go, tienes que ser capaz de mantener tu código en los servidores de alguien
más, y agunas organizaciones no te lo permitirán.
Debería ser un proceso claro determinar que solución o combinación de soluciones es apropiada para tí y para tu organización.

154

Distributed Git

5

Now that you have a remote Git repository set up as a point for all the developers to share their code, and you’re familiar with basic Git commands in a local
workflow, you’ll look at how to utilize some of the distributed workflows that
Git affords you.
In this chapter, you’ll see how to work with Git in a distributed environment
as a contributor and an integrator. That is, you’ll learn how to contribute code
successfully to a project and make it as easy on you and the project maintainer
as possible, and also how to maintain a project successfully with a number of
developers contributing.

Distributed Workflows
Unlike Centralized Version Control Systems (CVCSs), the distributed nature of
Git allows you to be far more flexible in how developers collaborate on projects.
In centralized systems, every developer is a node working more or less equally
on a central hub. In Git, however, every developer is potentially both a node
and a hub – that is, every developer can both contribute code to other repositories and maintain a public repository on which others can base their work and
which they can contribute to. This opens a vast range of workflow possibilities
for your project and/or your team, so we’ll cover a few common paradigms that
take advantage of this flexibility. We’ll go over the strengths and possible weaknesses of each design; you can choose a single one to use, or you can mix and
match features from each.

Centralized Workflow
In centralized systems, there is generally a single collaboration model–the centralized workflow. One central hub, or repository, can accept code, and everyone synchronizes their work to it. A number of developers are nodes – consumers of that hub – and synchronize to that one place.

155

CHAPTER 5: Distributed Git

FIGURE 5-1
Centralized
workflow.

This means that if two developers clone from the hub and both make
changes, the first developer to push their changes back up can do so with no
problems. The second developer must merge in the first one’s work before
pushing changes up, so as not to overwrite the first developer’s changes. This
concept is as true in Git as it is in Subversion (or any CVCS), and this model
works perfectly well in Git.
If you are already comfortable with a centralized workflow in your company
or team, you can easily continue using that workflow with Git. Simply set up a
single repository, and give everyone on your team push access; Git won’t let
users overwrite each other. Say John and Jessica both start working at the
same time. John finishes his change and pushes it to the server. Then Jessica
tries to push her changes, but the server rejects them. She is told that she’s trying to push non-fast-forward changes and that she won’t be able to do so until
she fetches and merges. This workflow is attractive to a lot of people because
it’s a paradigm that many are familiar and comfortable with.
This is also not limited to small teams. With Git’s branching model, it’s possible for hundreds of developers to successfully work on a single project through
dozens of branches simultaneously.

Integration-Manager Workflow
Because Git allows you to have multiple remote repositories, it’s possible to
have a workflow where each developer has write access to their own public
repository and read access to everyone else’s. This scenario often includes a
canonical repository that represents the “official” project. To contribute to that
project, you create your own public clone of the project and push your changes
to it. Then, you can send a request to the maintainer of the main project to pull
in your changes. The maintainer can then add your repository as a remote, test

156

Distributed Workflows

your changes locally, merge them into their branch, and push back to their
repository. The process works as follows (see Figure 5-2):
1. The project maintainer pushes to their public repository.
2. A contributor clones that repository and makes changes.
3. The contributor pushes to their own public copy.
4. The contributor sends the maintainer an e-mail asking them to pull
changes.
5. The maintainer adds the contributor’s repo as a remote and merges locally.
6. The maintainer pushes merged changes to the main repository.

FIGURE 5-2
Integrationmanager workflow.

This is a very common workflow with hub-based tools like GitHub or GitLab,
where it’s easy to fork a project and push your changes into your fork for everyone to see. One of the main advantages of this approach is that you can continue to work, and the maintainer of the main repository can pull in your changes
at any time. Contributors don’t have to wait for the project to incorporate their
changes – each party can work at their own pace.

Dictator and Lieutenants Workflow
This is a variant of a multiple-repository workflow. It’s generally used by huge
projects with hundreds of collaborators; one famous example is the Linux kernel. Various integration managers are in charge of certain parts of the repository; they’re called lieutenants. All the lieutenants have one integration manager
known as the benevolent dictator. The benevolent dictator’s repository serves
as the reference repository from which all the collaborators need to pull. The
process works like this (see Figure 5-3):

157

CHAPTER 5: Distributed Git

1. Regular developers work on their topic branch and rebase their work on
top of master. The master branch is that of the dictator.
2. Lieutenants merge the developers’ topic branches into their master
branch.
3. The dictator merges the lieutenants’ master branches into the dictator’s
master branch.
4. The dictator pushes their master to the reference repository so the other
developers can rebase on it.

FIGURE 5-3
Benevolent dictator
workflow.

This kind of workflow isn’t common, but can be useful in very big projects, or
in highly hierarchical environments. It allows the project leader (the dictator) to
delegate much of the work and collect large subsets of code at multiple points
before integrating them.

Workflows Summary
These are some commonly used workflows that are possible with a distributed
system like Git, but you can see that many variations are possible to suit your
particular real-world workflow. Now that you can (hopefully) determine which
workflow combination may work for you, we’ll cover some more specific examples of how to accomplish the main roles that make up the different flows. In
the next section, you’ll learn about a few common patterns for contributing to a
project.

158

Contributing to a Project

Contributing to a Project
The main difficulty with describing how to contribute to a project is that there
are a huge number of variations on how it’s done. Because Git is very flexible,
people can and do work together in many ways, and it’s problematic to describe how you should contribute – every project is a bit different. Some of the
variables involved are active contributor count, chosen workflow, your commit
access, and possibly the external contribution method.
The first variable is active contributor count – how many users are actively
contributing code to this project, and how often? In many instances, you’ll have
two or three developers with a few commits a day, or possibly less for somewhat dormant projects. For larger companies or projects, the number of developers could be in the thousands, with hundreds or thousands of commits coming in each day. This is important because with more and more developers, you
run into more issues with making sure your code applies cleanly or can be easily merged. Changes you submit may be rendered obsolete or severely broken
by work that is merged in while you were working or while your changes were
waiting to be approved or applied. How can you keep your code consistently up
to date and your commits valid?
The next variable is the workflow in use for the project. Is it centralized, with
each developer having equal write access to the main codeline? Does the
project have a maintainer or integration manager who checks all the patches?
Are all the patches peer-reviewed and approved? Are you involved in that process? Is a lieutenant system in place, and do you have to submit your work to
them first?
The next issue is your commit access. The workflow required in order to contribute to a project is much different if you have write access to the project than
if you don’t. If you don’t have write access, how does the project prefer to accept contributed work? Does it even have a policy? How much work are you
contributing at a time? How often do you contribute?
All these questions can affect how you contribute effectively to a project and
what workflows are preferred or available to you. We’ll cover aspects of each of
these in a series of use cases, moving from simple to more complex; you should
be able to construct the specific workflows you need in practice from these examples.

Commit Guidelines
Before we start looking at the specific use cases, here’s a quick note about commit messages. Having a good guideline for creating commits and sticking to it
makes working with Git and collaborating with others a lot easier. The Git

159

CHAPTER 5: Distributed Git

project provides a document that lays out a number of good tips for creating
commits from which to submit patches – you can read it in the Git source code
in the Documentation/SubmittingPatches file.
First, you don’t want to submit any whitespace errors. Git provides an easy
way to check for this – before you commit, run git diff --check, which
identifies possible whitespace errors and lists them for you.

FIGURE 5-4
Output of git diff

--check.

If you run that command before committing, you can tell if you’re about to
commit whitespace issues that may annoy other developers.
Next, try to make each commit a logically separate changeset. If you can, try
to make your changes digestible – don’t code for a whole weekend on five different issues and then submit them all as one massive commit on Monday.
Even if you don’t commit during the weekend, use the staging area on Monday
to split your work into at least one commit per issue, with a useful message per
commit. If some of the changes modify the same file, try to use git add -patch to partially stage files (covered in detail in “Interactive Staging”). The
project snapshot at the tip of the branch is identical whether you do one commit or five, as long as all the changes are added at some point, so try to make
things easier on your fellow developers when they have to review your changes.
This approach also makes it easier to pull out or revert one of the changesets if
you need to later. “Rewriting History” describes a number of useful Git tricks
for rewriting history and interactively staging files – use these tools to help craft
a clean and understandable history before sending the work to someone else.
The last thing to keep in mind is the commit message. Getting in the habit of
creating quality commit messages makes using and collaborating with Git a lot
easier. As a general rule, your messages should start with a single line that’s no

160

Contributing to a Project

more than about 50 characters and that describes the changeset concisely, followed by a blank line, followed by a more detailed explanation. The Git project
requires that the more detailed explanation include your motivation for the
change and contrast its implementation with previous behavior – this is a good
guideline to follow. It’s also a good idea to use the imperative present tense in
these messages. In other words, use commands. Instead of “I added tests for”
or “Adding tests for,” use “Add tests for.” Here is a template originally written by
Tim Pope:
Short (50 chars or less) summary of changes
More detailed explanatory text, if necessary. Wrap it to
about 72 characters or so. In some contexts, the first
line is treated as the subject of an email and the rest of
the text as the body. The blank line separating the
summary from the body is critical (unless you omit the body
entirely); tools like rebase can get confused if you run
the two together.
Further paragraphs come after blank lines.
- Bullet points are okay, too
- Typically a hyphen or asterisk is used for the bullet,
preceded by a single space, with blank lines in
between, but conventions vary here

If all your commit messages look like this, things will be a lot easier for you
and the developers you work with. The Git project has well-formatted commit
messages – try running git log --no-merges there to see what a nicely formatted project-commit history looks like.
In the following examples, and throughout most of this book, for the sake of
brevity this book doesn’t have nicely-formatted messages like this; instead, we
use the -m option to git commit. Do as we say, not as we do.

Private Small Team
The simplest setup you’re likely to encounter is a private project with one or
two other developers. “Private,” in this context, means closed-source – not accessible to the outside world. You and the other developers all have push access to the repository.
In this environment, you can follow a workflow similar to what you might do
when using Subversion or another centralized system. You still get the advantages of things like offline committing and vastly simpler branching and merg-

161

CHAPTER 5: Distributed Git

ing, but the workflow can be very similar; the main difference is that merges
happen client-side rather than on the server at commit time. Let’s see what it
might look like when two developers start to work together with a shared
repository. The first developer, John, clones the repository, makes a change,
and commits locally. (The protocol messages have been replaced with ... in
these examples to shorten them somewhat.)
# John's Machine
$ git clone john@githost:simplegit.git
Initialized empty Git repository in /home/john/simplegit/.git/
...
$ cd simplegit/
$ vim lib/simplegit.rb
$ git commit -am 'removed invalid default value'
[master 738ee87] removed invalid default value
1 files changed, 1 insertions(+), 1 deletions(-)

The second developer, Jessica, does the same thing – clones the repository
and commits a change:
# Jessica's Machine
$ git clone jessica@githost:simplegit.git
Initialized empty Git repository in /home/jessica/simplegit/.git/
...
$ cd simplegit/
$ vim TODO
$ git commit -am 'add reset task'
[master fbff5bc] add reset task
1 files changed, 1 insertions(+), 0 deletions(-)

Now, Jessica pushes her work up to the server:
# Jessica's Machine
$ git push origin master
...
To jessica@githost:simplegit.git
1edee6b..fbff5bc master -> master

John tries to push his change up, too:
# John's Machine
$ git push origin master
To john@githost:simplegit.git

162

Contributing to a Project

! [rejected]
master -> master (non-fast forward)
error: failed to push some refs to 'john@githost:simplegit.git'

John isn’t allowed to push because Jessica has pushed in the meantime.
This is especially important to understand if you’re used to Subversion, because you’ll notice that the two developers didn’t edit the same file. Although
Subversion automatically does such a merge on the server if different files are
edited, in Git you must merge the commits locally. John has to fetch Jessica’s
changes and merge them in before he will be allowed to push:
$ git fetch origin
...
From john@githost:simplegit
+ 049d078...fbff5bc master

-> origin/master

At this point, John’s local repository looks something like this:

FIGURE 5-5
John’s divergent
history.

John has a reference to the changes Jessica pushed up, but he has to merge
them into his own work before he is allowed to push:
$ git merge origin/master
Merge made by recursive.

163

CHAPTER 5: Distributed Git

TODO |
1 +
1 files changed, 1 insertions(+), 0 deletions(-)

The merge goes smoothly – John’s commit history now looks like this:

FIGURE 5-6
John’s repository
after merging
origin/master.

Now, John can test his code to make sure it still works properly, and then he
can push his new merged work up to the server:
$ git push origin master
...
To john@githost:simplegit.git
fbff5bc..72bbc59 master -> master

Finally, John’s commit history looks like this:

FIGURE 5-7
John’s history after
pushing to the
origin server.

164

Contributing to a Project

In the meantime, Jessica has been working on a topic branch. She’s created
a topic branch called issue54 and done three commits on that branch. She
hasn’t fetched John’s changes yet, so her commit history looks like this:

FIGURE 5-8
Jessica’s topic
branch.

Jessica wants to sync up with John, so she fetches:
# Jessica's Machine
$ git fetch origin
...
From jessica@githost:simplegit
fbff5bc..72bbc59 master

-> origin/master

That pulls down the work John has pushed up in the meantime. Jessica’s
history now looks like this:

FIGURE 5-9
Jessica’s history
after fetching John’s
changes.

Jessica thinks her topic branch is ready, but she wants to know what she has
to merge into her work so that she can push. She runs git log to find out:
$ git log --no-merges issue54..origin/master
commit 738ee872852dfaa9d6634e0dea7a324040193016
Author: John Smith 

165

CHAPTER 5: Distributed Git

Date:

Fri May 29 16:01:27 2009 -0700

removed invalid default value

The issue54..origin/master syntax is a log filter that asks Git to only
show the list of commits that are on the latter branch (in this case origin/
master) that are not on the first branch (in this case issue54). We’ll go over
this syntax in detail in “Commit Ranges”.
For now, we can see from the output that there is a single commit that John
has made that Jessica has not merged in. If she merges origin/master, that is
the single commit that will modify her local work.
Now, Jessica can merge her topic work into her master branch, merge John’s
work (origin/master) into her master branch, and then push back to the
server again. First, she switches back to her master branch to integrate all this
work:
$ git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.

She can merge either origin/master or issue54 first – they’re both upstream, so the order doesn’t matter. The end snapshot should be identical no
matter which order she chooses; only the history will be slightly different. She
chooses to merge in issue54 first:
$ git merge issue54
Updating fbff5bc..4af4298
Fast forward
README
|
1 +
lib/simplegit.rb |
6 +++++2 files changed, 6 insertions(+), 1 deletions(-)

No problems occur; as you can see it was a simple fast-forward. Now Jessica
merges in John’s work (origin/master):
$ git merge origin/master
Auto-merging lib/simplegit.rb
Merge made by recursive.
lib/simplegit.rb |
2 +1 files changed, 1 insertions(+), 1 deletions(-)

166

Contributing to a Project

Everything merges cleanly, and Jessica’s history looks like this:

FIGURE 5-10
Jessica’s history
after merging John’s
changes.

Now origin/master is reachable from Jessica’s master branch, so she
should be able to successfully push (assuming John hasn’t pushed again in the
meantime):
$ git push origin master
...
To jessica@githost:simplegit.git
72bbc59..8059c15 master -> master

Each developer has committed a few times and merged each other’s work
successfully.

FIGURE 5-11
Jessica’s history
after pushing all
changes back to the
server.

That is one of the simplest workflows. You work for a while, generally in a
topic branch, and merge into your master branch when it’s ready to be integrated. When you want to share that work, you merge it into your own master
branch, then fetch and merge origin/master if it has changed, and finally
push to the master branch on the server. The general sequence is something
like this:

167

CHAPTER 5: Distributed Git

FIGURE 5-12
General sequence of
events for a simple
multiple-developer
Git workflow.

Private Managed Team
In this next scenario, you’ll look at contributor roles in a larger private group.
You’ll learn how to work in an environment where small groups collaborate on
features and then those team-based contributions are integrated by another
party.
Let’s say that John and Jessica are working together on one feature, while
Jessica and Josie are working on a second. In this case, the company is using a

168

Contributing to a Project

type of integration-manager workflow where the work of the individual groups
is integrated only by certain engineers, and the master branch of the main repo
can be updated only by those engineers. In this scenario, all work is done in
team-based branches and pulled together by the integrators later.
Let’s follow Jessica’s workflow as she works on her two features, collaborating in parallel with two different developers in this environment. Assuming she
already has her repository cloned, she decides to work on featureA first. She
creates a new branch for the feature and does some work on it there:
# Jessica's Machine
$ git checkout -b featureA
Switched to a new branch 'featureA'
$ vim lib/simplegit.rb
$ git commit -am 'add limit to log function'
[featureA 3300904] add limit to log function
1 files changed, 1 insertions(+), 1 deletions(-)

At this point, she needs to share her work with John, so she pushes her featureA branch commits up to the server. Jessica doesn’t have push access to
the master branch – only the integrators do – so she has to push to another
branch in order to collaborate with John:
$ git push -u origin featureA
...
To jessica@githost:simplegit.git
* [new branch]
featureA -> featureA

Jessica e-mails John to tell him that she’s pushed some work into a branch
named featureA and he can look at it now. While she waits for feedback from
John, Jessica decides to start working on featureB with Josie. To begin, she
starts a new feature branch, basing it off the server’s master branch:
# Jessica's Machine
$ git fetch origin
$ git checkout -b featureB origin/master
Switched to a new branch 'featureB'

Now, Jessica makes a couple of commits on the featureB branch:
$ vim lib/simplegit.rb
$ git commit -am 'made the ls-tree function recursive'

169

CHAPTER 5: Distributed Git

[featureB e5b0fdc] made the ls-tree function recursive
1 files changed, 1 insertions(+), 1 deletions(-)
$ vim lib/simplegit.rb
$ git commit -am 'add ls-files'
[featureB 8512791] add ls-files
1 files changed, 5 insertions(+), 0 deletions(-)

Jessica’s repository looks like this:

FIGURE 5-13
Jessica’s initial
commit history.

She’s ready to push up her work, but gets an e-mail from Josie that a branch
with some initial work on it was already pushed to the server as featureBee.
Jessica first needs to merge those changes in with her own before she can push
to the server. She can then fetch Josie’s changes down with git fetch:
$ git fetch origin
...
From jessica@githost:simplegit
* [new branch]
featureBee -> origin/featureBee

Jessica can now merge this into the work she did with git merge:
$ git merge origin/featureBee
Auto-merging lib/simplegit.rb
Merge made by recursive.
lib/simplegit.rb |
4 ++++
1 files changed, 4 insertions(+), 0 deletions(-)

170

Contributing to a Project

There is a bit of a problem – she needs to push the merged work in her featureB branch to the featureBee branch on the server. She can do so by specifying the local branch followed by a colon (:) followed by the remote branch to
the git push command:
$ git push -u origin featureB:featureBee
...
To jessica@githost:simplegit.git
fba9af8..cd685d1 featureB -> featureBee

This is called a refspec. See “The Refspec” for a more detailed discussion of
Git refspecs and different things you can do with them. Also notice the -u flag;
this is short for --set-upstream, which configures the branches for easier
pushing and pulling later.
Next, John e-mails Jessica to say he’s pushed some changes to the featureA branch and asks her to verify them. She runs a git fetch to pull down
those changes:
$ git fetch origin
...
From jessica@githost:simplegit
3300904..aad881d featureA

-> origin/featureA

Then, she can see what has been changed with git log:
$ git log featureA..origin/featureA
commit aad881d154acdaeb2b6b18ea0e827ed8a6d671e6
Author: John Smith 
Date:
Fri May 29 19:57:33 2009 -0700
changed log output to 30 from 25

Finally, she merges John’s work into her own featureA branch:
$ git checkout featureA
Switched to branch 'featureA'
$ git merge origin/featureA
Updating 3300904..aad881d
Fast forward
lib/simplegit.rb |
10 +++++++++1 files changed, 9 insertions(+), 1 deletions(-)

171

CHAPTER 5: Distributed Git

Jessica wants to tweak something, so she commits again and then pushes
this back up to the server:
$ git commit -am 'small tweak'
[featureA 774b3ed] small tweak
1 files changed, 1 insertions(+), 1 deletions(-)
$ git push
...
To jessica@githost:simplegit.git
3300904..774b3ed featureA -> featureA

Jessica’s commit history now looks something like this:

FIGURE 5-14
Jessica’s history
after committing on
a feature branch.

Jessica, Josie, and John inform the integrators that the featureA and featureBee branches on the server are ready for integration into the mainline. Affterthe integrators merge these branches into the mainline, a fetch will bring
down the new merge commit, making the history look like this:

172

Contributing to a Project

FIGURE 5-15
Jessica’s history
after merging both
her topic branches.

Many groups switch to Git because of this ability to have multiple teams
working in parallel, merging the different lines of work late in the process. The
ability of smaller subgroups of a team to collaborate via remote branches
without necessarily having to involve or impede the entire team is a huge benefit of Git. The sequence for the workflow you saw here is something like this:

173

CHAPTER 5: Distributed Git

FIGURE 5-16
Basic sequence of
this managed-team
workflow.

Forked Public Project
Contributing to public projects is a bit different. Because you don’t have the
permissions to directly update branches on the project, you have to get the
work to the maintainers some other way. This first example describes contributing via forking on Git hosts that support easy forking. Many hosting sites support this (including GitHub, BitBucket, Google Code, repo.or.cz, and others),
and many project maintainers expect this style of contribution. The next section deals with projects that prefer to accept contributed patches via e-mail.
First, you’ll probably want to clone the main repository, create a topic
branch for the patch or patch series you’re planning to contribute, and do your
work there. The sequence looks basically like this:

174

Contributing to a Project

$
$
$
#
$
#
$

git clone (url)
cd project
git checkout -b featureA
(work)
git commit
(work)
git commit

You may want to use rebase -i to squash your work down to a single
commit, or rearrange the work in the commits to make the patch easier
for the maintainer to review – see “Rewriting History” for more information about interactive rebasing.

When your branch work is finished and you’re ready to contribute it back to
the maintainers, go to the original project page and click the “Fork” button, creating your own writable fork of the project. You then need to add in this new
repository URL as a second remote, in this case named myfork:
$ git remote add myfork (url)

Then you need to push your work up to it. It’s easiest to push the topic
branch you’re working on up to your repository, rather than merging into your
master branch and pushing that up. The reason is that if the work isn’t accepted or is cherry picked, you don’t have to rewind your master branch. If the
maintainers merge, rebase, or cherry-pick your work, you’ll eventually get it
back via pulling from their repository anyhow:
$ git push -u myfork featureA

When your work has been pushed up to your fork, you need to notify the
maintainer. This is often called a pull request, and you can either generate it via
the website – GitHub has its own Pull Request mechanism that we’ll go over in
Chapter 6 – or you can run the git request-pull command and e-mail the
output to the project maintainer manually.
The request-pull command takes the base branch into which you want
your topic branch pulled and the Git repository URL you want them to pull
from, and outputs a summary of all the changes you’re asking to be pulled in.
For instance, if Jessica wants to send John a pull request, and she’s done two
commits on the topic branch she just pushed up, she can run this:

175

CHAPTER 5: Distributed Git

$ git request-pull origin/master myfork
The following changes since commit 1edee6b1d61823a2de3b09c160d7080b8d1b3a40:
John Smith (1):
added a new function
are available in the git repository at:
git://githost/simplegit.git featureA
Jessica Smith (2):
add limit to log function
change log output to 30 from 25
lib/simplegit.rb |
10 +++++++++1 files changed, 9 insertions(+), 1 deletions(-)

The output can be sent to the maintainer–it tells them where the work was
branched from, summarizes the commits, and tells where to pull this work
from.
On a project for which you’re not the maintainer, it’s generally easier to have
a branch like master always track origin/master and to do your work in topic branches that you can easily discard if they’re rejected. Having work themes
isolated into topic branches also makes it easier for you to rebase your work if
the tip of the main repository has moved in the meantime and your commits no
longer apply cleanly. For example, if you want to submit a second topic of work
to the project, don’t continue working on the topic branch you just pushed up –
start over from the main repository’s master branch:
$
#
$
$
#
$

git checkout -b featureB origin/master
(work)
git commit
git push myfork featureB
(email maintainer)
git fetch origin

Now, each of your topics is contained within a silo – similar to a patch queue
– that you can rewrite, rebase, and modify without the topics interfering or interdepending on each other, like so:

176

Contributing to a Project

FIGURE 5-17
Initial commit
history with
featureB work.

Let’s say the project maintainer has pulled in a bunch of other patches and
tried your first branch, but it no longer cleanly merges. In this case, you can try
to rebase that branch on top of origin/master, resolve the conflicts for the
maintainer, and then resubmit your changes:
$ git checkout featureA
$ git rebase origin/master
$ git push -f myfork featureA

This rewrites your history to now look like Figure 5-18.

FIGURE 5-18
Commit history after

featureA work.

Because you rebased the branch, you have to specify the -f to your push
command in order to be able to replace the featureA branch on the server
with a commit that isn’t a descendant of it. An alternative would be to push this
new work to a different branch on the server (perhaps called featureAv2).
Let’s look at one more possible scenario: the maintainer has looked at work
in your second branch and likes the concept but would like you to change an
implementation detail. You’ll also take this opportunity to move the work to be

177

CHAPTER 5: Distributed Git

based off the project’s current master branch. You start a new branch based off
the current origin/master branch, squash the featureB changes there, resolve any conflicts, make the implementation change, and then push that up as
a new branch:

$
$
#
$
$

git checkout -b featureBv2 origin/master
git merge --no-commit --squash featureB
(change implementation)
git commit
git push myfork featureBv2

The --squash option takes all the work on the merged branch and squashes it into one non-merge commit on top of the branch you’re on. The --nocommit option tells Git not to automatically record a commit. This allows you
to introduce all the changes from another branch and then make more changes
before recording the new commit.
Now you can send the maintainer a message that you’ve made the requested changes and they can find those changes in your featureBv2 branch.

FIGURE 5-19
Commit history after

featureBv2 work.

Public Project over E-Mail
Many projects have established procedures for accepting patches – you’ll need
to check the specific rules for each project, because they will differ. Since there
are several older, larger projects which accept patches via a developer mailing
list, we’ll go over an example of that now.
The workflow is similar to the previous use case – you create topic branches
for each patch series you work on. The difference is how you submit them to
the project. Instead of forking the project and pushing to your own writable version, you generate e-mail versions of each commit series and e-mail them to
the developer mailing list:

178

Contributing to a Project

$
#
$
#
$

git checkout -b topicA
(work)
git commit
(work)
git commit

Now you have two commits that you want to send to the mailing list. You use

git format-patch to generate the mbox-formatted files that you can e-mail
to the list – it turns each commit into an e-mail message with the first line of the
commit message as the subject and the rest of the message plus the patch that
the commit introduces as the body. The nice thing about this is that applying a
patch from an e-mail generated with format-patch preserves all the commit
information properly.
$ git format-patch -M origin/master
0001-add-limit-to-log-function.patch
0002-changed-log-output-to-30-from-25.patch

The format-patch command prints out the names of the patch files it creates. The -M switch tells Git to look for renames. The files end up looking like
this:
$ cat 0001-add-limit-to-log-function.patch
From 330090432754092d704da8e76ca5c05c198e71a8 Mon Sep 17 00:00:00 2001
From: Jessica Smith 
Date: Sun, 6 Apr 2008 10:17:23 -0700
Subject: [PATCH 1/2] add limit to log function
Limit log functionality to the first 20
--lib/simplegit.rb |
2 +1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index 76f47bc..f9815f1 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -14,7 +14,7 @@ class SimpleGit
end

+

def log(treeish = 'master')
command("git log #{treeish}")
command("git log -n 20 #{treeish}")

179

CHAPTER 5: Distributed Git

end
def ls_tree(treeish = 'master')
-2.1.0

You can also edit these patch files to add more information for the e-mail list
that you don’t want to show up in the commit message. If you add text between
the --- line and the beginning of the patch (the diff --git line), then developers can read it; but applying the patch excludes it.
To e-mail this to a mailing list, you can either paste the file into your e-mail
program or send it via a command-line program. Pasting the text often causes
formatting issues, especially with “smarter” clients that don’t preserve newlines and other whitespace appropriately. Luckily, Git provides a tool to help
you send properly formatted patches via IMAP, which may be easier for you.
We’ll demonstrate how to send a patch via Gmail, which happens to be the email agent we know best; you can read detailed instructions for a number of
mail programs at the end of the aforementioned Documentation/SubmittingPatches file in the Git source code.
First, you need to set up the imap section in your ~/.gitconfig file. You
can set each value separately with a series of git config commands, or you
can add them manually, but in the end your config file should look something
like this:
[imap]
folder = "[Gmail]/Drafts"
host = imaps://imap.gmail.com
user = user@gmail.com
pass = p4ssw0rd
port = 993
sslverify = false

If your IMAP server doesn’t use SSL, the last two lines probably aren’t necessary, and the host value will be imap:// instead of imaps://. When that is set
up, you can use git send-email to place the patch series in the Drafts folder
of the specified IMAP server:
$ git send-email *.patch
0001-added-limit-to-log-function.patch
0002-changed-log-output-to-30-from-25.patch
Who should the emails appear to be from? [Jessica Smith ]
Emails will be sent from: Jessica Smith 

180

Maintaining a Project

Who should the emails be sent to? jessica@example.com
Message-ID to be used as In-Reply-To for the first email? y

Then, Git spits out a bunch of log information looking something like this for
each patch you’re sending:
(mbox) Adding cc: Jessica Smith  from
\line 'From: Jessica Smith '
OK. Log says:
Sendmail: /usr/sbin/sendmail -i jessica@example.com
From: Jessica Smith 
To: jessica@example.com
Subject: [PATCH 1/2] added limit to log function
Date: Sat, 30 May 2009 13:29:15 -0700
Message-Id: <1243715356-61726-1-git-send-email-jessica@example.com>
X-Mailer: git-send-email 1.6.2.rc1.20.g8c5b.dirty
In-Reply-To: 
References: 
Result: OK

At this point, you should be able to go to your Drafts folder, change the To
field to the mailing list you’re sending the patch to, possibly CC the maintainer
or person responsible for that section, and send it off.

Summary
This section has covered a number of common workflows for dealing with several very different types of Git projects you’re likely to encounter, and introduced a couple of new tools to help you manage this process. Next, you’ll see
how to work the other side of the coin: maintaining a Git project. You’ll learn
how to be a benevolent dictator or integration manager.

Maintaining a Project
In addition to knowing how to effectively contribute to a project, you’ll likely
need to know how to maintain one. This can consist of accepting and applying
patches generated via format-patch and e-mailed to you, or integrating
changes in remote branches for repositories you’ve added as remotes to your
project. Whether you maintain a canonical repository or want to help by verifying or approving patches, you need to know how to accept work in a way that is
clearest for other contributors and sustainable by you over the long run.

181

CHAPTER 5: Distributed Git

Working in Topic Branches
When you’re thinking of integrating new work, it’s generally a good idea to try it
out in a topic branch – a temporary branch specifically made to try out that
new work. This way, it’s easy to tweak a patch individually and leave it if it’s not
working until you have time to come back to it. If you create a simple branch
name based on the theme of the work you’re going to try, such as ruby_client or something similarly descriptive, you can easily remember it if you have
to abandon it for a while and come back later. The maintainer of the Git project
tends to namespace these branches as well – such as sc/ruby_client, where
sc is short for the person who contributed the work. As you’ll remember, you
can create the branch based off your master branch like this:
$ git branch sc/ruby_client master

Or, if you want to also switch to it immediately, you can use the checkout -

b option:
$ git checkout -b sc/ruby_client master

Now you’re ready to add your contributed work into this topic branch and
determine if you want to merge it into your longer-term branches.

Applying Patches from E-mail
If you receive a patch over e-mail that you need to integrate into your project,
you need to apply the patch in your topic branch to evaluate it. There are two
ways to apply an e-mailed patch: with git apply or with git am.
APPLYING A PATCH WITH APPLY
If you received the patch from someone who generated it with the git diff or
a Unix diff command (which is not recommended; see the next section), you
can apply it with the git apply command. Assuming you saved the patch
at /tmp/patch-ruby-client.patch, you can apply the patch like this:
$ git apply /tmp/patch-ruby-client.patch

182

Maintaining a Project

This modifies the files in your working directory. It’s almost identical to running a patch -p1 command to apply the patch, although it’s more paranoid
and accepts fewer fuzzy matches than patch. It also handles file adds, deletes,
and renames if they’re described in the git diff format, which patch won’t
do. Finally, git apply is an “apply all or abort all” model where either everything is applied or nothing is, whereas patch can partially apply patchfiles,
leaving your working directory in a weird state. git apply is overall much
more conservative than patch. It won’t create a commit for you – after running
it, you must stage and commit the changes introduced manually.
You can also use git apply to see if a patch applies cleanly before you try actually applying it – you can run git apply --check with the patch:
$ git apply --check 0001-seeing-if-this-helps-the-gem.patch
error: patch failed: ticgit.gemspec:1
error: ticgit.gemspec: patch does not apply

If there is no output, then the patch should apply cleanly. This command also exits with a non-zero status if the check fails, so you can use it in scripts if
you want.
APPLYING A PATCH WITH AM
If the contributor is a Git user and was good enough to use the format-patch
command to generate their patch, then your job is easier because the patch
contains author information and a commit message for you. If you can, encourage your contributors to use format-patch instead of diff to generate patches for you. You should only have to use git apply for legacy patches and
things like that.
To apply a patch generated by format-patch, you use git am . Technically,
git am is built to read an mbox file, which is a simple, plain-text format for
storing one or more e-mail messages in one text file. It looks something like
this:
From 330090432754092d704da8e76ca5c05c198e71a8 Mon Sep 17 00:00:00 2001
From: Jessica Smith 
Date: Sun, 6 Apr 2008 10:17:23 -0700
Subject: [PATCH 1/2] add limit to log function
Limit log functionality to the first 20

183

CHAPTER 5: Distributed Git

This is the beginning of the output of the format-patch command that you
saw in the previous section. This is also a valid mbox e-mail format. If someone
has e-mailed you the patch properly using git send-email, and you download
that into an mbox format, then you can point git am to that mbox file, and it
will start applying all the patches it sees. If you run a mail client that can save
several e-mails out in mbox format, you can save entire patch series into a file
and then use git am to apply them one at a time.
However, if someone uploaded a patch file generated via format-patch to
a ticketing system or something similar, you can save the file locally and then
pass that file saved on your disk to git am to apply it:
$ git am 0001-limit-log-function.patch
Applying: add limit to log function

You can see that it applied cleanly and automatically created the new commit for you. The author information is taken from the e-mail’s From and Date
headers, and the message of the commit is taken from the Subject and body
(before the patch) of the e-mail. For example, if this patch was applied from the
mbox example above, the commit generated would look something like this:
$ git log --pretty=fuller -1
commit 6c5e70b984a60b3cecd395edd5b48a7575bf58e0
Author:
Jessica Smith 
AuthorDate: Sun Apr 6 10:17:23 2008 -0700
Commit:
Scott Chacon 
CommitDate: Thu Apr 9 09:19:06 2009 -0700
add limit to log function
Limit log functionality to the first 20

The Commit information indicates the person who applied the patch and the
time it was applied. The Author information is the individual who originally
created the patch and when it was originally created.
But it’s possible that the patch won’t apply cleanly. Perhaps your main
branch has diverged too far from the branch the patch was built from, or the
patch depends on another patch you haven’t applied yet. In that case, the git
am process will fail and ask you what you want to do:
$ git am 0001-seeing-if-this-helps-the-gem.patch
Applying: seeing if this helps the gem
error: patch failed: ticgit.gemspec:1
error: ticgit.gemspec: patch does not apply

184

Maintaining a Project

Patch failed at 0001.
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".

This command puts conflict markers in any files it has issues with, much like
a conflicted merge or rebase operation. You solve this issue much the same way
– edit the file to resolve the conflict, stage the new file, and then run git am -resolved to continue to the next patch:
$ (fix the file)
$ git add ticgit.gemspec
$ git am --resolved
Applying: seeing if this helps the gem

If you want Git to try a bit more intelligently to resolve the conflict, you can
pass a -3 option to it, which makes Git attempt a three-way merge. This option
isn’t on by default because it doesn’t work if the commit the patch says it was
based on isn’t in your repository. If you do have that commit – if the patch was
based on a public commit – then the -3 option is generally much smarter about
applying a conflicting patch:
$ git am -3 0001-seeing-if-this-helps-the-gem.patch
Applying: seeing if this helps the gem
error: patch failed: ticgit.gemspec:1
error: ticgit.gemspec: patch does not apply
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.

In this case, this patch had already been applied. Without the -3 option, it
looks like a conflict.
If you’re applying a number of patches from an mbox, you can also run the
am command in interactive mode, which stops at each patch it finds and asks if
you want to apply it:
$ git am -3 -i mbox
Commit Body is:
-------------------------seeing if this helps the gem

185

CHAPTER 5: Distributed Git

-------------------------Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all

This is nice if you have a number of patches saved, because you can view the
patch first if you don’t remember what it is, or not apply the patch if you’ve already done so.
When all the patches for your topic are applied and committed into your
branch, you can choose whether and how to integrate them into a longerrunning branch.

Checking Out Remote Branches
If your contribution came from a Git user who set up their own repository, pushed a number of changes into it, and then sent you the URL to the repository and
the name of the remote branch the changes are in, you can add them as a remote and do merges locally.
For instance, if Jessica sends you an e-mail saying that she has a great new
feature in the ruby-client branch of her repository, you can test it by adding
the remote and checking out that branch locally:
$ git remote add jessica git://github.com/jessica/myproject.git
$ git fetch jessica
$ git checkout -b rubyclient jessica/ruby-client

If she e-mails you again later with another branch containing another great
feature, you can fetch and check out because you already have the remote setup.
This is most useful if you’re working with a person consistently. If someone
only has a single patch to contribute once in a while, then accepting it over email may be less time consuming than requiring everyone to run their own
server and having to continually add and remove remotes to get a few patches.
You’re also unlikely to want to have hundreds of remotes, each for someone
who contributes only a patch or two. However, scripts and hosted services may
make this easier – it depends largely on how you develop and how your contributors develop.
The other advantage of this approach is that you get the history of the commits as well. Although you may have legitimate merge issues, you know where
in your history their work is based; a proper three-way merge is the default
rather than having to supply a -3 and hope the patch was generated off a public commit to which you have access.

186

Maintaining a Project

If you aren’t working with a person consistently but still want to pull from
them in this way, you can provide the URL of the remote repository to the git
pull command. This does a one-time pull and doesn’t save the URL as a remote reference:
$ git pull https://github.com/onetimeguy/project
From https://github.com/onetimeguy/project
* branch
HEAD
-> FETCH_HEAD
Merge made by recursive.

Determining What Is Introduced
Now you have a topic branch that contains contributed work. At this point, you
can determine what you’d like to do with it. This section revisits a couple of
commands so you can see how you can use them to review exactly what you’ll
be introducing if you merge this into your main branch.
It’s often helpful to get a review of all the commits that are in this branch but
that aren’t in your master branch. You can exclude commits in the master
branch by adding the --not option before the branch name. This does the
same thing as the master..contrib format that we used earlier. For example,
if your contributor sends you two patches and you create a branch called contrib and applied those patches there, you can run this:
$ git log contrib --not master
commit 5b6235bd297351589efc4d73316f0a68d484f118
Author: Scott Chacon 
Date:
Fri Oct 24 09:53:59 2008 -0700
seeing if this helps the gem
commit 7482e0d16d04bea79d0dba8988cc78df655f16a0
Author: Scott Chacon 
Date:
Mon Oct 22 19:38:36 2008 -0700
updated the gemspec to hopefully work better

To see what changes each commit introduces, remember that you can pass
the -p option to git log and it will append the diff introduced to each commit.
To see a full diff of what would happen if you were to merge this topic
branch with another branch, you may have to use a weird trick to get the correct results. You may think to run this:

187

CHAPTER 5: Distributed Git

$ git diff master

This command gives you a diff, but it may be misleading. If your master
branch has moved forward since you created the topic branch from it, then
you’ll get seemingly strange results. This happens because Git directly compares the snapshots of the last commit of the topic branch you’re on and the
snapshot of the last commit on the master branch. For example, if you’ve added a line in a file on the master branch, a direct comparison of the snapshots
will look like the topic branch is going to remove that line.
If master is a direct ancestor of your topic branch, this isn’t a problem; but if
the two histories have diverged, the diff will look like you’re adding all the new
stuff in your topic branch and removing everything unique to the master
branch.
What you really want to see are the changes added to the topic branch – the
work you’ll introduce if you merge this branch with master. You do that by having Git compare the last commit on your topic branch with the first common
ancestor it has with the master branch.
Technically, you can do that by explicitly figuring out the common ancestor
and then running your diff on it:
$ git merge-base contrib master
36c7dba2c95e6bbb78dfa822519ecfec6e1ca649
$ git diff 36c7db

However, that isn’t convenient, so Git provides another shorthand for doing
the same thing: the triple-dot syntax. In the context of the diff command, you
can put three periods after another branch to do a diff between the last commit of the branch you’re on and its common ancestor with another branch:
$ git diff master...contrib

This command shows you only the work your current topic branch has introduced since its common ancestor with master. That is a very useful syntax to
remember.

Integrating Contributed Work
When all the work in your topic branch is ready to be integrated into a more
mainline branch, the question is how to do it. Furthermore, what overall work-

188

Maintaining a Project

flow do you want to use to maintain your project? You have a number of
choices, so we’ll cover a few of them.
MERGING WORKFLOWS
One simple workflow merges your work into your master branch. In this scenario, you have a master branch that contains basically stable code. When you
have work in a topic branch that you’ve done or that someone has contributed
and you’ve verified, you merge it into your master branch, delete the topic
branch, and then continue the process. If we have a repository with work in two
branches named ruby_client and php_client that looks like Figure 5-20
and merge ruby_client first and then php_client next, then your history
will end up looking like Figure 5-21.

FIGURE 5-20
History with several
topic branches.

FIGURE 5-21
After a topic branch
merge.

189

CHAPTER 5: Distributed Git

That is probably the simplest workflow, but it can possibly be problematic if
you’re dealing with larger or more stable projects where you want to be really
careful about what you introduce.
If you have a more important project, you might want to use a two-phase
merge cycle. In this scenario, you have two long-running branches, master and
develop, in which you determine that master is updated only when a very stable release is cut and all new code is integrated into the develop branch. You
regularly push both of these branches to the public repository. Each time you
have a new topic branch to merge in (Figure 5-22), you merge it into develop
(Figure 5-23); then, when you tag a release, you fast-forward master to wherever the now-stable develop branch is (Figure 5-24).

FIGURE 5-22
Before a topic
branch merge.

FIGURE 5-23
After a topic branch
merge.

190

Maintaining a Project

FIGURE 5-24
After a project
release.

This way, when people clone your project’s repository, they can either check
out master to build the latest stable version and keep up to date on that easily,
or they can check out develop, which is the more cutting-edge stuff. You can
also continue this concept, having an integrate branch where all the work is
merged together. Then, when the codebase on that branch is stable and passes
tests, you merge it into a develop branch; and when that has proven itself stable for a while, you fast-forward your master branch.
LARGE-MERGING WORKFLOWS
The Git project has four long-running branches: master, next, and pu (proposed updates) for new work, and maint for maintenance backports. When
new work is introduced by contributors, it’s collected into topic branches in the
maintainer’s repository in a manner similar to what we’ve described (see
Figure 5-25). At this point, the topics are evaluated to determine whether
they’re safe and ready for consumption or whether they need more work. If
they’re safe, they’re merged into next, and that branch is pushed up so everyone can try the topics integrated together.

191

CHAPTER 5: Distributed Git

FIGURE 5-25
Managing a complex
series of parallel
contributed topic
branches.

If the topics still need work, they’re merged into pu instead. When it’s determined that they’re totally stable, the topics are re-merged into master and are
then rebuilt from the topics that were in next but didn’t yet graduate to master. This means master almost always moves forward, next is rebased occasionally, and pu is rebased even more often:

FIGURE 5-26
Merging contributed
topic branches into
long-term
integration
branches.

192

Maintaining a Project

When a topic branch has finally been merged into master, it’s removed from
the repository. The Git project also has a maint branch that is forked off from
the last release to provide backported patches in case a maintenance release is
required. Thus, when you clone the Git repository, you have four branches that
you can check out to evaluate the project in different stages of development,
depending on how cutting edge you want to be or how you want to contribute;
and the maintainer has a structured workflow to help them vet new contributions.
REBASING AND CHERRY PICKING WORKFLOWS
Other maintainers prefer to rebase or cherry-pick contributed work on top of
their master branch, rather than merging it in, to keep a mostly linear history.
When you have work in a topic branch and have determined that you want to
integrate it, you move to that branch and run the rebase command to rebuild
the changes on top of your current master (or develop, and so on) branch. If
that works well, you can fast-forward your master branch, and you’ll end up
with a linear project history.
The other way to move introduced work from one branch to another is to
cherry-pick it. A cherry-pick in Git is like a rebase for a single commit. It takes
the patch that was introduced in a commit and tries to reapply it on the branch
you’re currently on. This is useful if you have a number of commits on a topic
branch and you want to integrate only one of them, or if you only have one
commit on a topic branch and you’d prefer to cherry-pick it rather than run rebase. For example, suppose you have a project that looks like this:

FIGURE 5-27
Example history
before a cherry-pick.

If you want to pull commit e43a6 into your master branch, you can run

193

CHAPTER 5: Distributed Git

$ git cherry-pick e43a6fd3e94888d76779ad79fb568ed180e5fcdf
Finished one cherry-pick.
[master]: created a0a41a9: "More friendly message when locking the index fails."
3 files changed, 17 insertions(+), 3 deletions(-)

This pulls the same change introduced in e43a6, but you get a new commit
SHA-1 value, because the date applied is different. Now your history looks like
this:

FIGURE 5-28
History after cherrypicking a commit on
a topic branch.

Now you can remove your topic branch and drop the commits you didn’t
want to pull in.
RERERE
If you’re doing lots of merging and rebasing, or you’re maintaining a long-lived
topic branch, Git has a feature called “rerere” that can help.
Rerere stands for “reuse recorded resolution” – it’s a way of shortcutting
manual conflict resolution. When rerere is enabled, Git will keep a set of preand post-images from successful merges, and if it notices that there’s a conflict
that looks exactly like one you’ve already fixed, it’ll just use the fix from last
time, without bothering you with it.
This feature comes in two parts: a configuration setting and a command. The
configuration setting is rerere.enabled, and it’s handy enough to put in your
global config:

194

Maintaining a Project

$ git config --global rerere.enabled true

Now, whenever you do a merge that resolves conflicts, the resolution will be
recorded in the cache in case you need it in the future.
If you need to, you can interact with the rerere cache using the git rerere
command. When it’s invoked alone, Git checks its database of resolutions and
tries to find a match with any current merge conflicts and resolve them (although this is done automatically if rerere.enabled is set to true). There are
also subcommands to see what will be recorded, to erase specific resolution
from the cache, and to clear the entire cache. We will cover rerere in more detail
in “Rerere”.

Tagging Your Releases
When you’ve decided to cut a release, you’ll probably want to drop a tag so you
can re-create that release at any point going forward. You can create a new tag
as discussed in Chapter 2. If you decide to sign the tag as the maintainer, the
tagging may look something like this:
$ git tag -s v1.5 -m 'my signed 1.5 tag'
You need a passphrase to unlock the secret key for
user: "Scott Chacon "
1024-bit DSA key, ID F721C45A, created 2009-02-09

If you do sign your tags, you may have the problem of distributing the public
PGP key used to sign your tags. The maintainer of the Git project has solved this
issue by including their public key as a blob in the repository and then adding a
tag that points directly to that content. To do this, you can figure out which key
you want by running gpg --list-keys:
$ gpg --list-keys
/Users/schacon/.gnupg/pubring.gpg
--------------------------------pub
1024D/F721C45A 2009-02-09 [expires: 2010-02-09]
uid
Scott Chacon 
sub
2048g/45D02282 2009-02-09 [expires: 2010-02-09]

Then, you can directly import the key into the Git database by exporting it
and piping that through git hash-object, which writes a new blob with
those contents into Git and gives you back the SHA-1 of the blob:

195

CHAPTER 5: Distributed Git

$ gpg -a --export F721C45A | git hash-object -w --stdin
659ef797d181633c87ec71ac3f9ba29fe5775b92

Now that you have the contents of your key in Git, you can create a tag that
points directly to it by specifying the new SHA-1 value that the hash-object
command gave you:
$ git tag -a maintainer-pgp-pub 659ef797d181633c87ec71ac3f9ba29fe5775b92

If you run git push --tags, the maintainer-pgp-pub tag will be shared
with everyone. If anyone wants to verify a tag, they can directly import your
PGP key by pulling the blob directly out of the database and importing it into
GPG:
$ git show maintainer-pgp-pub | gpg --import

They can use that key to verify all your signed tags. Also, if you include instructions in the tag message, running git show  will let you give the
end user more specific instructions about tag verification.

Generating a Build Number
Because Git doesn’t have monotonically increasing numbers like v123 or the
equivalent to go with each commit, if you want to have a human-readable
name to go with a commit, you can run git describe on that commit. Git
gives you the name of the nearest tag with the number of commits on top of
that tag and a partial SHA-1 value of the commit you’re describing:
$ git describe master
v1.6.2-rc1-20-g8c5b85c

This way, you can export a snapshot or build and name it something understandable to people. In fact, if you build Git from source code cloned from the
Git repository, git --version gives you something that looks like this. If
you’re describing a commit that you have directly tagged, it gives you the tag
name.
The git describe command favors annotated tags (tags created with the
-a or -s flag), so release tags should be created this way if you’re using git

196

Maintaining a Project

describe, to ensure the commit is named properly when described. You can
also use this string as the target of a checkout or show command, although it
relies on the abbreviated SHA-1 value at the end, so it may not be valid forever.
For instance, the Linux kernel recently jumped from 8 to 10 characters to ensure
SHA-1 object uniqueness, so older git describe output names were invalidated.

Preparing a Release
Now you want to release a build. One of the things you’ll want to do is create an
archive of the latest snapshot of your code for those poor souls who don’t use
Git. The command to do this is git archive:
$ git archive master --prefix='project/' | gzip > `git describe master`.tar.gz
$ ls *.tar.gz
v1.6.2-rc1-20-g8c5b85c.tar.gz

If someone opens that tarball, they get the latest snapshot of your project
under a project directory. You can also create a zip archive in much the same
way, but by passing the --format=zip option to git archive:
$ git archive master --prefix='project/' --format=zip > `git describe master`.zip

You now have a nice tarball and a zip archive of your project release that you
can upload to your website or e-mail to people.

The Shortlog
It’s time to e-mail your mailing list of people who want to know what’s happening in your project. A nice way of quickly getting a sort of changelog of what has
been added to your project since your last release or e-mail is to use the git
shortlog command. It summarizes all the commits in the range you give it; for
example, the following gives you a summary of all the commits since your last
release, if your last release was named v1.0.1:
$ git shortlog --no-merges master --not v1.0.1
Chris Wanstrath (8):
Add support for annotated tags to Grit::Tag
Add packed-refs annotated tag support.
Add Grit::Commit#to_patch

197

CHAPTER 5: Distributed Git

Update version and History.txt
Remove stray `puts`
Make ls_tree ignore nils
Tom Preston-Werner (4):
fix dates in history
dynamic version method
Version bump to 1.0.2
Regenerated gemspec for version 1.0.2

You get a clean summary of all the commits since v1.0.1, grouped by author,
that you can e-mail to your list.

Summary
You should feel fairly comfortable contributing to a project in Git as well as
maintaining your own project or integrating other users’ contributions. Congratulations on being an effective Git developer! In the next chapter, you’ll learn
about how to use the largest and most popular Git hosting service, GitHub.

198

GitHub

6

GitHub is the single largest host for Git repositories, and is the central point of
collaboration for millions of developers and projects. A large percentage of all
Git repositories are hosted on GitHub, and many open-source projects use it for
Git hosting, issue tracking, code review, and other things. So while it’s not a direct part of the Git open source project, there’s a good chance that you’ll want
or need to interact with GitHub at some point while using Git professionally.
This chapter is about using GitHub effectively. We’ll cover signing up for and
managing an account, creating and using Git repositories, common workflows
to contribute to projects and to accept contributions to yours, GitHub’s programmatic interface and lots of little tips to make your life easier in general.
If you are not interested in using GitHub to host your own projects or to collaborate with other projects that are hosted on GitHub, you can safely skip to
Chapter 7.
INTERFACES CHANGE
It’s important to note that like many active websites, the UI elements in
these screenshots are bound to change over time. Hopefully the general
idea of what we’re trying to accomplish here will still be there, but if you
want more up to date versions of these screens, the online versions of
this book may have newer screenshots.

Account Setup and Configuration
The first thing you need to do is set up a free user account. Simply visit https://
github.com, choose a user name that isn’t already taken, provide an email address and a password, and click the big green “Sign up for GitHub” button.

199

CHAPTER 6: GitHub

FIGURE 6-1
The GitHub sign-up
form.

The next thing you’ll see is the pricing page for upgraded plans, but it’s safe
to ignore this for now. GitHub will send you an email to verify the address you
provided. Go ahead and do this, it’s pretty important (as we’ll see later).
GitHub provides all of its functionality with free accounts, with the limitation that all of your projects are fully public (everyone has read access).
GitHub’s paid plans include a set number of private projects, but we
won’t be covering those in this book.

Clicking the Octocat logo at the top-left of the screen will take you to your
dashboard page. You’re now ready to use GitHub.

SSH Access
As of right now, you’re fully able to connect with Git repositories using the

https:// protocol, authenticating with the username and password you just
set up. However, to simply clone public projects, you don’t even need to sign up
- the account we just created comes into play when we fork projects and push
to our forks a bit later.

200

Account Setup and Configuration

If you’d like to use SSH remotes, you’ll need to configure a public key. (If you
don’t already have one, see “Generating Your SSH Public Key”.) Open up your
account settings using the link at the top-right of the window:

FIGURE 6-2
The “Account
settings” link.

Then select the “SSH keys” section along the left-hand side.

FIGURE 6-3
The “SSH keys” link.

From there, click the "Add an SSH key" button, give your key a name,
paste the contents of your ~/.ssh/id_rsa.pub (or whatever you named it)
public-key file into the text area, and click “Add key”.
Be sure to name your SSH key something you can remember. You can
name each of your keys (e.g. “My Laptop” or “Work Account”) so that if
you need to revoke a key later, you can easily tell which one you’re looking for.

201

CHAPTER 6: GitHub

Your Avatar
Next, if you wish, you can replace the avatar that is generated for you with an
image of your choosing. First go to the “Profile” tab (above the SSH Keys tab)
and click “Upload new picture”.

FIGURE 6-4
The “Profile” link.

We’ll choose a copy of the Git logo that is on our hard drive and then we get
a chance to crop it.

202

Account Setup and Configuration

FIGURE 6-5
Crop your avatar

Now anywhere you interact on the site, people will see your avatar next to
your username.
If you happen to have uploaded an avatar to the popular Gravatar service
(often used for Wordpress accounts), that avatar will be used by default and you
don’t need to do this step.

Your Email Addresses
The way that GitHub maps your Git commits to your user is by email address. If
you use multiple email addresses in your commits and you want GitHub to link
them up properly, you need to add all the email addresses you have used to the
Emails section of the admin section.

203

CHAPTER 6: GitHub

FIGURE 6-6
Add email addresses

In Figure 6-6 we can see some of the different states that are possible. The
top address is verified and set as the primary address, meaning that is where
you’ll get any notifications and receipts. The second address is verified and so
can be set as the primary if you wish to switch them. The final address is unverified, meaning that you can’t make it your primary address. If GitHub sees any of
these in commit messages in any repository on the site, it will be linked to your
user now.

Two Factor Authentication
Finally, for extra security, you should definitely set up Two-factor Authentication or “2FA”. Two-factor Authentication is an authentication mechanism that is
becoming more and more popular recently to mitigate the risk of your account
being compromised if your password is stolen somehow. Turning it on will
make GitHub ask you for two different methods of authentication, so that if one
of them is compromised, an attacker will not be able to access your account.
You can find the Two-factor Authentication setup under the Security tab of
your Account settings.

204

Contributing to a Project

FIGURE 6-7
2FA in the Security
Tab

If you click on the “Set up two-factor authentication” button, it will take you
to a configuration page where you can choose to use a phone app to generate
your secondary code (a “time based one-time password”), or you can have GitHub send you a code via SMS each time you need to log in.
After you choose which method you prefer and follow the instructions for
setting up 2FA, your account will then be a little more secure and you will have
to provide a code in addition to your password whenever you log into GitHub.

Contributing to a Project
Now that our account is set up, let’s walk through some details that could be
useful in helping you contribute to an existing project.

Forking Projects
If you want to contribute to an existing project to which you don’t have push
access, you can “fork” the project. What this means is that GitHub will make a
copy of the project that is entirely yours; it lives in your user’s namespace, and
you can push to it.

205

CHAPTER 6: GitHub

Historically, the term “fork” has been somewhat negative in context,
meaning that someone took an open source project in a different direction, sometimes creating a competing project and splitting the contributors. In GitHub, a “fork” is simply the same project in your own namespace, allowing you to make changes to a project publicly as a way to
contribute in a more open manner.

This way, projects don’t have to worry about adding users as collaborators
to give them push access. People can fork a project, push to it, and contribute
their changes back to the original repository by creating what’s called a Pull Request, which we’ll cover next. This opens up a discussion thread with code review, and the owner and the contributor can then communicate about the
change until the owner is happy with it, at which point the owner can merge it
in.
To fork a project, visit the project page and click the “Fork” button at the
top-right of the page.

FIGURE 6-8
The “Fork” button.

After a few seconds, you’ll be taken to your new project page, with your own
writeable copy of the code.

The GitHub Flow
GitHub is designed around a particular collaboration workflow, centered on
Pull Requests. This flow works whether you’re collaborating with a tightly-knit
team in a single shared repository, or a globally-distributed company or network of strangers contributing to a project through dozens of forks. It is centered on the “Ramas Puntuales” workflow covered in Chapter 3.
Here’s how it generally works:
1. Create a topic branch from master.
2. Make some commits to improve the project.
3. Push this branch to your GitHub project.
4. Open a Pull Request on GitHub.
5. Discuss, and optionally continue committing.
6. The project owner merges or closes the Pull Request.

206

Contributing to a Project

This is basically the Integration Manager workflow covered in “IntegrationManager Workflow”, but instead of using email to communicate and review
changes, teams use GitHub’s web based tools.
Let’s walk through an example of proposing a change to an open source
project hosted on GitHub using this flow.
CREATING A PULL REQUEST
Tony is looking for code to run on his Arduino programmable microcontroller
and has found a great program file on GitHub at https://github.com/schacon/
blink.

FIGURE 6-9
The project we want
to contribute to.

The only problem is that the blinking rate is too fast, we think it’s much nicer
to wait 3 seconds instead of 1 in between each state change. So let’s improve
the program and submit it back to the project as a proposed change.
First, we click the Fork button as mentioned earlier to get our own copy of
the project. Our user name here is “tonychacon” so our copy of this project is at
https://github.com/tonychacon/blink and that’s where we can edit it.
We will clone it locally, create a topic branch, make the code change and finally
push that change back up to GitHub.

207

CHAPTER 6: GitHub

$ git clone https://github.com/tonychacon/blink
Cloning into 'blink'...
$ cd blink
$ git checkout -b slow-blink
Switched to a new branch 'slow-blink'
$ sed -i '' 's/1000/3000/' blink.ino
$ git diff --word-diff
diff --git a/blink.ino b/blink.ino
index 15b9911..a6cc5a5 100644
--- a/blink.ino
+++ b/blink.ino
@@ -18,7 +18,7 @@ void setup() {
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH);
// turn the LED on (HIGH is the voltage level)
[-delay(1000);-]{+delay(3000);+}
// wait for a second
digitalWrite(led, LOW);
// turn the LED off by making the voltage LOW
[-delay(1000);-]{+delay(3000);+}
// wait for a second
}
$ git commit -a -m 'three seconds is better'
[slow-blink 5ca509d] three seconds is better
1 file changed, 2 insertions(+), 2 deletions(-)
$ git push origin slow-blink
Username for 'https://github.com': tonychacon
Password for 'https://tonychacon@github.com':
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 340 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/tonychacon/blink
* [new branch]
slow-blink -> slow-blink

Clone our fork of the project locally
Create a descriptive topic branch
Make our change to the code
Check that the change is good
Commit our change to the topic branch

208

Contributing to a Project

Push our new topic branch back up to our GitHub fork
Now if we go back to our fork on GitHub, we can see that GitHub noticed that
we pushed a new topic branch up and present us with a big green button to
check out our changes and open a Pull Request to the original project.
You can alternatively go to the “Branches” page at https://github.com/
//branches to locate your branch and open a new Pull Request from there.

FIGURE 6-10
Pull Request button

If we click that green button, we’ll see a screen that allows us to create a title
and description for the change we would like to request so the project owner
has a good reason to consider it. It is generally a good idea to spend some effort
making this description as useful as possible so the author knows why this is
being suggested and why it would be a valuable change for them to accept.
We also see a list of the commits in our topic branch that are “ahead” of the
master branch (in this case, just the one) and a unified diff of all the changes
that will be made should this branch get merged by the project owner.

209

CHAPTER 6: GitHub

FIGURE 6-11
Pull Request
creation page

When you hit the Create pull request button on this screen, the owner of the
project you forked will get a notification that someone is suggesting a change
and will link to a page that has all of this information on it.
Though Pull Requests are used commonly for public projects like this
when the contributor has a complete change ready to be made, it’s also
often used in internal projects at the beginning of the development cycle.
Since you can keep pushing to the topic branch even after the Pull Request is opened, it’s often opened early and used as a way to iterate on
work as a team within a context, rather than opened at the very end of
the process.

ITERATING ON A PULL REQUEST
At this point, the project owner can look at the suggested change and merge it,
reject it or comment on it. Let’s say that he likes the idea, but would prefer a
slightly longer time for the light to be off than on.

210

Contributing to a Project

Where this conversation may take place over email in the workflows presented in Chapter 5, on GitHub this happens online. The project owner can review
the unified diff and leave a comment by clicking on any of the lines.

FIGURE 6-12
Comment on a
specific line of code
in a Pull Request

Once the maintainer makes this comment, the person who opened the Pull
Request (and indeed, anyone else watching the repository) will get a notification. We’ll go over customizing this later, but if he had email notifications
turned on, Tony would get an email like this:

FIGURE 6-13
Comments sent as
email notifications

Anyone can also leave general comments on the Pull Request. In Figure 6-14
we can see an example of the project owner both commenting on a line of code

211

CHAPTER 6: GitHub

and then leaving a general comment in the discussion section. You can see that
the code comments are brought into the conversation as well.

FIGURE 6-14
Pull Request
discussion page

Now the contributor can see what they need to do in order to get their
change accepted. Luckily this is also a very simple thing to do. Where over email
you may have to re-roll your series and resubmit it to the mailing list, with GitHub you simply commit to the topic branch again and push.
If the contributor does that then the project owner will get notified again
and when they visit the page they will see that it’s been addressed. In fact, since
a line of code changed that had a comment on it, GitHub notices that and collapses the outdated diff.

212

Contributing to a Project

FIGURE 6-15
Pull Request final

An interesting thing to notice is that if you click on the “Files Changed” tab
on this Pull Request, you’ll get the “unified” diff — that is, the total aggregate
difference that would be introduced to your main branch if this topic branch
was merged in. In git diff terms, it basically automatically shows you git
diff master... for the branch this Pull Request is based on. See
“Determining What Is Introduced” for more about this type of diff.
The other thing you’ll notice is that GitHub checks to see if the Pull Request
merges cleanly and provides a button to do the merge for you on the server.
This button only shows up if you have write access to the repository and a trivial merge is possible. If you click it GitHub will perform a “non-fast-forward”
merge, meaning that even if the merge could be a fast-forward, it will still create a merge commit.

213

CHAPTER 6: GitHub

If you would prefer, you can simply pull the branch down and merge it locally. If you merge this branch into the master branch and push it to GitHub, the
Pull Request will automatically be closed.
This is the basic workflow that most GitHub projects use. Topic branches are
created, Pull Requests are opened on them, a discussion ensues, possibly more
work is done on the branch and eventually the request is either closed or
merged.
NOT ONLY FORKS
It’s important to note that you can also open a Pull Request between two
branches in the same repository. If you’re working on a feature with
someone and you both have write access to the project, you can push a
topic branch to the repository and open a Pull Request on it to the master
branch of that same project to initiate the code review and discussion
process. No forking necessary.

Advanced Pull Requests
Now that we’ve covered the basics of contributing to a project on GitHub, let’s
cover a few interesting tips and tricks about Pull Requests so you can be more
effective in using them.
PULL REQUESTS AS PATCHES
It’s important to understand that many projects don’t really think of Pull Requests as queues of perfect patches that should apply cleanly in order, as most
mailing list-based projects think of patch series contributions. Most GitHub
projects think about Pull Request branches as iterative conversations around a
proposed change, culminating in a unified diff that is applied by merging.
This is an important distinction, because generally the change is suggested
before the code is thought to be perfect, which is far more rare with mailing list
based patch series contributions. This enables an earlier conversation with the
maintainers so that arriving at the proper solution is more of a community efffort. When code is proposed with a Pull Request and the maintainers or community suggest a change, the patch series is generally not re-rolled, but instead
the difference is pushed as a new commit to the branch, moving the conversation forward with the context of the previous work intact.
For instance, if you go back and look again at Figure 6-15, you’ll notice that
the contributor did not rebase his commit and send another Pull Request. Instead they added new commits and pushed them to the existing branch. This
way if you go back and look at this Pull Request in the future, you can easily find
all of the context of why decisions were made. Pushing the “Merge” button on

214

Contributing to a Project

the site purposefully creates a merge commit that references the Pull Request
so that it’s easy to go back and research the original conversation if necessary.
KEEPING UP WITH UPSTREAM
If your Pull Request becomes out of date or otherwise doesn’t merge cleanly,
you will want to fix it so the maintainer can easily merge it. GitHub will test this
for you and let you know at the bottom of every Pull Request if the merge is
trivial or not.

FIGURE 6-16
Pull Request does
not merge cleanly

If you see something like Figure 6-16, you’ll want to fix your branch so that it
turns green and the maintainer doesn’t have to do extra work.
You have two main options in order to do this. You can either rebase your
branch on top of whatever the target branch is (normally the master branch of
the repository you forked), or you can merge the target branch into your
branch.
Most developers on GitHub will choose to do the latter, for the same reasons
we just went over in the previous section. What matters is the history and the
final merge, so rebasing isn’t getting you much other than a slightly cleaner history and in return is far more difficult and error prone.
If you want to merge in the target branch to make your Pull Request mergeable, you would add the original repository as a new remote, fetch from it, merge
the main branch of that repository into your topic branch, fix any issues and finally push it back up to the same branch you opened the Pull Request on.
For example, let’s say that in the “tonychacon” example we were using before, the original author made a change that would create a conflict in the Pull
Request. Let’s go through those steps.
$ git remote add upstream https://github.com/schacon/blink
$ git fetch upstream
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
From https://github.com/schacon/blink

215

CHAPTER 6: GitHub

* [new branch]

master

-> upstream/master

$ git merge upstream/master
Auto-merging blink.ino
CONFLICT (content): Merge conflict in blink.ino
Automatic merge failed; fix conflicts and then commit the result.
$ vim blink.ino
$ git add blink.ino
$ git commit
[slow-blink 3c8d735] Merge remote-tracking branch 'upstream/master' \
into slower-blink
$ git push origin slow-blink
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 682 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
To https://github.com/tonychacon/blink
ef4725c..3c8d735 slower-blink -> slow-blink

Add the original repository as a remote named “upstream”
Fetch the newest work from that remote
Merge the main branch into your topic branch
Fix the conflict that occurred
Push back up to the same topic branch
Once you do that, the Pull Request will be automatically updated and rechecked to see if it merges cleanly.

216

Contributing to a Project

FIGURE 6-17
Pull Request now
merges cleanly

One of the great things about Git is that you can do that continuously. If you
have a very long-running project, you can easily merge from the target branch
over and over again and only have to deal with conflicts that have arisen since
the last time that you merged, making the process very manageable.
If you absolutely wish to rebase the branch to clean it up, you can certainly
do so, but it is highly encouraged to not force push over the branch that the Pull
Request is already opened on. If other people have pulled it down and done
more work on it, you run into all of the issues outlined in “Los Peligros de Reorganizar”. Instead, push the rebased branch to a new branch on GitHub and
open a brand new Pull Request referencing the old one, then close the original.
REFERENCES
Your next question may be “How do I reference the old Pull Request?”. It turns
out there are many, many ways to reference other things almost anywhere you
can write in GitHub.
Let’s start with how to cross-reference another Pull Request or an Issue. All
Pull Requests and Issues are assigned numbers and they are unique within the
project. For example, you can’t have Pull Request #3 and Issue #3. If you want
to reference any Pull Request or Issue from any other one, you can simply put
# in any comment or description. You can also be more specific if the Issue or Pull request lives somewhere else; write username# if you’re referring to an Issue or Pull Request in a fork of the repository you’re in, or username/repo# to reference something in another repository.
Let’s look at an example. Say we rebased the branch in the previous example, created a new pull request for it, and now we want to reference the old pull
request from the new one. We also want to reference an issue in the fork of the
repository and an issue in a completely different project. We can fill out the description just like Figure 6-18.

217

CHAPTER 6: GitHub

FIGURE 6-18
Cross references in a
Pull Request.

When we submit this pull request, we’ll see all of that rendered like
Figure 6-19.

FIGURE 6-19
Cross references
rendered in a Pull
Request.

Notice that the full GitHub URL we put in there was shortened to just the information needed.
Now if Tony goes back and closes out the original Pull Request, we can see
that by mentioning it in the new one, GitHub has automatically created a trackback event in the Pull Request timeline. This means that anyone who visits this
Pull Request and sees that it is closed can easily link back to the one that superseded it. The link will look something like Figure 6-20.

218

Contributing to a Project

FIGURE 6-20
Cross references
rendered in a Pull
Request.

In addition to issue numbers, you can also reference a specific commit by
SHA-1. You have to specify a full 40 character SHA-1, but if GitHub sees that in a
comment, it will link directly to the commit. Again, you can reference commits
in forks or other repositories in the same way you did with issues.

Markdown
Linking to other Issues is just the beginning of interesting things you can do
with almost any text box on GitHub. In Issue and Pull Request descriptions,
comments, code comments and more, you can use what is called “GitHub Flavored Markdown”. Markdown is like writing in plain text but which is rendered
richly.
See Figure 6-21 for an example of how comments or text can be written and
then rendered using Markdown.

FIGURE 6-21
An example of
Markdown as
written and as
rendered.

219

CHAPTER 6: GitHub

GITHUB FLAVORED MARKDOWN
The GitHub flavor of Markdown adds more things you can do beyond the basic
Markdown syntax. These can all be really useful when creating useful Pull Request or Issue comments or descriptions.

Task Lists

The first really useful GitHub specific Markdown feature, especially for use in
Pull Requests, is the Task List. A task list is a list of checkboxes of things you
want to get done. Putting them into an Issue or Pull Request normally indicates
things that you want to get done before you consider the item complete.
You can create a task list like this:
- [X] Write the code
- [ ] Write all the tests
- [ ] Document the code

If we include this in the description of our Pull Request or Issue, we’ll see it
rendered like Figure 6-22

FIGURE 6-22
Task lists rendered
in a Markdown
comment.

This is often used in Pull Requests to indicate what all you would like to get
done on the branch before the Pull Request will be ready to merge. The really
cool part is that you can simply click the checkboxes to update the comment —
you don’t have to edit the Markdown directly to check tasks off.
What’s more, GitHub will look for task lists in your Issues and Pull Requests
and show them as metadata on the pages that list them out. For example, if you
have a Pull Request with tasks and you look at the overview page of all Pull Requests, you can see how far done it is. This helps people break down Pull Requests into subtasks and helps other people track the progress of the branch.
You can see an example of this in Figure 6-23.

220

Contributing to a Project

FIGURE 6-23
Task list summary in
the Pull Request list.

These are incredibly useful when you open a Pull Request early and use it to
track your progress through the implementation of the feature.

Code Snippets

You can also add code snippets to comments. This is especially useful if you
want to present something that you could try to do before actually implementing it as a commit on your branch. This is also often used to add example code
of what is not working or what this Pull Request could implement.
To add a snippet of code you have to “fence” it in backticks.
```java
for(int i=0 ; i < 5 ; i++)
{
System.out.println("i is : " + i);
}
```

If you add a language name like we did there with java, GitHub will also try
to syntax highlight the snippet. In the case of the above example, it would end
up rendering like Figure 6-24.

FIGURE 6-24
Rendered fenced
code example.

Quoting

If you’re responding to a small part of a long comment, you can selectively
quote out of the other comment by preceding the lines with the > character. In
fact, this is so common and so useful that there is a keyboard shortcut for it. If

221

CHAPTER 6: GitHub

you highlight text in a comment that you want to directly reply to and hit the r
key, it will quote that text in the comment box for you.
The quotes look something like this:
> Whether 'tis Nobler in the mind to suffer
> The Slings and Arrows of outrageous Fortune,
How big are these slings and in particular, these arrows?

Once rendered, the comment will look like Figure 6-25.

FIGURE 6-25
Rendered quoting
example.

Emoji

Finally, you can also use emoji in your comments. This is actually used quite
extensively in comments you see on many GitHub Issues and Pull Requests.
There is even an emoji helper in GitHub. If you are typing a comment and you
start with a : character, an autocompleter will help you find what you’re looking for.

222

Contributing to a Project

FIGURE 6-26
Emoji autocompleter
in action.

Emojis take the form of :: anywhere in the comment. For instance,
you could write something like this:
I :eyes: that :bug: and I :cold_sweat:.
:trophy: for :microscope: it.
:+1: and :sparkles: on this :ship:, it's :fire::poop:!
:clap::tada::panda_face:

When rendered, it would look something like Figure 6-27.

FIGURE 6-27
Heavy emoji
commenting.

Not that this is incredibly useful, but it does add an element of fun and emotion to a medium that is otherwise hard to convey emotion in.
There are actually quite a number of web services that make use of emoji
characters these days. A great cheat sheet to reference to find emoji that
expresses what you want to say can be found at:
http://www.emoji-cheat-sheet.com

223

CHAPTER 6: GitHub

Images

This isn’t technically GitHub Flavored Markdown, but it is incredibly useful.
In addition to adding Markdown image links to comments, which can be difficult to find and embed URLs for, GitHub allows you to drag and drop images
into text areas to embed them.

FIGURE 6-28
Drag and drop
images to upload
them and autoembed them.

If you look back at Figure 6-18, you can see a small “Parsed as Markdown”
hint above the text area. Clicking on that will give you a full cheat sheet of everything you can do with Markdown on GitHub.

Maintaining a Project
Now that we’re comfortable contributing to a project, let’s look at the other
side: creating, maintaining and administering your own project.

Creating a New Repository
Let’s create a new repository to share our project code with. Start by clicking
the “New repository” button on the right-hand side of the dashboard, or from
the + button in the top toolbar next to your username as seen in Figure 6-30.

224

Maintaining a Project

FIGURE 6-29
The “Your
repositories” area.

FIGURE 6-30
The “New
repository”
dropdown.

This takes you to the “new repository” form:

225

CHAPTER 6: GitHub

FIGURE 6-31
The “new
repository” form.

All you really have to do here is provide a project name; the rest of the fields
are completely optional. For now, just click the “Create Repository” button, and
boom – you have a new repository on GitHub, named /
.
Since you have no code there yet, GitHub will show you instructions for how
create a brand-new Git repository, or connect an existing Git project. We won’t
belabor this here; if you need a refresher, check out Chapter 2.
Now that your project is hosted on GitHub, you can give the URL to anyone
you want to share your project with. Every project on GitHub is accessible over
HTTP as https://github.com//, and over SSH as
git@github.com:/. Git can fetch from and push to
both of these URLs, but they are access-controlled based on the credentials of
the user connecting to them.
It is often preferable to share the HTTP based URL for a public project,
since the user does not have to have a GitHub account to access it for
cloning. Users will have to have an account and an uploaded SSH key to
access your project if you give them the SSH URL. The HTTP one is also
exactly the same URL they would paste into a browser to view the project
there.

Adding Collaborators
If you’re working with other people who you want to give commit access to, you
need to add them as “collaborators”. If Ben, Jeff, and Louise all sign up for ac-

226

Maintaining a Project

counts on GitHub, and you want to give them push access to your repository,
you can add them to your project. Doing so will give them “push” access, which
means they have both read and write access to the project and Git repository.
Click the “Settings” link at the bottom of the right-hand sidebar.

FIGURE 6-32
The repository
settings link.

Then select “Collaborators” from the menu on the left-hand side. Then, just
type a username into the box, and click “Add collaborator.” You can repeat this
as many times as you like to grant access to everyone you like. If you need to
revoke access, just click the “X” on the right-hand side of their row.

FIGURE 6-33
Repository
collaborators.

227

CHAPTER 6: GitHub

Managing Pull Requests
Now that you have a project with some code in it and maybe even a few collaborators who also have push access, let’s go over what to do when you get a Pull
Request yourself.
Pull Requests can either come from a branch in a fork of your repository or
they can come from another branch in the same repository. The only difference
is that the ones in a fork are often from people where you can’t push to their
branch and they can’t push to yours, whereas with internal Pull Requests generally both parties can access the branch.
For these examples, let’s assume you are “tonychacon” and you’ve created a
new Arduino code project named “fade”.
EMAIL NOTIFICATIONS
Someone comes along and makes a change to your code and sends you a Pull
Request. You should get an email notifying you about the new Pull Request and
it should look something like Figure 6-34.

FIGURE 6-34
Email notification of
a new Pull Request.

There are a few things to notice about this email. It will give you a small diffstat — a list of files that have changed in the Pull Request and by how much. It

228

Maintaining a Project

gives you a link to the Pull Request on GitHub. It also gives you a few URLs that
you can use from the command line.
If you notice the line that says git pull  patch-1, this is a simple
way to merge in a remote branch without having to add a remote. We went over
this quickly in “Checking Out Remote Branches”. If you wish, you can create
and switch to a topic branch and then run this command to merge in the Pull
Request changes.
The other interesting URLs are the .diff and .patch URLs, which as you
may guess, provide unified diff and patch versions of the Pull Request. You
could technically merge in the Pull Request work with something like this:
$ curl http://github.com/tonychacon/fade/pull/1.patch | git am

COLLABORATING ON THE PULL REQUEST
As we covered in “The GitHub Flow”, you can now have a conversation with
the person who opened the Pull Request. You can comment on specific lines of
code, comment on whole commits or comment on the entire Pull Request itself,
using GitHub Flavored Markdown everywhere.
Every time someone else comments on the Pull Request you will continue to
get email notifications so you know there is activity happening. They will each
have a link to the Pull Request where the activity is happening and you can also
directly respond to the email to comment on the Pull Request thread.

FIGURE 6-35
Responses to emails
are included in the
thread.

Once the code is in a place you like and want to merge it in, you can either
pull the code down and merge it locally, either with the git pull 
 syntax we saw earlier, or by adding the fork as a remote and fetching
and merging.
If the merge is trivial, you can also just hit the “Merge” button on the GitHub
site. This will do a “non-fast-forward” merge, creating a merge commit even if a

229

CHAPTER 6: GitHub

fast-forward merge was possible. This means that no matter what, every time
you hit the merge button, a merge commit is created. As you can see in
Figure 6-36, GitHub gives you all of this information if you click the hint link.

FIGURE 6-36
Merge button and
instructions for
merging a Pull
Request manually.

If you decide you don’t want to merge it, you can also just close the Pull Request and the person who opened it will be notified.
PULL REQUEST REFS
If you’re dealing with a lot of Pull Requests and don’t want to add a bunch of
remotes or do one time pulls every time, there is a neat trick that GitHub allows
you to do. This is a bit of an advanced trick and we’ll go over the details of this a
bit more in “The Refspec”, but it can be pretty useful.
GitHub actually advertises the Pull Request branches for a repository as sort
of pseudo-branches on the server. By default you don’t get them when you
clone, but they are there in an obscured way and you can access them pretty
easily.
To demonstrate this, we’re going to use a low-level command (often referred
to as a “plumbing” command, which we’ll read about more in “Plumbing and
Porcelain”) called ls-remote. This command is generally not used in day-today Git operations but it’s useful to show us what references are present on the
server.
If we run this command against the “blink” repository we were using earlier,
we will get a list of all the branches and tags and other references in the repository.

230

Maintaining a Project

$ git ls-remote https://github.com/schacon/blink
10d539600d86723087810ec636870a504f4fee4d
HEAD
10d539600d86723087810ec636870a504f4fee4d
refs/heads/master
6a83107c62950be9453aac297bb0193fd743cd6e
refs/pull/1/head
afe83c2d1a70674c9505cc1d8b7d380d5e076ed3
refs/pull/1/merge
3c8d735ee16296c242be7a9742ebfbc2665adec1
refs/pull/2/head
15c9f4f80973a2758462ab2066b6ad9fe8dcf03d
refs/pull/2/merge
a5a7751a33b7e86c5e9bb07b26001bb17d775d1a
refs/pull/4/head
31a45fc257e8433c8d8804e3e848cf61c9d3166c
refs/pull/4/merge

Of course, if you’re in your repository and you run git ls-remote origin
or whatever remote you want to check, it will show you something similar to
this.
If the repository is on GitHub and you have any Pull Requests that have been
opened, you’ll get these references that are prefixed with refs/pull/. These
are basically branches, but since they’re not under refs/heads/ you don’t get
them normally when you clone or fetch from the server — the process of fetching ignores them normally.
There are two references per Pull Request - the one that ends in /head
points to exactly the same commit as the last commit in the Pull Request
branch. So if someone opens a Pull Request in our repository and their branch
is named bug-fix and it points to commit a5a775, then in our repository we
will not have a bug-fix branch (since that’s in their fork), but we will have
pull//head that points to a5a775. This means that we can pretty easily
pull down every Pull Request branch in one go without having to add a bunch
of remotes.
Now, you could do something like fetching the reference directly.
$ git fetch origin refs/pull/958/head
From https://github.com/libgit2/libgit2
* branch
refs/pull/958/head -> FETCH_HEAD

This tells Git, “Connect to the origin remote, and download the ref named
refs/pull/958/head.” Git happily obeys, and downloads everything you
need to construct that ref, and puts a pointer to the commit you want under .git/FETCH_HEAD. You can follow that up with git merge FETCH_HEAD
into a branch you want to test it in, but that merge commit message looks a bit
weird. Also, if you’re reviewing a lot of pull requests, this gets tedious.
There’s also a way to fetch all of the pull requests, and keep them up to date
whenever you connect to the remote. Open up .git/config in your favorite
editor, and look for the origin remote. It should look a bit like this:

231

CHAPTER 6: GitHub

[remote "origin"]
url = https://github.com/libgit2/libgit2
fetch = +refs/heads/*:refs/remotes/origin/*

That line that begins with fetch = is a “refspec.” It’s a way of mapping
names on the remote with names in your local .git directory. This particular
one tells Git, “the things on the remote that are under refs/heads should go in
my local repository under refs/remotes/origin.” You can modify this section to add another refspec:
[remote "origin"]
url = https://github.com/libgit2/libgit2.git
fetch = +refs/heads/*:refs/remotes/origin/*
fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

That last line tells Git, “All the refs that look like refs/pull/123/head
should be stored locally like refs/remotes/origin/pr/123.” Now, if you
save that file, and do a git fetch:
$ git fetch
# …
* [new ref]
* [new ref]
* [new ref]
# …

refs/pull/1/head -> origin/pr/1
refs/pull/2/head -> origin/pr/2
refs/pull/4/head -> origin/pr/4

Now all of the remote pull requests are represented locally with refs that act
much like tracking branches; they’re read-only, and they update when you do a
fetch. This makes it super easy to try the code from a pull request locally:
$ git checkout pr/2
Checking out files: 100% (3769/3769), done.
Branch pr/2 set up to track remote branch pr/2 from origin.
Switched to a new branch 'pr/2'

The eagle-eyed among you would note the head on the end of the remote
portion of the refspec. There’s also a refs/pull/#/merge ref on the GitHub
side, which represents the commit that would result if you push the “merge”
button on the site. This can allow you to test the merge before even hitting the
button.

232

Maintaining a Project

PULL REQUESTS ON PULL REQUESTS
Not only can you open Pull Requests that target the main or master branch,
you can actually open a Pull Request targeting any branch in the network. In
fact, you can even target another Pull Request.
If you see a Pull Request that is moving in the right direction and you have
an idea for a change that depends on it or you’re not sure is a good idea, or you
just don’t have push access to the target branch, you can open a Pull Request
directly to it.
When you go to open a Pull Request, there is a box at the top of the page
that specifies which branch you’re requesting to pull to and which you’re requesting to pull from. If you hit the “Edit” button at the right of that box you can
change not only the branches but also which fork.

FIGURE 6-37
Manually change
the Pull Request
target fork and
branch.

Here you can fairly easily specify to merge your new branch into another Pull
Request or another fork of the project.

Mentions and Notifications
GitHub also has a pretty nice notifications system built in that can come in handy when you have questions or need feedback from specific individuals or
teams.
In any comment you can start typing a @ character and it will begin to autocomplete with the names and usernames of people who are collaborators or
contributors in the project.

233

CHAPTER 6: GitHub

FIGURE 6-38
Start typing @ to
mention someone.

You can also mention a user who is not in that dropdown, but often the autocompleter can make it faster.
Once you post a comment with a user mention, that user will be notified.
This means that this can be a really effective way of pulling people into conversations rather than making them poll. Very often in Pull Requests on GitHub
people will pull in other people on their teams or in their company to review an
Issue or Pull Request.
If someone gets mentioned on a Pull Request or Issue, they will be “subscribed” to it and will continue getting notifications any time some activity occurs
on it. You will also be subscribed to something if you opened it, if you’re watching the repository or if you comment on something. If you no longer wish to receive notifications, there is an “Unsubscribe” button on the page you can click
to stop receiving updates on it.

234

Maintaining a Project

FIGURE 6-39
Unsubscribe from an
Issue or Pull
Request.

THE NOTIFICATIONS PAGE
When we mention “notifications” here with respect to GitHub, we mean a specific way that GitHub tries to get in touch with you when events happen and
there are a few different ways you can configure them. If you go to the “Notification center” tab from the settings page, you can see some of the options you
have.

FIGURE 6-40
Notification center
options.

235

CHAPTER 6: GitHub

The two choices are to get notifications over “Email” and over “Web” and
you can choose either, neither or both for when you actively participate in
things and for activity on repositories you are watching.

Web Notifications

Web notifications only exist on GitHub and you can only check them on GitHub. If you have this option selected in your preferences and a notification is
triggered for you, you will see a small blue dot over your notifications icon at
the top of your screen as seen in Figure 6-41.

FIGURE 6-41
Notification center.

If you click on that, you will see a list of all the items you have been notified
about, grouped by project. You can filter to the notifications of a specific project
by clicking on it’s name in the left hand sidebar. You can also acknowledge the
notification by clicking the checkmark icon next to any notification, or acknowledge all of the notifications in a project by clicking the checkmark at the top of
the group. There is also a mute button next to each checkmark that you can
click to not receive any further notifications on that item.
All of these tools are very useful for handling large numbers of notifications.
Many GitHub power users will simply turn off email notifications entirely and
manage all of their notifications through this screen.

Email Notifications

Email notifications are the other way you can handle notifications through
GitHub. If you have this turned on you will get emails for each notification. We
saw examples of this in Figure 6-13 and Figure 6-34. The emails will also be
threaded properly, which is nice if you’re using a threading email client.
There is also a fair amount of metadata embedded in the headers of the
emails that GitHub sends you, which can be really helpful for setting up custom
filters and rules.

236

Maintaining a Project

For instance, if we look at the actual email headers sent to Tony in the email
shown in Figure 6-34, we will see the following among the information sent:
To: tonychacon/fade 
Message-ID: 
Subject: [fade] Wait longer to see the dimming effect better (#1)
X-GitHub-Recipient: tonychacon
List-ID: tonychacon/fade 
List-Archive: https://github.com/tonychacon/fade
List-Post: 
List-Unsubscribe: ,...
X-GitHub-Recipient-Address: tchacon@example.com

There are a couple of interesting things here. If you want to highlight or reroute emails to this particular project or even Pull Request, the information in
Message-ID gives you all the data in /// format. If this were an issue, for example, the  field would have been “issues” rather than “pull”.
The List-Post and List-Unsubscribe fields mean that if you have a mail
client that understands those, you can easily post to the list or “Unsubscribe”
from the thread. That would be essentially the same as clicking the “mute” button on the web version of the notification or “Unsubscribe” on the Issue or Pull
Request page itself.
It’s also worth noting that if you have both email and web notifications enabled and you read the email version of the notification, the web version will be
marked as read as well if you have images allowed in your mail client.

Special Files
There are a couple of special files that GitHub will notice if they are present in
your repository.

README
The first is the README file, which can be of nearly any format that GitHub recognizes as prose. For example, it could be README, README.md, README.asciidoc, etc. If GitHub sees a README file in your source, it will render
it on the landing page of the project.
Many teams use this file to hold all the relevant project information for
someone who might be new to the repository or project. This generally includes
things like:
• What the project is for

237

CHAPTER 6: GitHub

• How to configure and install it
• An example of how to use it or get it running
• The license that the project is offered under
• How to contribute to it
Since GitHub will render this file, you can embed images or links in it for added ease of understanding.

CONTRIBUTING
The other special file that GitHub recognizes is the CONTRIBUTING file. If you
have a file named CONTRIBUTING with any file extension, GitHub will show
Figure 6-42 when anyone starts opening a Pull Request.

FIGURE 6-42
Opening a Pull
Request when a
CONTRIBUTING file
exists.

The idea here is that you can specify specific things you want or don’t want
in a Pull Request sent to your project. This way people may actually read the
guidelines before opening the Pull Request.

Project Administration
Generally there are not a lot of administrative things you can do with a single
project, but there are a couple of items that might be of interest.

238

Maintaining a Project

CHANGING THE DEFAULT BRANCH
If you are using a branch other than “master” as your default branch that you
want people to open Pull Requests on or see by default, you can change that in
your repository’s settings page under the “Options” tab.

FIGURE 6-43
Change the default
branch for a project.

Simply change the default branch in the dropdown and that will be the default for all major operations from then on, including which branch is checked
out by default when someone clones the repository.
TRANSFERRING A PROJECT
If you would like to transfer a project to another user or an organization in GitHub, there is a “Transfer ownership” option at the bottom of the same “Options” tab of your repository settings page that allows you to do this.

FIGURE 6-44
Transfer a project to
anther GitHub user
or Organization.

This is helpful if you are abandoning a project and someone wants to take it
over, or if your project is getting bigger and want to move it into an organization.

239

CHAPTER 6: GitHub

Not only does this move the repository along with all it’s watchers and stars
to another place, it also sets up a redirect from your URL to the new place. It
will also redirect clones and fetches from Git, not just web requests.

Managing an organization
In addition to single-user accounts, GitHub has what are called Organizations.
Like personal accounts, Organizational accounts have a namespace where all
their projects exist, but many other things are different. These accounts represent a group of people with shared ownership of projects, and there are many
tools to manage subgroups of those people. Normally these accounts are used
for Open Source groups (such as “perl” or “rails”) or companies (such as “google” or “twitter”).

Organization Basics
An organization is pretty easy to create; just click on the “+” icon at the topright of any GitHub page, and select “New organization” from the menu.

FIGURE 6-45
The “New
organization” menu
item.

First you’ll need to name your organization and provide an email address for
a main point of contact for the group. Then you can invite other users to be coowners of the account if you want to.

240

Managing an organization

Follow these steps and you’ll soon be the owner of a brand-new organization. Like personal accounts, organizations are free if everything you plan to
store there will be open source.
As an owner in an organization, when you fork a repository, you’ll have the
choice of forking it to your organization’s namespace. When you create new repositories you can create them either under your personal account or under
any of the organizations that you are an owner in. You also automatically
“watch” any new repository created under these organizations.
Just like in “Your Avatar”, you can upload an avatar for your organization to
personalize it a bit. Also just like personal accounts, you have a landing page for
the organization that lists all of your repositories and can be viewed by other
people.
Now let’s cover some of the things that are a bit different with an organizational account.

Teams
Organizations are associated with individual people by way of teams, which are
simply a grouping of individual user accounts and repositories within the organization and what kind of access those people have in those repositories.
For example, say your company has three repositories: frontend, backend,
and deployscripts. You’d want your HTML/CSS/Javascript developers to
have access to frontend and maybe backend, and your Operations people to
have access to backend and deployscripts. Teams make this easy, without
having to manage the collaborators for every individual repository.
The Organization page shows you a simple dashboard of all the repositories,
users and teams that are under this organization.

241

CHAPTER 6: GitHub

FIGURE 6-46
The Organization
page.

To manage your Teams, you can click on the Teams sidebar on the right
hand side of the page in Figure 6-46. This will bring you to a page you can use
to add members to the team, add repositories to the team or manage the settings and access control levels for the team. Each team can have read only,
read/write or administrative access to the repositories. You can change that level by clicking the “Settings” button in Figure 6-47.

FIGURE 6-47
The Team page.

When you invite someone to a team, they will get an email letting them
know they’ve been invited.

242

Managing an organization

Additionally, team @mentions (such as @acmecorp/frontend) work much
the same as they do with individual users, except that all members of the team
are then subscribed to the thread. This is useful if you want the attention from
someone on a team, but you don’t know exactly who to ask.
A user can belong to any number of teams, so don’t limit yourself to only
access-control teams. Special-interest teams like ux, css, or refactoring are
useful for certain kinds of questions, and others like legal and colorblind for
an entirely different kind.

Audit Log
Organizations also give owners access to all the information about what went
on under the organization. You can go to the Audit Log tab and see what events
have happened at an organization level, who did them and where in the world
they were done.

243

CHAPTER 6: GitHub

FIGURE 6-48
The Audit log.

You can also filter down to specific types of events, specific places or specific
people.

Scripting GitHub
So now we’ve covered all of the major features and workflows of GitHub, but
any large group or project will have customizations they may want to make or
external services they may want to integrate.
Luckily for us, GitHub is really quite hackable in many ways. In this section
we’ll cover how to use the GitHub hooks system and it’s API to make GitHub
work how we want it to.

244

Scripting GitHub

Hooks
The Hooks and Services section of GitHub repository administration is the easiest way to have GitHub interact with external systems.
SERVICES
First we’ll take a look at Services. Both the Hooks and Services integrations can
be found in the Settings section of your repository, where we previously looked
at adding Collaborators and changing the default branch of your project. Under
the “Webhooks and Services” tab you will see something like Figure 6-49.

FIGURE 6-49
Services and Hooks
configuration
section.

There are dozens of services you can choose from, most of them integrations
into other commercial and open source systems. Most of them are for Continuous Integration services, bug and issue trackers, chat room systems and documentation systems. We’ll walk through setting up a very simple one, the Email
hook. If you choose “email” from the “Add Service” dropdown, you’ll get a configuration screen like Figure 6-50.

245

CHAPTER 6: GitHub

FIGURE 6-50
Email service
configuration.

In this case, if we hit the “Add service” button, the email address we specified will get an email every time someone pushes to the repository. Services
can listen for lots of different types of events, but most only listen for push
events and then do something with that data.
If there is a system you are using that you would like to integrate with GitHub, you should check here to see if there is an existing service integration
available. For example, if you’re using Jenkins to run tests on your codebase,
you can enable the Jenkins builtin service integration to kick off a test run every
time someone pushes to your repository.
HOOKS
If you need something more specific or you want to integrate with a service or
site that is not included in this list, you can instead use the more generic hooks
system. GitHub repository hooks are pretty simple. You specify a URL and GitHub will post an HTTP payload to that URL on any event you want.
Generally the way this works is you can setup a small web service to listen
for a GitHub hook payload and then do something with the data when it is received.
To enable a hook, you click the “Add webhook” button in Figure 6-49. This
will bring you to a page that looks like Figure 6-51.

246

Scripting GitHub

FIGURE 6-51
Web hook
configuration.

The configuration for a web hook is pretty simple. In most cases you simply
enter a URL and a secret key and hit “Add webhook”. There are a few options for
which events you want GitHub to send you a payload for — the default is to only
get a payload for the push event, when someone pushes new code to any
branch of your repository.
Let’s see a small example of a web service you may set up to handle a web
hook. We’ll use the Ruby web framework Sinatra since it’s fairly concise and you
should be able to easily see what we’re doing.
Let’s say we want to get an email if a specific person pushes to a specific
branch of our project modifying a specific file. We could fairly easily do that
with code like this:
require 'sinatra'
require 'json'
require 'mail'
post '/payload' do
push = JSON.parse(request.body.read) # parse the JSON
# gather the data we're looking for
pusher = push["pusher"]["name"]
branch = push["ref"]
# get a list of all the files touched
files = push["commits"].map do |commit|

247

CHAPTER 6: GitHub

commit['added'] + commit['modified'] + commit['removed']
end
files = files.flatten.uniq
# check for our criteria
if pusher == 'schacon' &&
branch == 'ref/heads/special-branch' &&
files.include?('special-file.txt')
Mail.deliver do
from
'tchacon@example.com'
to
'tchacon@example.com'
subject 'Scott Changed the File'
body
"ALARM"
end
end
end

Here we’re taking the JSON payload that GitHub delivers us and looking up
who pushed it, what branch they pushed to and what files were touched in all
the commits that were pushed. Then we check that against our criteria and
send an email if it matches.
In order to develop and test something like this, you have a nice developer
console in the same screen where you set the hook up. You can see the last few
deliveries that GitHub has tried to make for that webhook. For each hook you
can dig down into when it was delivered, if it was successful and the body and
headers for both the request and the response. This makes it incredibly easy to
test and debug your hooks.

248

Scripting GitHub

FIGURE 6-52
Web hook debugging
information.

The other great feature of this is that you can redeliver any of the payloads
to test your service easily.
For more information on how to write webhooks and all the different event
types you can listen for, go to the GitHub Developer documentation at: https://
developer.github.com/webhooks/

The GitHub API
Services and hooks give you a way to receive push notifications about events
that happen on your repositories, but what if you need more information about
these events? What if you need to automate something like adding collaborators or labeling issues?

249

CHAPTER 6: GitHub

This is where the GitHub API comes in handy. GitHub has tons of API endpoints for doing nearly anything you can do on the website in an automated
fashion. In this section we’ll learn how to authenticate and connect to the API,
how to comment on an issue and how to change the status of a Pull Request
through the API.

Basic Usage
The most basic thing you can do is a simple GET request on an endpoint that
doesn’t require authentication. This could be a user or read-only information
on an open source project. For example, if we want to know more about a user
named “schacon”, we can run something like this:
$ curl https://api.github.com/users/schacon
{
"login": "schacon",
"id": 70,
"avatar_url": "https://avatars.githubusercontent.com/u/70",
# …
"name": "Scott Chacon",
"company": "GitHub",
"following": 19,
"created_at": "2008-01-27T17:19:28Z",
"updated_at": "2014-06-10T02:37:23Z"
}

There are tons of endpoints like this to get information about organizations,
projects, issues, commits — just about anything you can publicly see on GitHub.
You can even use the API to render arbitrary Markdown or find a .gitignore
template.
$ curl https://api.github.com/gitignore/templates/Java
{
"name": "Java",
"source": "*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot
hs_err_pid*

250

Scripting GitHub

"
}

Commenting on an Issue
However, if you want to do an action on the website such as comment on an
Issue or Pull Request or if you want to view or interact with private content,
you’ll need to authenticate.
There are several ways to authenticate. You can use basic authentication
with just your username and password, but generally it’s a better idea to use a
personal access token. You can generate this from the “Applications” tab of
your settings page.

FIGURE 6-53
Generate your access
token from the
“Applications” tab
of your settings
page.

It will ask you which scopes you want for this token and a description. Make
sure to use a good description so you feel comfortable removing the token
when your script or application is no longer used.
GitHub will only show you the token once, so be sure to copy it. You can now
use this to authenticate in your script instead of using a username and password. This is nice because you can limit the scope of what you want to do and
the token is revocable.
This also has the added advantage of increasing your rate limit. Without authenticating, you will be limited to 60 requests per hour. If you authenticate you
can make up to 5,000 requests per hour.
So let’s use it to make a comment on one of our issues. Let’s say we want to
leave a comment on a specific issue, Issue #6. To do so we have to do an HTTP

251

CHAPTER 6: GitHub

POST request to repos///issues//comments with the
token we just generated as an Authorization header.
$ curl -H "Content-Type: application/json" \
-H "Authorization: token TOKEN" \
--data '{"body":"A new comment, :+1:"}' \
https://api.github.com/repos/schacon/blink/issues/6/comments
{
"id": 58322100,
"html_url": "https://github.com/schacon/blink/issues/6#issuecomment-58322100",
...
"user": {
"login": "tonychacon",
"id": 7874698,
"avatar_url": "https://avatars.githubusercontent.com/u/7874698?v=2",
"type": "User",
},
"created_at": "2014-10-08T07:48:19Z",
"updated_at": "2014-10-08T07:48:19Z",
"body": "A new comment, :+1:"
}

Now if you go to that issue, you can see the comment that we just successfully posted as in Figure 6-54.

FIGURE 6-54
A comment posted
from the GitHub API.

You can use the API to do just about anything you can do on the website —
creating and setting milestones, assigning people to Issues and Pull Requests,
creating and changing labels, accessing commit data, creating new commits
and branches, opening, closing or merging Pull Requests, creating and editing
teams, commenting on lines of code in a Pull Request, searching the site and on
and on.

Changing the Status of a Pull Request
One final example we’ll look at since it’s really useful if you’re working with Pull
Requests. Each commit can have one or more statuses associated with it and
there is an API to add and query that status.

252

Scripting GitHub

Most of the Continuous Integration and testing services make use of this API
to react to pushes by testing the code that was pushed, and then report back if
that commit has passed all the tests. You could also use this to check if the
commit message is properly formatted, if the submitter followed all your contribution guidelines, if the commit was validly signed — any number of things.
Let’s say you set up a webhook on your repository that hits a small web service that checks for a Signed-off-by string in the commit message.
require 'httparty'
require 'sinatra'
require 'json'
post '/payload' do
push = JSON.parse(request.body.read) # parse the JSON
repo_name = push['repository']['full_name']
# look through each commit message
push["commits"].each do |commit|
# look for a Signed-off-by string
if /Signed-off-by/.match commit['message']
state = 'success'
description = 'Successfully signed off!'
else
state = 'failure'
description = 'No signoff found.'
end
# post status to GitHub
sha = commit["id"]
status_url = "https://api.github.com/repos/#{repo_name}/statuses/#{sha}"
status = {
"state"
=> state,
"description" => description,
"target_url" => "http://example.com/how-to-signoff",
"context"
=> "validate/signoff"
}
HTTParty.post(status_url,
:body => status.to_json,
:headers => {
'Content-Type' => 'application/json',
'User-Agent'
=> 'tonychacon/signoff',
'Authorization' => "token #{ENV['TOKEN']}" }
)
end
end

253

CHAPTER 6: GitHub

Hopefully this is fairly simple to follow. In this web hook handler we look
through each commit that was just pushed, we look for the string Signed-off-by
in the commit message and finally we POST via HTTP to the /repos//
/statuses/ API endpoint with the status.
In this case you can send a state (success, failure, error), a description of
what happened, a target URL the user can go to for more information and a
“context” in case there are multiple statuses for a single commit. For example, a
testing service may provide a status and a validation service like this may also
provide a status — the “context” field is how they’re differentiated.
If someone opens a new Pull Request on GitHub and this hook is set up, you
may see something like Figure 6-55.

FIGURE 6-55
Commit status via
the API.

You can now see a little green check mark next to the commit that has a
“Signed-off-by” string in the message and a red cross through the one where
the author forgot to sign off. You can also see that the Pull Request takes the
status of the last commit on the branch and warns you if it is a failure. This is
really useful if you’re using this API for test results so you don’t accidentally
merge something where the last commit is failing tests.

Octokit
Though we’ve been doing nearly everything through curl and simple HTTP requests in these examples, several open-source libraries exist that make this API
available in a more idiomatic way. At the time of this writing, the supported languages include Go, Objective-C, Ruby, and .NET. Check out http://github.com/

254

Summary

octokit for more information on these, as they handle much of the HTTP for
you.
Hopefully these tools can help you customize and modify GitHub to work
better for your specific workflows. For complete documentation on the entire
API as well as guides for common tasks, check out https://developer.github.com.

Summary
Now you’re a GitHub user. You know how to create an account, manage an organization, create and push to repositories, contribute to other people’s
projects and accept contributions from others. In the next chapter, you’ll learn
more powerful tools and tips for dealing with complex situations, which will
truly make you a Git master.

255

Git Tools

7

By now, you’ve learned most of the day-to-day commands and workflows that
you need to manage or maintain a Git repository for your source code control.
You’ve accomplished the basic tasks of tracking and committing files, and
you’ve harnessed the power of the staging area and lightweight topic branching
and merging.
Now you’ll explore a number of very powerful things that Git can do that you
may not necessarily use on a day-to-day basis but that you may need at some
point.

Revision Selection
Git allows you to specify specific commits or a range of commits in several
ways. They aren’t necessarily obvious but are helpful to know.

Single Revisions
You can obviously refer to a commit by the SHA-1 hash that it’s given, but there
are more human-friendly ways to refer to commits as well. This section outlines
the various ways you can refer to a single commit.

Short SHA-1
Git is smart enough to figure out what commit you meant to type if you provide
the first few characters, as long as your partial SHA-1 is at least four characters
long and unambiguous – that is, only one object in the current repository begins with that partial SHA-1.
For example, to see a specific commit, suppose you run a git log command and identify the commit where you added certain functionality:

257

CHAPTER 7: Git Tools

$ git log
commit 734713bc047d87bf7eac9674765ae793478c50d3
Author: Scott Chacon 
Date:
Fri Jan 2 18:32:33 2009 -0800
fixed refs handling, added gc auto, updated tests
commit d921970aadf03b3cf0e71becdaab3147ba71cdef
Merge: 1c002dd... 35cfb2b...
Author: Scott Chacon 
Date:
Thu Dec 11 15:08:43 2008 -0800
Merge commit 'phedders/rdocs'
commit 1c002dd4b536e7479fe34593e72e6c6c1819e53b
Author: Scott Chacon 
Date:
Thu Dec 11 14:58:32 2008 -0800
added some blame and merge stuff

In this case, choose 1c002dd.... If you git show that commit, the following commands are equivalent (assuming the shorter versions are unambiguous):
$ git show 1c002dd4b536e7479fe34593e72e6c6c1819e53b
$ git show 1c002dd4b536e7479f
$ git show 1c002d

Git can figure out a short, unique abbreviation for your SHA-1 values. If you
pass --abbrev-commit to the git log command, the output will use shorter
values but keep them unique; it defaults to using seven characters but makes
them longer if necessary to keep the SHA-1 unambiguous:
$ git log --abbrev-commit --pretty=oneline
ca82a6d changed the version number
085bb3b removed unnecessary test code
a11bef0 first commit

Generally, eight to ten characters are more than enough to be unique within
a project.
As an example, the Linux kernel, which is a pretty large project with over
450k commits and 3.6 million objects, has no two objects whose SHA-1s overlap more than the first 11 characters.

258

Revision Selection

A SHORT NOTE ABOUT SHA-1
A lot of people become concerned at some point that they will, by random happenstance, have two objects in their repository that hash to the
same SHA-1 value. What then?
If you do happen to commit an object that hashes to the same SHA-1 value as a previous object in your repository, Git will see the previous object
already in your Git database and assume it was already written. If you try
to check out that object again at some point, you’ll always get the data of
the first object.
However, you should be aware of how ridiculously unlikely this scenario
is. The SHA-1 digest is 20 bytes or 160 bits. The number of randomly
hashed objects needed to ensure a 50% probability of a single collision is
about 280 (the formula for determining collision probability is p =

(n(n-1)/2) * (1/2^160)). 280 is 1.2 x 1024 or 1 million billion billion.
That’s 1,200 times the number of grains of sand on the earth.
Here’s an example to give you an idea of what it would take to get a
SHA-1 collision. If all 6.5 billion humans on Earth were programming,
and every second, each one was producing code that was the equivalent
of the entire Linux kernel history (3.6 million Git objects) and pushing it
into one enormous Git repository, it would take roughly 2 years until that
repository contained enough objects to have a 50% probability of a single
SHA-1 object collision. A higher probability exists that every member of
your programming team will be attacked and killed by wolves in unrelated incidents on the same night.

Branch References
The most straightforward way to specify a commit requires that it has a branch
reference pointed at it. Then, you can use a branch name in any Git command
that expects a commit object or SHA-1 value. For instance, if you want to show
the last commit object on a branch, the following commands are equivalent,
assuming that the topic1 branch points to ca82a6d:
$ git show ca82a6dff817ec66f44342007202690a93763949
$ git show topic1

If you want to see which specific SHA-1 a branch points to, or if you want to
see what any of these examples boils down to in terms of SHA-1s, you can use a
Git plumbing tool called rev-parse. You can see Chapter 10 for more information about plumbing tools; basically, rev-parse exists for lower-level operations and isn’t designed to be used in day-to-day operations. However, it can be

259

CHAPTER 7: Git Tools

helpful sometimes when you need to see what’s really going on. Here you can
run rev-parse on your branch.
$ git rev-parse topic1
ca82a6dff817ec66f44342007202690a93763949

RefLog Shortnames
One of the things Git does in the background while you’re working away is keep
a “reflog” – a log of where your HEAD and branch references have been for the
last few months.
You can see your reflog by using git reflog:
$ git reflog
734713b HEAD@{0}:
d921970 HEAD@{1}:
1c002dd HEAD@{2}:
1c36188 HEAD@{3}:
95df984 HEAD@{4}:
1c36188 HEAD@{5}:
7e05da5 HEAD@{6}:

commit: fixed refs handling, added gc auto, updated
merge phedders/rdocs: Merge made by recursive.
commit: added some blame and merge stuff
rebase -i (squash): updating HEAD
commit: # This is a combination of two commits.
rebase -i (squash): updating HEAD
rebase -i (pick): updating HEAD

Every time your branch tip is updated for any reason, Git stores that information for you in this temporary history. And you can specify older commits
with this data, as well. If you want to see the fifth prior value of the HEAD of
your repository, you can use the @{n} reference that you see in the reflog output:
$ git show HEAD@{5}

You can also use this syntax to see where a branch was some specific
amount of time ago. For instance, to see where your master branch was yesterday, you can type
$ git show master@{yesterday}

That shows you where the branch tip was yesterday. This technique only
works for data that’s still in your reflog, so you can’t use it to look for commits
older than a few months.

260

Revision Selection

To see reflog information formatted like the git log output, you can run
git log -g:
$ git log -g master
commit 734713bc047d87bf7eac9674765ae793478c50d3
Reflog: master@{0} (Scott Chacon )
Reflog message: commit: fixed refs handling, added gc auto, updated
Author: Scott Chacon 
Date:
Fri Jan 2 18:32:33 2009 -0800
fixed refs handling, added gc auto, updated tests
commit d921970aadf03b3cf0e71becdaab3147ba71cdef
Reflog: master@{1} (Scott Chacon )
Reflog message: merge phedders/rdocs: Merge made by recursive.
Author: Scott Chacon 
Date:
Thu Dec 11 15:08:43 2008 -0800
Merge commit 'phedders/rdocs'

It’s important to note that the reflog information is strictly local – it’s a log of
what you’ve done in your repository. The references won’t be the same on
someone else’s copy of the repository; and right after you initially clone a
repository, you’ll have an empty reflog, as no activity has occurred yet in your
repository. Running git show HEAD@{2.months.ago} will work only if you
cloned the project at least two months ago – if you cloned it five minutes ago,
you’ll get no results.

Ancestry References
The other main way to specify a commit is via its ancestry. If you place a ^ at the
end of a reference, Git resolves it to mean the parent of that commit. Suppose
you look at the history of your project:
$ git log --pretty=format:'%h %s' --graph
* 734713b fixed refs handling, added gc auto, updated tests
*
d921970 Merge commit 'phedders/rdocs'
|\
| * 35cfb2b Some rdoc changes
* | 1c002dd added some blame and merge stuff
|/
* 1c36188 ignore *.gem
* 9b29157 add open3_detach to gemspec file list

261

CHAPTER 7: Git Tools

Then, you can see the previous commit by specifying HEAD^, which means
“the parent of HEAD”:
$ git show HEAD^
commit d921970aadf03b3cf0e71becdaab3147ba71cdef
Merge: 1c002dd... 35cfb2b...
Author: Scott Chacon 
Date:
Thu Dec 11 15:08:43 2008 -0800
Merge commit 'phedders/rdocs'

You can also specify a number after the ^ – for example, d921970^2 means
“the second parent of d921970.” This syntax is only useful for merge commits,
which have more than one parent. The first parent is the branch you were on
when you merged, and the second is the commit on the branch that you
merged in:
$ git show d921970^
commit 1c002dd4b536e7479fe34593e72e6c6c1819e53b
Author: Scott Chacon 
Date:
Thu Dec 11 14:58:32 2008 -0800
added some blame and merge stuff
$ git show d921970^2
commit 35cfb2b795a55793d7cc56a6cc2060b4bb732548
Author: Paul Hedderly 
Date:
Wed Dec 10 22:22:03 2008 +0000
Some rdoc changes

The other main ancestry specification is the ~. This also refers to the first
parent, so HEAD~ and HEAD^ are equivalent. The difference becomes apparent
when you specify a number. HEAD~2 means “the first parent of the first parent,”
or “the grandparent” – it traverses the first parents the number of times you
specify. For example, in the history listed earlier, HEAD~3 would be
$ git show HEAD~3
commit 1c3618887afb5fbcbea25b7c013f4e2114448b8d
Author: Tom Preston-Werner 
Date:
Fri Nov 7 13:47:59 2008 -0500
ignore *.gem

262

Revision Selection

This can also be written HEAD^^^, which again is the first parent of the first
parent of the first parent:
$ git show HEAD^^^
commit 1c3618887afb5fbcbea25b7c013f4e2114448b8d
Author: Tom Preston-Werner 
Date:
Fri Nov 7 13:47:59 2008 -0500
ignore *.gem

You can also combine these syntaxes – you can get the second parent of the
previous reference (assuming it was a merge commit) by using HEAD~3^2, and
so on.

Commit Ranges
Now that you can specify individual commits, let’s see how to specify ranges of
commits. This is particularly useful for managing your branches – if you have a
lot of branches, you can use range specifications to answer questions such as,
“What work is on this branch that I haven’t yet merged into my main branch?”
DOUBLE DOT
The most common range specification is the double-dot syntax. This basically
asks Git to resolve a range of commits that are reachable from one commit but
aren’t reachable from another. For example, say you have a commit history that
looks like Figure 7-1.

FIGURE 7-1
Example history for
range selection.

You want to see what is in your experiment branch that hasn’t yet been
merged into your master branch. You can ask Git to show you a log of just those
commits with master..experiment – that means “all commits reachable by
experiment that aren’t reachable by master.” For the sake of brevity and clarity

263

CHAPTER 7: Git Tools

in these examples, I’ll use the letters of the commit objects from the diagram in
place of the actual log output in the order that they would display:
$ git log master..experiment
D
C

If, on the other hand, you want to see the opposite – all commits in master
that aren’t in experiment – you can reverse the branch names. experiment..master shows you everything in master not reachable from experiment:
$ git log experiment..master
F
E

This is useful if you want to keep the experiment branch up to date and
preview what you’re about to merge in. Another very frequent use of this syntax
is to see what you’re about to push to a remote:
$ git log origin/master..HEAD

This command shows you any commits in your current branch that aren’t in
the master branch on your origin remote. If you run a git push and your
current branch is tracking origin/master, the commits listed by git log
origin/master..HEAD are the commits that will be transferred to the server.
You can also leave off one side of the syntax to have Git assume HEAD. For example, you can get the same results as in the previous example by typing git
log origin/master.. – Git substitutes HEAD if one side is missing.
MULTIPLE POINTS
The double-dot syntax is useful as a shorthand; but perhaps you want to specify more than two branches to indicate your revision, such as seeing what commits are in any of several branches that aren’t in the branch you’re currently on.
Git allows you to do this by using either the ^ character or --not before any
reference from which you don’t want to see reachable commits. Thus these
three commands are equivalent:

264

Revision Selection

$ git log refA..refB
$ git log ^refA refB
$ git log refB --not refA

This is nice because with this syntax you can specify more than two references in your query, which you cannot do with the double-dot syntax. For instance, if you want to see all commits that are reachable from refA or refB but
not from refC, you can type one of these:
$ git log refA refB ^refC
$ git log refA refB --not refC

This makes for a very powerful revision query system that should help you
figure out what is in your branches.
TRIPLE DOT
The last major range-selection syntax is the triple-dot syntax, which specifies all
the commits that are reachable by either of two references but not by both of
them. Look back at the example commit history in Figure 7-1. If you want to
see what is in master or experiment but not any common references, you can
run
$ git log master...experiment
F
E
D
C

Again, this gives you normal log output but shows you only the commit information for those four commits, appearing in the traditional commit date ordering.
A common switch to use with the log command in this case is --leftright, which shows you which side of the range each commit is in. This helps
make the data more useful:
$ git log --left-right master...experiment
< F
< E

265

CHAPTER 7: Git Tools

> D
> C

With these tools, you can much more easily let Git know what commit or
commits you want to inspect.

Interactive Staging
Git comes with a couple of scripts that make some command-line tasks easier.
Here, you’ll look at a few interactive commands that can help you easily craft
your commits to include only certain combinations and parts of files. These
tools are very helpful if you modify a bunch of files and then decide that you
want those changes to be in several focused commits rather than one big messy commit. This way, you can make sure your commits are logically separate
changesets and can be easily reviewed by the developers working with you. If
you run git add with the -i or --interactive option, Git goes into an interactive shell mode, displaying something like this:
$ git add -i
staged
1:
unchanged
2:
unchanged
3:
unchanged

unstaged
+0/-1
+1/-1
+5/-1

*** Commands ***
1: status
2: update
5: patch
6: diff
What now>

path
TODO
index.html
lib/simplegit.rb

3: revert
7: quit

4: add untracked
8: help

You can see that this command shows you a much different view of your
staging area – basically the same information you get with git status but a
bit more succinct and informative. It lists the changes you’ve staged on the left
and unstaged changes on the right.
After this comes a Commands section. Here you can do a number of things,
including staging files, unstaging files, staging parts of files, adding untracked
files, and seeing diffs of what has been staged.

Staging and Unstaging Files
If you type 2 or u at the What now> prompt, the script prompts you for which
files you want to stage:

266

Interactive Staging

What now> 2
staged
1:
unchanged
2:
unchanged
3:
unchanged
Update>>

unstaged
+0/-1
+1/-1
+5/-1

path
TODO
index.html
lib/simplegit.rb

To stage the TODO and index.html files, you can type the numbers:
Update>> 1,2
staged
* 1:
unchanged
* 2:
unchanged
3:
unchanged
Update>>

unstaged
+0/-1
+1/-1
+5/-1

path
TODO
index.html
lib/simplegit.rb

The * next to each file means the file is selected to be staged. If you press
Enter after typing nothing at the Update>> prompt, Git takes anything selected
and stages it for you:
Update>>
updated 2 paths
*** Commands ***
1: status
2: update
5: patch
6: diff
What now> 1
staged
unstaged
1:
+0/-1
nothing
2:
+1/-1
nothing
3:
unchanged
+5/-1

3: revert
7: quit

4: add untracked
8: help

path
TODO
index.html
lib/simplegit.rb

Now you can see that the TODO and index.html files are staged and the simplegit.rb file is still unstaged. If you want to unstage the TODO file at this point,
you use the 3 or r (for revert) option:
*** Commands ***
1: status
2: update
5: patch
6: diff
What now> 3
staged
unstaged
1:
+0/-1
nothing
2:
+1/-1
nothing

3: revert
7: quit

4: add untracked
8: help

path
TODO
index.html

267

CHAPTER 7: Git Tools

3:
unchanged
Revert>> 1
staged
* 1:
+0/-1
2:
+1/-1
3:
unchanged
Revert>> [enter]
reverted one path

+5/-1 lib/simplegit.rb
unstaged
nothing
nothing
+5/-1

path
TODO
index.html
lib/simplegit.rb

Looking at your Git status again, you can see that you’ve unstaged the TODO
file:
*** Commands ***
1: status
2: update
3: revert
4: add untracked
5: patch
6: diff
7: quit
8: help
What now> 1
staged
unstaged path
1:
unchanged
+0/-1 TODO
2:
+1/-1
nothing index.html
3:
unchanged
+5/-1 lib/simplegit.rb

To see the diff of what you’ve staged, you can use the 6 or d (for diff) command. It shows you a list of your staged files, and you can select the ones for
which you would like to see the staged diff. This is much like specifying git
diff --cached on the command line:
*** Commands ***
1: status
2: update
3: revert
5: patch
6: diff
7: quit
What now> 6
staged
unstaged path
1:
+1/-1
nothing index.html
Review diff>> 1
diff --git a/index.html b/index.html
index 4d07108..4335f49 100644
--- a/index.html
+++ b/index.html
@@ -16,7 +16,7 @@ Date Finder

4: add untracked
8: help

...

- +

Navigation menu