# Hello Git

## Introduction

This tutorial is for beginners who are new to  [Git](https://git-scm.com/)  and  [GitLab](https://gitlab.com/) .

_Git_ is a  "[free and open source distributed version control system](https://git-scm.com/)  designed to handle everything from small to very large projects with speed and efficiency." Use Git locally on your computer to manage software versions.

_GitLab_ is a  [DevOps platform](https://about.gitlab.com/what-is-gitlab/). It can host your local Git repos for remote collaboration, and a whole lot more. Some other Git servers are [GitHub](https://github.com/)  and  [Bitbucket](https://bitbucket.org/product).

## Register with GitLab and Create a Repo

1. Register for a GitLab account [here](https://gitlab.com/users/sign_up) or on your private GitLab server.
1. From a web browser, log into your GitLab account.
1. Create a new repo:

  * Create a new project:  [https://gitlab.com/projects/new](https://gitlab.com/projects/new)
  * Click _Create blank project_.
  * Project name: **hello-world**
  * Click the _Create project_ button.

1. Your remote GitLab repo is created.

## Install Git Locally

Make sure that you have Git locally on your computer.

1. Check if you already have Git. From your local computer, open a macOS Terminal or Git Bash terminal and enter: `git --version`. If you have Git, then continue to [Establish SSH Access](#establish-ssh-access).
1. Install Git:

  * _Windows_: https://git-scm.com/download/win
  * _macOS_: From the Terminal app, enter `git --version` and follow instructions to install _Xcode Command Line Tools_.

For the remainder of this tutorial, "terminal" refers to either the _macOS Terminal_ app or _Windows Git Bash_ terminal.

## Establish SSH Access

To access your remote GitLab repos from your local computer, you must first establish SSH access. This requires uploading your public SSH key from your computer to GitLab.

1. Check if you already have an SSH key:

  * From the _macOS_ terminal, enter: `cat ~/.ssh/id_rsa.pub | pbcopy`
  * From the _Windows Git Bash_ terminal, enter: `cat ~/.ssh/id_rsa.pub | clip`
  * If the command succeeds, then your public SSH key is in the clipboard. Otherwise go to the next step.

1. Create your SSH key:

  * **Warning**: this step will create/overwrite these files in your home directory:

    ```bash
    ~/.ssh/id_rsa
    ~/.ssh/id_rsa.pub
    ```
   
  * From the terminal, enter the following commands, replacing `YOUREMAIL@MAIL.COM` with your email address:

    ```bash
    cd  # work from your home directory
    ssh-keygen -o -t rsa -b 4096 -C "YOUREMAIL@MAIL.COM"    
    ```

  * Press the `Enter` key to accept all defaults. Do not specify a password, unless you want to enter a password each time you interact with the remote Git server.
  * Go back to _Step 1_ above to copy your public SSH key to the clipboard.

1. Add your SSH key to your GitLab account:

  * From a web browser, log into your GitLab account.
  * Go to SSH Keys: [https://gitlab.com/-/profile/keys](https://gitlab.com/-/profile/keys).
  * In the _Key_ textbox, paste your public SSH key from the clipboard.
  * Click the _Add key_ button.

1. You know have SSH access from your local computer to your remote GitLab account.

## Use Git Locally and GitLab Remotely

Now try to use Git locally and interact with your remote repos in GitLab.

1. From a web browser, log into your GitLab account.
1. Go to your **hello-world** repo, click the *Clone* button, and copy the _Clone with SSH_ link to the clipboard.
1. From your local computer, open a terminal and go (`cd`) to a directory to work from.
1. Enter the following command to _clone_ your GitLab repo locally on your computer. Paste the SSH link at the end. For example, if your GitLab username is `JohnSmith`:

  ```bash
  git clone git@gitlab.com:JohnSmith/hello-world.git
  ```

1. There is now a `hello-world` folder locally on your computer. From the terminal, go into the folder: `cd hello-world`
1. Enter `git status` to see the current state of the repo. There are no changes.
1. Create a few local files:

  ```bash
  echo "red blue green" > colors.txt
  echo "apple cherry watermelon" > fruits.txt
  ```

1. Enter `git status` to see the current state of the repo. Now there are local changes.
1. Add the changes to the Git staging area:

  ```bash
  git add colors.txt fruits.txt  # or simply: git add .
  ```

1. Commit the changes to the local repo:

  ```bash
  git commit -m "my first Git commit" .
  ```

1. Now the two new files are part of your repo. Push your local changes to the `master` branch of the remote GitLab repo:

  ```bash
  git push origin master
  git status  # repo is clean
  ```

1. From a web browser, go to your GitLab `hello-world` repo. Refresh the browser and verify that your local changes made it to the remote server.

**Congratulations!** You have successfully created a GitLab account and repo, established a local Git repo, and used Git both locally and remotely. Well done! Now learn more advanced Git commands and try them both locally and remotely.

Thanks for reading!

Follow me on Twitter [`@realEdwinTorres`](https://twitter.com/realEdwinTorres) for programming tips, software engineering content, and career advice. 😊
