Hello Git

A beginner's tutorial on using Git and GitLab

ยท

4 min read

Introduction

This tutorial is for beginners who are new to Git and GitLab .

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." Use Git locally on your computer to manage software versions.

GitLab is a DevOps platform. It can host your local Git repos for remote collaboration, and a whole lot more. Some other Git servers are GitHub and Bitbucket.

Register with GitLab and Create a Repo

  1. Register for a GitLab account here or on your private GitLab server.
  2. From a web browser, log into your GitLab account.
  3. Create a new repo:

  4. 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.
  2. Install Git:

    • Windows: 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.
  2. Create your SSH key:

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

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

      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.
  3. 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.
    • In the Key textbox, paste your public SSH key from the clipboard.
    • Click the Add key button.
  4. 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.
  2. Go to your hello-world repo, click the Clone button, and copy the Clone with SSH link to the clipboard.
  3. From your local computer, open a terminal and go (cd) to a directory to work from.
  4. 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:

    git clone git@gitlab.com:JohnSmith/hello-world.git
    
  5. There is now a hello-world folder locally on your computer. From the terminal, go into the folder: cd hello-world

  6. Enter git status to see the current state of the repo. There are no changes.
  7. Create a few local files:

    echo "red blue green" > colors.txt
    echo "apple cherry watermelon" > fruits.txt
    
  8. Enter git status to see the current state of the repo. Now there are local changes.

  9. Add the changes to the Git staging area:

    git add colors.txt fruits.txt  # or simply: git add .
    
  10. Commit the changes to the local repo:

    git commit -m "my first Git commit" .
    
  11. Now the two new files are part of your repo. Push your local changes to the master branch of the remote GitLab repo:

    git push origin master
    git status  # repo is clean
    
  12. 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 for programming tips, software engineering content, and career advice. ๐Ÿ˜Š

ย