1

How do I set up a Git repository?

 3 years ago
source link: https://help.dreamhost.com/hc/en-us/articles/216445317-How-do-I-set-up-a-Git-repository-
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How do I set up a Git repository?

This article assumes you're setting up your Git repository on your DreamHost server. View the SSH overview article for instructions on how to log into your server.

Pre-installed Git

Git is installed on all DreamHost servers. Run the following commands to view the location and version.

[server]$ which git
/usr/bin/git
[server]$ git --version
git version 2.20.1

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:

You can also just proceed with the following steps for a basic setup.

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 [email protected]

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

[user]
  name = John Doe
  email = [email protected]

Check to confirm Git can find those settings by running the following:

[server]$ git config --list
user.name=John Doe
[email protected]

Setting up the repository and commit

  1. Navigate to the directory of your application (or website). This example sets up Git in your website's directory. Change username to your Shell user and example.com to your website.
    [server]$ cd /home/username/example.com
    
  2. Run the following to initialize the new repository:
    [server]$ git init
    Initialized empty Git repository in /home/username/example.com/.git/
    
    This command creates the /.git directory and your git repository.
  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
    
    Initial commit Changes to be committed: (use "git rm --cached ..." to unstage) new file: favicon.ico new file: images/image1.jpg new file: images/image2.jpg new file: images/image3.jpg new file: index.html new file: mystyles.css

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

    • favicon.ico
    • images/image1.jpg
    • images/image2.jpg
    • images/image3.jpg
    • index.html
    • mystyles.css
  5. If you're sure you're ready to commit (save) these versions of the files to your Git repository, run the following command:

    Make sure to add a short and clear message that explains what this commit was for. View the following article for details on how to create a good commit message.

    [server]$ git commit -m "Committing website files for the first time"
    [master (root-commit) e11cdb7] Committing website files for the first time
     6 files changed, 34 insertions(+)
     create mode 100644 favicon.ico
     create mode 100644 images/image1.jpg
     create mode 100644 images/image2.jpg
     create mode 100644 images/image3.jpg
     create mode 100644 index.html
    create mode 100644 mystyles.css
    The -m flag allows you to add a message to this commit.

This creates your first commit. You can see your commit history by running the following:

[server]$ git log --oneline
e11cdb7 Committing website files for the first time

You don't need the --oneline flag, this just shortens the response so it's easier to read. View the following link for further examples and explanations:

See also

Did this article answer your questions?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK