How do I set up a Git repository?

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:

[server]$ which git
/usr/bin/git
[server]$ 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:

[server]$ git config --global user.name "John Doe"
[server]$ 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:

[server]$ 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:
    [server]$ 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:
    [server]$ 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:
    [server]$ 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
    #
    

    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:
    [server]$ 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:

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.

See also