Linode Library Home
Linode Library RSS Feed
Home :: Linux Tools :: Version Control
Print View View Source

Git Source Control Management

Published: by

Git is version control software. Version control is used to keep track of changes to a file or group of files. Version control is usually associated with source code, but can be used on any file type. For example, a technical document may be placed in version control in order to keep track of changes made to the document. Version control can also be applied to graphics. You can rollback to a previous version to keep track of the changes made on a particular file.

This document will provide rudimentary installation instructions as well as some basics for getting started. However, the way you use Git is entirely up to you.

Contents

Installing Git

Git can be installed on a variety of operating systems. It can also be downloaded and compiled. However, it is recommended that you use your Linux distribution's repository installer as this is usually the most reliable installation method.

Note

The steps in this guide are written using a non-root user. If you are logged in as Root you will not need sudo for certain steps.

Linux Installation

For Debian and Ubuntu systems, the command is as follows:

sudo apt-get install git

For a CentOS system, the command is as follows:

sudo yum install git

For a Fedora system, the command is as follows:

sudo yum install git-core

For an Arch Linux system, the command is as follows:

sudo pacman -Sy git

For a Gentoo system, the command is as follows:

sudo emerge ask --verbose dev-util/git

Local System Installation

Git may be installed on a local system running either Mac OS X or Windows. The installation procedure for both types of operating systems is outlined below.

Mac OS X

There are different ways to install Git on OS X. This may be achieved by installing Homebrew or MacPorts before Git or by downloading the Git installer package. Regardless of which method you choose, make sure you verify what operating system version you are currently running.

Installing Homebrew and Git

To install Git by Homebrew do the following:

  1. Go to Homebrew website.

  2. Copy and paste the command listed under Install Homebrew into a terminal window.

    Enter the install command.
  3. At the next prompt enter your password and hit Return.

    Enter your password.
  4. You will receive an installation successful message when complete.

    Enter your password.
  5. It is recommended that you install Brew Doctor to fix any errors before installing Git. Enter brew doctor at the prompt to install. If any errors appear, they will be fixed, and you will see a message stating what was repaired.

    Install brew doctor.
  6. To update Homebrew run brew update at the command prompt.

    Update Homebrew.
  7. To install Git with Homebrew the command is as follows:

    brew install git
    

Installing MacPorts and Git

To install Git by MacPorts do the following:

  1. Go to MacPorts website.

  2. Click on the link for your operating system.

  3. Click on the file after it downloads to start the installer.

  4. Click on the Continue button to begin the install.

    Begin MacPort install.
  5. Click on the Agree button to accept the license agreement.

Agree to the license agreement.
  1. To leave the default installation location click on the Install button or click the Change Install Location button to change the default location.

    Change default location.
  2. Enter your password when prompted and click on the Install Software button.

    Enter your password.

8.After the install finishes, click the Close button to exit the installer.

Click close to exit.
  1. Open a terminal window.

  2. To install Git use the following command:

    $ sudo port install git-core +svn +doc +bash_completion +gitweb
    

Installing Git by Package Installer

To install Git by the package installer you will need to do the following:

  1. Go to the Git website and download the current installer for your operating system.

  2. Double click on the downloaded file to begin the install.

  3. Click on the Continue button to begin the install.

    Click on continue.
  4. Click on the large Install for all users of this computer button and click on Continue to begin the installation.

    Click on install for all users of this computer.
  5. Click Install to continue with the default location. If you need to change the installation location click Change Install Location button.

    Click on change install location.
  6. Enter your username and password and click Install Software to continue.

    Enter username and password and click install software.
  7. Click the Close button to exit the installer.

    Click close to exit.
  8. Check your settings by issuing the command:

    git config --list
    

Your output should show at least your username and email address. Sample output is below:

MacBook-Pro:~ user$ git config --list
user.name=user
user.email=user@email.com
MacBook-Pro:~ user$

Windows

To install Git on Windows you will need to download the installer from the Git website:

1.Download the most current version for your operating system by double clicking on the package name.

Click to download version for Windows.
  1. Select Run to begin the installation.

    Click Run to start the installer.
  2. Click on Yes to continue.

    Click Yes to continue.
  3. Click Next to continue.

    Click Next to continue.
  4. Click Next to continue.

    Click Next to continue.
  5. To accept the default location click on Next. If you need to change the installation folder, click on the Browse button and select a new location.

    Click Next for default or Browse to change.
  6. To accept the default components to be installed click Next. Otherwise, click on the additional components to be installed before clicking the Next button.

    Accept the default or select additional components.
  7. Accept the default start menu folder by clicking on Next or use Browse to select a new folder location.

    Select the start menu folder.
  8. Keep the default line ending conversion by clicking Next. To change the default, choose one of the two other choices before clicking on Next.

    Select line ending conversions.
  9. The program will now begin installing.

Git is installing.
  1. Click Finish to exit the installer.
Click Finish to exit.

Note

It is strongly recommended that you keep the default settings for line ending conversions. Changing from the default may result in formatting problems when viewed on your Linode.

  1. Once you open Git you can select either Create New, Clone Existing or Open Existing Repository. In this example, create new repository was selected.
Create New Repository.
  1. Enter a directory name or click on Browse to navigate to a directory.
Directory for a new repository.
  1. A blank repository has been created.
Blank repository.

Configuring Git

After the installation is complete, you will need to configure Git for first time use. There is a built-in tool called git config that obtains and sets configuration variables for how Git functions and appears. These configuration variables are located in three different places on a Linux system:

  • /etc/gitconfig file - stores the information for all system users and their respective repositories
  • ~/.gitconfig file - pertains to your user
  • .git/config - this is the configuration file of your current working repository.

For a Windows system, the .gitconfig is located in the $HOME directory of the user's profile. The full path is C:\Document and Settings\$USER or C:\Users\$USER

After installing Git make sure your username and email address are set correctly. To verify use the command:

git config --list

If your name and email are not listed in the output, you may set them manually. Use the following command to set your name:

git config --global user.name <name>

Use the following command to set your email address:

git config --global user.email <user@email.com>

Set your default text editor with the command:

git config --global core.editor <editor-name>

Below is a sample gitconfig file with the username and email address.

Sample gitconfig.

Working with the Repository

There are two ways to obtain a Git repository, either by importing or copying. If you have an existing project and you want to start using Git to keep track of its changes, you will need to run the command from the existing project's directory:

git init

This will create a new subdirectory called .git. In order to start tracking changes made to this project, you will first want to add files to be tracked by using the following command:

git add <filename>

After adding the files you wish to track, use the command:

git commit

Enter the commit message using your favorite file editor, then save and quit to finish writing the commit.

To copy an existing Git repository use the command:

git clone

Just remember that every file will be copied when the git clone command is used. When using a URL, the command is:

git clone <url>

To check the status of your files use the command:

git status

The status command will also display the status of your submodules. When this command is run it will tell you what branch you are on, and if any tracked files have been modified.

Setup a Local Empty Repository

A bare repository contains no files but the .git folder. It is used to configure an initial Git server so you can export your repository into a new bare repository. For this example, we will use a repository named practice. Keep the naming convention simple e.g. lowercase, short names, etc. To create a bare or empty repository use the command:

git init --bare practice.git

If you already have a repository on your local machine and want to push it to your new Git server, use the command:

git remote set-url origin git@<IP address>:practice.git

Now you should be able to push and pull files from your local PC to your Linode.

Basic File Commands

The most basic commands you will need to know to start using Git are in the table below:

Command Description Example
git add adds a file to a repository git add <filename>
git rm removes a file from a repository git rm <filename>
git mv moves or renames a tracked file, directory, or symlink git mv <file_from> <file_to>
git branch lists all the local and remote branches git branch <branch name>
git commit commits all staged objects N/A
git pull downloads all changes from the remote repo and merges them in a specified repo file git pull <repo> <refspec>
git push publishes the changes to the remote repo git push <repo>
git log creates a log of all commits, includes: person, commit, date, time, and msg N/A
gitk displays a visual commit history in a graphical tool N/A
git commit --amend replaces the latest commit in the current branch with a new commit, rewriting history N/A

Note: Refspec is used to configure which remote branch or branches should be used, and the local branch or branches should be named. The syntax for refspec is as follows:

git pull <name> <source-branch>:<destiantion-branch>

Branches

Branches are made up of individual code changes. For example, by default the main branch is called "master". The core idea is that branches are created for each feature, implementation, or bug fix. So to use an overly simplified example, the first or master branch is the truck of the tree and each branch is a new iteration. In this example, there might be branches broken out by new feature names or specific bug fixes. Git can also be used to track document changes, as well as code.

The basic options used with the git branch command are listed below:

Option Description
-r lists the remote branches
-a shows both local and remote branches
-m renames an old branch
-d deletes a branch
-r -d deletes a remote branch

Working with Remote Repositories

Remote repositories are hosted on a network or another location on the Internet. You can have several remote repositories. This section will provide some basics for working with remote repositories.

To view which remote servers are configured, use the command:

git remote

This will display the short names of your remote repositories. If your repository was cloned, you would see a repository called "origin". The default name origin comes from the cloned repository. To view more information about the remote repositories, use the command:

git remote -v

Below are some basic commands for working with remote repositories:

Command Description
git remote add [remote-name] [url] adds a new remote repository
git fetch [repository [refspec]] gathers all the data from a remote project that you do not have yet
git pull obtains and merges a remote branch into your current branch
git push [remote-name] [branch-name] moves your data from your branch to your server
git remote show [remote-name] displays information about the remote you specified
git remote rename [old-name] [new-name] renames a remote
git remote rm [name] removes the remote you specified

Installing Gitolite

  1. Gitolite works on top of Git, and allows access to remote users without having to give them shell access. Install Gitolite with the command:

    sudo apt-get install gitolite
    
  2. A Linux user needs to be created in order for Gitolite to manage its configuration. This Gitolite user works through Git so do not set a password. The command to create the git user is as follows:

    sudo adduser --system --group --disabled-password git
    

Create a SSH Keys

  1. If you have not already created SSH keys, you will need to do this now. The keys must be created on your local machine. In a terminal window enter the command:

    ssh-keygen -t rsa
    
  2. Accept the defaults and do not enter a password. The public key will need to be copied up to your Git server. Use the command:

    scp ~/.ssh/id_rsa.pub <username>@<git-server-IP>:/<location><key-name>
    

The location is where you copied your key. For example:

scp ~/.ssh/id_rsa.pub <git@192.168.0.1>:/tmp/git.pub

The private key remains on your system and should not be copied anywhere else.

For Windows users consult the Windows Operating System section in our Public Key guide.

Configure Gitolite

  1. Log in to your Git server with you normal user account. Enter the command:

    gl-setup /<key-location>/
    
  2. Then hit enter. Depending on your distribution you may see a warning about the gitolite.rc file hit enter to continue.

    Enable SSH key.
  3. You will be prompted to enter a number to select a text editor. Select the editor you prefer. In this example, number 2 was selected which is Nano.

    Select a text editor.
  4. The gitolite.rc file will now open in your preferred text editor. The default settings should be used, so exit the file to continue.

    Viewing the gitolite.rc file.

Adding Users Prerequisites

Before users may be added to any of your projects, you will need to clone the Gitolite information from your server to your local machine. Enter the following command on your Git server:

git clone <user>@<IP address>:gitolite-admin

This will create a new directory called gitolite-admin. Navigate to the newly created directory and if you run a ls command you will see two files inside conf and keydir. The keydir is the directory where the user keys are stored.

Adding Users

In order to add a new user, you will need their name, email, and public key. For this example, the name lmartin will be used. The procedure is as follows:

  1. Copy the user's public key:

    cp /path/<user>/public/key.pub ~/gitolite-admin/keydir/<lmartin>.pub
    
  2. Configure the user's name:

    git config --global user.name <lmartin>
    
  3. Configure the user's email:

    git config --global user.email <lmartin@email.com>
    
  4. Configure the text editor:

    git config --global core-editor <editor-name>
    
  5. Now add the user's public key:

    git add keydir/<lmartin>.pub
    
  6. Commit the change:

    git commit -a -m "New user lmartin added"
    
  7. Finally push the changes up to the server:

    git push
    

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Creative Commons License

This guide is licensed under a Creative Commons Attribution-NoDerivs 3.0 United States License.

Last edited by Bobbi L on Friday, February 28th, 2014 (r4303).