# Git in Five Minutes

## Introduction 
*Git* is a version control tool that manages source code. A Git *repo* is a repository for a software project.

*GitHub* is a web-based service that uses Git. It lets developers collaborate remotely in the same repo. *GitLab* is similar to GitHub. This guide uses GitLab.

As a developer, you manage your software in a local Git repo on your computer. Then, you *push* your repo to GitLab, so other developers can retrieve your latest code.

## GitLab
1. Register for a free GitLab account [here](https://gitlab.com/users/sign_up).
1. [Sign into](https://gitlab.com/users/sign_in) your GitLab account.
1. Create a [new repo](https://gitlab.com/projects/new#blank_project):

  * Enter *hello-world* for the *Project name*.
  * Set the *Visibility Level* to *Public*.
  * Click the *Create project* button.

## Terminal window
A basic way to execute Git commands is from a terminal window. On your local computer, open a terminal window:

  * On Mac computers, open the *Terminal* app and type `git version`. It will prompt you to install Git if needed.
  * On Windows computers, install [Git for Windows](https://gitforwindows.org/). Then open a *Git Bash* terminal.

## SSH Access
You must configure GitLab to allow Git commands from your local computer. SSH access is one way to do this. Copy your local, public SSH key and paste it into GitLab to establish SSH access.

1. Open a terminal window on your local computer and copy the contents of the `id_rsa.pub` file (your public SSH key) to the clipboard:

  ```bash
  cat ~/.ssh/id_rsa.pub | pbcopy  # Mac computers
  cat ~/.ssh/id_rsa.pub | clip  # Windows computers
  ```

1. If the file does not exist, create it by entering these commands:

  ```bash
  cd
  ssh-keygen  # press Enter to accept all defaults, you may supply a password if you like
  ```
1. Repeat Step 1 above to copy the contents of the `id_rsa.pub` file to the clipboard.
1. Log into GitLab and go to [SSH Keys](https://gitlab.com/-/profile/keys).
1. Paste your SSH key into the *Key* textarea and click *Add key*.
1. You now have SSH access from your local computer to your remote GitLab account.

## Cloning your repo
1. [Sign into](https://gitlab.com/users/sign_in) your GitLab account.
1. Click your `hello-world` repo.
1. Click the *Clone* button in the main window.
1. Copy the *Clone with SSH* link to the clipboard. The link will look something like this: `git@gitlab.com:UserName/hello-world.git`, but with your UserName. This URL is the location of the repo in GitLab.
1. Go to the terminal window on your local computer and clone the repo by typing `git clone` with your SSH URL: `git clone git@gitlab.com:UserName/hello-world.git`. This creates a local copy of the `hello-world` repo on your computer.
1. Type `cd hello-world` to go into the repo folder. You execute Git commands from inside the repo folder.
1. You now have a local copy of your `hello-world` Git repo. You make changes in this local repo and push the changes to the remote GitLab repo when needed.

## Using Git
You now have a remote GitLab repo on the Internet and a local Git repo on your computer. There are many Git commands, but you only need a few to get started.
1. Open a terminal window and find your `hello-world` folder. Go into that folder by typing `cd hello-world`.
1. Check the current status of the repo by typing `git status`.
1. Type the following command to create a sample Java program for the repo:

  ```bash
  echo "// my first Java program" >> Hello.java
  ```
1. There is now a file named `Hello.java` in the `hello-world` folder. Type `git status`. Git informs you that there is a new, untracked file in the repo.
1. Type `git add Hello.java`. This adds the file to the staging area. Type `git status` to see the new file in the staging area. You must place files in the staging area before committing them to the repo.
1. Type `git commit -m "my first commit" Hello.java`. This commits the file to the local Git repo, along with a descriptive comment. The file is now officially a part of the local repo. Type `git status`. Git informs you that you should publish the local commits to your remote GitLab repo.
1. Type `git push` to push your local commits to the remote repo. Type `git status` to see that your local repo is up to date with the remote repo. Go to your [GitLab account](https://gitlab.com/) and see the new file in the `hello-world` repo.
1. Congratulations! You just made your first commit and pushed it to GitLab 👏.

Whenever you add, change, or delete files in your local repo, you must execute `git add`, `git commit`, and `git push` like you did above. Git retains all history, including deleted files.

## Summary
This tutorial is a basic introduction to Git and GitLab. You can do a lot more: branches, tags, advanced Git commands, and more. But you now know the basic commands to start versioning your software and collaborating with others.

Thanks for reading! 👍

*Follow me on Twitter [`@realEdwinTorres`](https://twitter.com/realEdwinTorres) for more programming tips and help.*



