DEV Community

Cover image for Git in Five Minutes
Edwin Torres
Edwin Torres

Posted on

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.
  2. Sign into your GitLab account.
  3. Create a new repo:
  • 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. 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:
  cat ~/.ssh/id_rsa.pub | pbcopy  # Mac computers
  cat ~/.ssh/id_rsa.pub | clip  # Windows computers
Enter fullscreen mode Exit fullscreen mode
  1. If the file does not exist, create it by entering these commands:
  cd
  ssh-keygen  # press Enter to accept all defaults, you may supply a password if you like
Enter fullscreen mode Exit fullscreen mode
  1. Repeat Step 1 above to copy the contents of the id_rsa.pub file to the clipboard.
  2. Log into GitLab and go to SSH Keys.
  3. Paste your SSH key into the Key textarea and click Add key.
  4. You now have SSH access from your local computer to your remote GitLab account.

Cloning your repo

  1. Sign into your GitLab account.
  2. Click your hello-world repo.
  3. Click the Clone button in the main window.
  4. 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.
  5. 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.
  6. Type cd hello-world to go into the repo folder. You execute Git commands from inside the repo folder.
  7. 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.
  2. Check the current status of the repo by typing git status.
  3. Type the following command to create a sample Java program for the repo:
  echo "// my first Java program" >> Hello.java
Enter fullscreen mode Exit fullscreen mode
  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.
  2. 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.
  3. 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.
  4. 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 and see the new file in the hello-world repo.
  5. 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 for more programming tips and help.

Top comments (0)