Hello Git: A Beginner’s Tutorial on Git and GitLab

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.” U…



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

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:

    ~/.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.

  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.
  • 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.
  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
  1. There is now a hello-world folder locally on your computer. From the terminal, go into the folder: cd hello-world
  2. Enter git status to see the current state of the repo. There are no changes.
  3. Create a few local files:
  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.
  2. Add the changes to the Git staging area:
  git add colors.txt fruits.txt  # or simply: git add .
  1. Commit the changes to the local repo:
  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:
  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 for programming tips, software engineering content, and career advice. 😊


Print Share Comment Cite Upload Translate
APA
Edwin Torres ✨ | Sciencx (2024-03-28T10:37:41+00:00) » Hello Git: A Beginner’s Tutorial on Git and GitLab. Retrieved from https://www.scien.cx/2022/01/01/hello-git-a-beginners-tutorial-on-git-and-gitlab/.
MLA
" » Hello Git: A Beginner’s Tutorial on Git and GitLab." Edwin Torres ✨ | Sciencx - Saturday January 1, 2022, https://www.scien.cx/2022/01/01/hello-git-a-beginners-tutorial-on-git-and-gitlab/
HARVARD
Edwin Torres ✨ | Sciencx Saturday January 1, 2022 » Hello Git: A Beginner’s Tutorial on Git and GitLab., viewed 2024-03-28T10:37:41+00:00,<https://www.scien.cx/2022/01/01/hello-git-a-beginners-tutorial-on-git-and-gitlab/>
VANCOUVER
Edwin Torres ✨ | Sciencx - » Hello Git: A Beginner’s Tutorial on Git and GitLab. [Internet]. [Accessed 2024-03-28T10:37:41+00:00]. Available from: https://www.scien.cx/2022/01/01/hello-git-a-beginners-tutorial-on-git-and-gitlab/
CHICAGO
" » Hello Git: A Beginner’s Tutorial on Git and GitLab." Edwin Torres ✨ | Sciencx - Accessed 2024-03-28T10:37:41+00:00. https://www.scien.cx/2022/01/01/hello-git-a-beginners-tutorial-on-git-and-gitlab/
IEEE
" » Hello Git: A Beginner’s Tutorial on Git and GitLab." Edwin Torres ✨ | Sciencx [Online]. Available: https://www.scien.cx/2022/01/01/hello-git-a-beginners-tutorial-on-git-and-gitlab/. [Accessed: 2024-03-28T10:37:41+00:00]
rf:citation
» Hello Git: A Beginner’s Tutorial on Git and GitLab | Edwin Torres ✨ | Sciencx | https://www.scien.cx/2022/01/01/hello-git-a-beginners-tutorial-on-git-and-gitlab/ | 2024-03-28T10:37:41+00:00
https://github.com/addpipe/simple-recorderjs-demo