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
- 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.
- 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
- 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
- Repeat Step 1 above to copy the contents of the
id_rsa.pub
file to the clipboard. - Log into GitLab and go to SSH Keys.
- Paste your SSH key into the Key textarea and click Add key.
- You now have SSH access from your local computer to your remote GitLab account.
Cloning your repo
- Sign into your GitLab account.
- Click your
hello-world
repo. - Click the Clone button in the main window.
- 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. - 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 thehello-world
repo on your computer. - Type
cd hello-world
to go into the repo folder. You execute Git commands from inside the repo folder. - 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.
- Open a terminal window and find your
hello-world
folder. Go into that folder by typingcd hello-world
. - Check the current status of the repo by typing
git status
. - Type the following command to create a sample Java program for the repo:
echo "// my first Java program" >> Hello.java
- There is now a file named
Hello.java
in thehello-world
folder. Typegit status
. Git informs you that there is a new, untracked file in the repo. - Type
git add Hello.java
. This adds the file to the staging area. Typegit status
to see the new file in the staging area. You must place files in the staging area before committing them to the repo. - 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. Typegit status
. Git informs you that you should publish the local commits to your remote GitLab repo. - Type
git push
to push your local commits to the remote repo. Typegit 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 thehello-world
repo. - 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)