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.
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.
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.
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
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.
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.
To install Git by Homebrew do the following:
Go to Homebrew website.
Copy and paste the command listed under Install Homebrew into a terminal window.
At the next prompt enter your password and hit Return.
You will receive an installation successful message when complete.
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.
To update Homebrew run brew update at the command prompt.
To install Git with Homebrew the command is as follows:
brew install git
To install Git by MacPorts do the following:
Go to MacPorts website.
Click on the link for your operating system.
Click on the file after it downloads to start the installer.
Click on the Continue button to begin the install.
Click on the Agree button to accept the license agreement.
To leave the default installation location click on the Install button or click the Change Install Location button to change the default location.
Enter your password when prompted and click on the Install Software button.
8.After the install finishes, click the Close button to exit the installer.
Open a terminal window.
To install Git use the following command:
$ sudo port install git-core +svn +doc +bash_completion +gitweb
To install Git by the package installer you will need to do the following:
Go to the Git website and download the current installer for your operating system.
Double click on the downloaded file to begin the install.
Click on the Continue button to begin the install.
Click on the large Install for all users of this computer button and click on Continue to begin the installation.
Click Install to continue with the default location. If you need to change the installation location click Change Install Location button.
Enter your username and password and click Install Software to continue.
Click the Close button to exit the installer.
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 firstname.lastname@example.org MacBook-Pro:~ user$
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.
Select Run to begin the installation.
Click on Yes to continue.
Click Next to continue.
Click Next to continue.
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.
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 start menu folder by clicking on Next or use Browse to select a new folder location.
Keep the default line ending conversion by clicking Next. To change the default, choose one of the two other choices before clicking on Next.
The program will now begin installing.
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.
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 <email@example.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.
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:
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:
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:
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:
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.
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.
The most basic commands you will need to know to start using Git are in the table below:
|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 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:
|-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|
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:
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:
|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|
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
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
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
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 <firstname.lastname@example.org>:/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.
Log in to your Git server with you normal user account. Enter the command:
Then hit enter. Depending on your distribution you may see a warning about the gitolite.rc file hit enter to continue.
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.
The gitolite.rc file will now open in your preferred text editor. The default settings should be used, so exit the file to continue.
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.
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:
Copy the user's public key:
cp /path/<user>/public/key.pub ~/gitolite-admin/keydir/<lmartin>.pub
Configure the user's name:
git config --global user.name <lmartin>
Configure the user's email:
git config --global user.email <email@example.com>
Configure the text editor:
git config --global core-editor <editor-name>
Now add the user's public key:
git add keydir/<lmartin>.pub
Commit the change:
git commit -a -m "New user lmartin added"
Finally push the changes up to the server:
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.
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).