Chapter 1. Collaborating

Effective revision control is essential to all multi-developer projects. It allows all developers in a team to create, review, revise, and document code in a systematic and orderly manner. Red Hat Enterprise Linux 6 supports three of the most popular open-source revision control systems: Git, SVN, and CVS.
The following sections provide a brief overview and references to relevant documentation for each tool.

1.1. Git

Git is a distributed revision control system with a peer-to-peer architecture. As opposed to centralized version control systems with a client-server model, Git ensures that each working copy of a Git repository is its exact copy with complete revision history. This not only allows you to work on and contribute to projects without the need to have permission to push your changes to their official repositories, but also makes it possible for you to work with no network connection.

1.1.1. Installing and Configuring Git

Installing the git Package

In Red Hat Enterprise Linux 6, Git is provided by the git package. To install the git package and all its dependencies on your system, type the following at a shell prompt as root:
~]# yum install git

Configuring the Default Text Editor

Certain Git commands, such as git commit, require the user to write a short message or make some changes in an external text editor. To determine which text editor to start, Git attempts to read the value of the GIT_EDITOR environment variable, the core.editor configuration option, the VISUAL environment variable, and finally the EDITOR environment variable in this particular order. If none of these options and variables are specified, the git command starts vi as a reasonable default option.
To change the value of the core.editor configuration option in order to specify a different text editor, type the following at a shell prompt:
git config --global core.editor command
Replace command with the command to be used to start the selected text editor.

Example 1.1. Configuring the Default Text Editor

To configure Git to use vim as the default text editor, type the following at a shell prompt:
~]$ git config --global core.editor vim

Setting Up User Information

In Git, each commit (or revision) is associated with the full name and email of the person responsible for it. By default, Git uses an identity based on the user name and the host name.
To change the full name associated with your Git commits, type the following at a shell prompt:
git config --global user.name "full name"
To change the email address associated with your Git commits, type:
git config --global user.email "email_address"

Example 1.2. Setting Up User Information

To configure Git to use John Doe as your full name and john@example.com as your email address, type the following at a shell prompt:
~]$ git config --global user.name "John Doe"
~]$ git config --global user.email "john@example.com"

1.1.2. Creating a New Repository

A repository is a place where Git stores all files that are under revision control, as well as additional data related to these files, such as the complete history of changes or information about who made those changes and when. Unlike in centralized revision control systems like Subversion or CVS, a Git repository and a working directory are usually the same. A typical Git repository also only stores a single project and when publicly accessible, it allows anyone to create its clone with a complete revision history.

Initializing an Empty Repository

To create a new, empty Git repository, change to the directory in which you want to keep the repository and type the following at a shell prompt:
git init
This creates a hidden directory named .git in which all repository information is stored.

Importing Data to a Repository

To put an existing project under revision control, create a Git repository in the directory with the project and run the following command:
git add .
This marks all files and directories in the current working directory as ready to be added to the Git repository. To proceed and actually add this content to the repository, commit the changes by typing the following at a shell prompt:
git commit [-m "commit message"]
Replace commit message with a short description of your revision. If you omit the -m option, this command allows you to write the commit message in an external text editor. For information on how to configure the default text editor, see the section called “Configuring the Default Text Editor”.

1.1.3. Cloning an Existing Repository

To clone an existing Git repository, type the following at a shell prompt:
git clone git_repository [directory]
Replace git_repository with a URL or a path to the Git repository you want to clone, and directory with a path to the directory in which you want to store the clone.

1.1.4. Adding, Renaming, and Deleting Files

Adding Files and Directories

To add an existing file to a Git repository and put it under revision control, change to the directory with your local Git repository and type the following at a shell prompt:
git add file...
Replace file with the file or files you want to add. This command marks the selected file or files as ready to be added to the Git repository. Similarly, to add all files that are stored in a certain directory to a Git repository, type:
git add directory...
Replace directory with the directory or directories you want to add. This command marks all files in the selected directory or directories as ready to be added to the Git repository.
To proceed and actually add this content to the repository, commit the changes as described in Section 1.1.6, “Committing Changes”.

Renaming Files and Directories

To rename an existing file or directory in a Git repository, change to the directory with your local Git repository and type the following at a shell prompt:
git mv old_name new_name
Replace old_name with the current name of the file or directory and new_name with the new name. This command renames the selected file or directory and marks it as ready to be renamed in the Git repository.
To proceed and actually rename the content in the repository, commit the changes as described in Section 1.1.6, “Committing Changes”.

Deleting Files and Directories

To delete an existing file from a Git repository, change to the directory with your local Git repository and type the following at a shell prompt:
git rm file...
Replace file with the file or files you want to delete. This command deletes all selected files and marks them as ready to be deleted form the Git repository. Similarly, to delete all files that are stored in a certain directory from a Git repository, type:
git rm -r directory...
Replace directory with the directory or directories you want to delete. This command deletes all selected directories and marks them as ready to be deleted from the Git repository.
To proceed and actually delete this content from the repository, commit the changes as described in Section 1.1.6, “Committing Changes”.

1.1.5. Viewing Changes

Viewing the Current Status

To determine the current status of your local Git repository, change to the directory with the repository and type the following command at a shell prompt:
git status
This command displays information about all uncommitted changes in the repository (new file, renamed, deleted, or modified) and tells you which changes will be applied the next time you commit them. For information on how to commit your changes, see Section 1.1.6, “Committing Changes”.

Viewing Differences

To view all changes in a Git repository, change to the directory with the repository and type the following at a shell prompt:
git diff
This command displays changes between the files in the repository and their latest revision. If you are only interested in changes in a particular file, supply its name on the command line as follows:
git diff file...
Replace file with the file or files you want to view.

1.1.6. Committing Changes

To apply your changes to a Git repository and create a new revision, change to the directory with the repository and type the following command at a shell prompt:
git commit [-m "commit message"]
Replace commit message with a short description of your revision. This command commits all changes in files that are explicitly marked as ready to be committed. To commit changes in all files that are under revision control, add the -a command line option as follows:
git commit -a [-m "commit message"]
Note that if you omit the -m option, the command allows you to write the commit message in an external text editor. For information on how to configure the default text editor, see the section called “Configuring the Default Text Editor”.

1.1.7. Sharing Changes

Unlike in centralized version control systems such as CVS or Subversion, when working with Git, project contributors usually do not make their changes in a single, central repository. Instead, they either create a publicly accessible clone of their local repository, or submit their changes to others over email as patches.

Pushing Changes to a Public Repository

To push your changes to a publicly accessible Git repository, change to the directory with your local repository and type the following at a shell prompt:
git push remote_repository
Replace remote_repository with the name of the remote repository you want to push your changes to. Note that the repository from which you originally cloned your local copy is automatically named origin.

Creating Patches from Individual Commits

To create patches from your commits, change to the directory with your local Git repository and type the following at a shell prompt:
git format-patch remote_repository
Replace remote_repository with the name of the remote repository from which you made your local copy. This creates a patch for each commit that is not present in this remote repository.

1.1.8. Updating a Repository

To update your local copy of a Git repository and get the latest changes from a remote repository, change to the directory with your local Git repository and type the following at a shell prompt:
git fetch remote_repository
Replace remote_repository with the name of the remote repository. This command fetches information about the current status of the remote repository, allowing you to review these changes before applying them to your local copy. To proceed and merge these changes with what you have in your local Git repository, type:
git merge remote_repository
Alternatively, you can perform both these steps at the same time by using the following command instead:
git pull remote_repository

1.1.9. Additional Resources

A detailed description of Git and its features is beyond the scope of this book. For more information about this revision control system, see the resources listed below.

Installed Documentation

  • gittutorial(7) — The manual page named gittutorial provides a brief introduction to Git and its usage.
  • gittutorial-2(7) — The manual page named gittutorial-2 provides the second part of a brief introduction to Git and its usage.
  • Git User's Manual — HTML documentation for Git is located at /usr/share/doc/git-1.7.1/user-manual.html.

Online Documentation

  • Pro Git — The online version of the Pro Git book provides a detailed description of Git, its concepts and its usage.