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.
- Enter hello-world for the Project name.
- Set the Visibility Level to Public.
- Click the Create project button.
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.
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.pubfile (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.pubfile 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.
- Sign into your GitLab account.
- Click your
- Click the Clone button in the main window.
- Copy the Clone with SSH link to the clipboard. The link will look something like this:
firstname.lastname@example.org: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 clonewith your SSH URL:
git clone email@example.com:UserName/hello-world.git. This creates a local copy of the
hello-worldrepo on your computer.
cd hello-worldto go into the repo folder. You execute Git commands from inside the repo folder.
- You now have a local copy of your
hello-worldGit repo. You make changes in this local repo and push the changes to the remote GitLab repo when needed.
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-worldfolder. Go into that folder by typing
- Check the current status of the repo by typing
- 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
git status. Git informs you that there is a new, untracked file in the repo.
git add Hello.java. This adds the file to the staging area. Type
git statusto see the new file in the staging area. You must place files in the staging area before committing them to the repo.
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.
git pushto push your local commits to the remote repo. Type
git statusto 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
- 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 commit, and
git push like you did above. Git retains all history, including deleted files.
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.