Git

From DreamHost
Jump to: navigation, search
The instructions provided in this article or section are considered advanced.

You are expected to be knowledgeable in the UNIX shell.
Support for these instructions is not available from DreamHost tech support.
Server changes performed by DreamHost may break your modifications. You, as the user, are responsible for troubleshooting and resolving issues with your customizations.

Overview

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. For further details, view the following page at Wikipedia:

Pre-installed Git

Git is installed on all DreamHost servers. As of August 2015, the following version is installed:

[servername]$ which git
/usr/bin/git
[servername]$ git --version
git version 1.7.9.5

How to set up Git for the first time

When you first set up you Git environment, you'll want to configure a few variables. View the following article for details:

Creating your identity

The following two commands create your username and email address to be used with Git. View the SSH article for details on how to log into your server:

[servername]$ git config --global user.name "John Doe"
[servername]$ git config --global user.email johndoe@example.com

This creates a file named .gitconfig in your user's directory with the following content:

[user]
        name = John Doe
        email = johndoe@example.com

Then, check your settings that Git can find by running the following:

[servername]$ git config --list
user.name=John Doe
user.email=johndoe@example.com

Setting up the repository and commit

  1. Navigate to the directory of your application. In the example below, the application is in the directory named /mygitapp.
  2. Run the following to initialize the new repository:
    [servername]$ git init
    Initialized empty Git repository in /home/username/mygitapp/.git/
    
    • This command creates the /.git directory.
  3. Add all files in your app to the Git repository. The following command shows how to add all files:
    [servername]$ git add .
    
    • You can also choose specific files if you wish.
  4. Run the status command to confirm which files are in the staging area:
    [servername]$ git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #       new file:   file1.txt
    #       new file:   file2.txt
    #       new file:   file3.txt
    #       new file:   subdirectory1/file4.txt
    #
    
    Note2 icon.png Note:

    In this example, you can see the following files are being tracked and ready to be committed:

    • file1.txt
    • file2.txt
    • file3.txt
    • file4.txt (Note how this file is in the /subdirectory1 directory)


  5. If you're sure you're ready to commit these versions of the files, run the following command:
    git commit -m "first commit"
    
    [master (root-commit) 0bd2848] first commit
     0 files changed
     create mode 100644 file1.txt
     create mode 100644 file2.txt
     create mode 100644 file3.txt
     create mode 100644 subdirectory1/file4.txt
    
    • The -m flag allows you to add a message to this commit'.

This creates your first commit which records the snapshot you set up in your staging area. View the following link for further examples and explanations:

How to push your repository to an external server

It's always a good idea to keep a copy of your repository on an external server in case your local copy is deleted. There are two general ways you can go about this:

  • Create your application and Git repository on your home computer. Once it's ready, push the repo to your DreamHost server.
  • Create your application and Git repository on your DreamHost server. Then, you can push to GitHub

These two general options are detailed below.

Pushing your DreamHost repository to GitHub

If you've developed your site on your DreamHost server, it's recommended to put a copy on GitHub for two main purposes:

  • This creates an external backup of your code, and
  • It makes future collaboration with other developers much easier.
  1. Sign up at GitHub.
  2. Create a public or private repository at GitHub.
  3. Log into your DreamHost server via SSH and navigate to your application folder where you set up Git.
  4. Run the following command to push your app to GitHub:
    [servername]$ git remote add origin https://github.com/<username>/<application_name>
    View the following links for details:
  5. Push your repository to GitHub
    git push origin master
    View the following link for details:

Pushing your local repository to DreamHost

These directions assume that you've created your application on your home computer, and then used Git to create a local repository. The following steps guide you through how to push your local Git repo to your DreamHost server.

Pushing from Linux

  1. Setup SSH keys.
     
    [local ~]$ ssh-keygen -t rsa -b 4096 -C "DreamHost Git repo"
    
  2. Use ssh-copy-id to install your public key into your DreamHost server's 'authorized_keys' file.
    [local ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@server.dreamhost.com
    
  3. Start ssh-agent by running the following command. Make sure you use the backquote ` character and not a single quote – this backquote character is usually on the top left of your keyboard on the tilde ~ key:
    [local ~]$ eval `ssh-agent`
    
    • ssh-agent is a program that handles passwords for private keys.
  4. Run the following to add your private key to ssh-agent:
    [local ~]$ ssh-add
    
  5. Enter your private key password.
  6. Enter the following into your .bash_profile or .bashrc file to kill the ssh-agent when logging out.
    kill $SSH_AGENT_PID
    
  7. Create your local repository as shown above.
  8. Log into your DreamHost server via SSH:
    [local ~]$ ssh user@server.dreamhost.com
    
  9. Create a bare remote repository on your DreamHost server:
    [servername]$ mkdir project.git
    [servername]$ cd project.git
    [servername]$ git init --bare
    [servername]$ exit
    
  10. Back on your local computer, push to the remote repository on your DreamHost server. Make sure to change 'user' and 'server' to your actual DreamHost username and servername:
    [local ~]$ git remote add origin ssh://user@server.dreamhost.com/~/project.git
    [local ~]$ git push origin master
    
    You should see a response as follows:
    Counting objects: 3, done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 200 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To ssh://username@server.dreamhost.com/~/project.git
    * [new branch] master -> master
    

Pushing from OS X

The instructions for OS X are the same as the ones for Linux, except step #2 – instead of using 'ssh-copy-id', use the following command to copy your public key to your DreamHost server:

[local ~]$cat ~/.ssh/id_rsa.pub | ssh user@server.dreamhost.com "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

Then, proceed with all the other steps.

Pushing from Windows

  1. Download Git for Windows:
    This example uses 2.5.0.
  2. Navigate to your project folder.
    01 Git on Windows.png
  3. Right click and then select 'Git Bash Here'.
    This opens up a terminal that's already running Git.
  4. Run the following to create your user and email to be used with Git:
    [local ~]$ git config --global user.name "John Doe"
    [local ~]$ git config --global user.email johndoe@example.com
    
    This creates a .gitconfig file under your username.
  5. Run the following in your project directory:
    git init
    git add .
    git commit -m "First Commit"
    
  6. Add the remote repository.
    [local ~]$ git remote add origin ssh://user@server.dreamhost.com/~/project.git
    [local ~]$ git push origin master
    
  7. Check your DreamHost server to confirm the repository was correctly pushed by following the instructions in the next section.

Confirming your repository was correctly pushed to your DreamHost server

  1. Log into your DreamHost server via SSH.
    When you run 'git status' in the /project.git directory, you'll receive the following error because it's a 'bare' repository:
    fatal: This operation must be run in a work tree
  2. Clone the repo to a new location by running the following in your user's directory:
    [servername]$ mkdir clonedproject.git
    [servername]$ git clone $HOME/project.git $HOME/clonedproject.git
    [servername]$ cd clonedproject.git
    
    This directory contains all the files you pushed from your local machine.

Access control with Gitolite

Gitolite allows you to set up fine-grained access controls as other features. Visit the Gitolite GitHub page for further details.