Getting started with Git and GitLab

Last updated on 2021-01-16

I’m using Git to take a series of snapshots of my program files - stored in a project directory configured as a git repository - as things change over time. Git allows me to add or roll back any of those changes as desired. GitLab, then, provides a free service for web-hosting git repositories that can be configured for public or private access.

Let’s go!

Install git on a Debian/Ubuntu-based Linux system by running …

$ sudo apt install git
$ git --version
git version 2.25.1

Configure a username and email address. Every time git commit ... takes a snapshot it adds this information to the commit …

$ git config --global "MY USERNAME"
$ git config --global "MY EMAIL ADDRESS"
$ git config --global --list

Settings are stored in ~/.gitconfig.

Local git repository

This month I’m exploring a bit of Python programming and I created a new project called bootsound. I track my changes by making my project directory into a git repository …

$ cd bootsound
$ git init

… which creates a new .git directory.

I start by creating 3 placeholder files that commonly exist in project directories tracked by git:

Create, add, and commit the changes …

$ touch .gitignore
$ git add .
$ git commit -m 'first commit'
$ git log

SSH keys

I use SSH to move data between local and (soon to be hosted on GitLab) git repositories. I configure no-password-at-login authentication to the remote repository by using SSH keys.

Check for existing keys …

$ ls -al ~/.ssh/*pub

Generate new keys and assign a key passphrase …

$ ssh-keygen -t ed25519 -C "$(whoami)@$(hostname)-$(date -I)"

Restrict access to ~/.ssh

$ chmod 700 ~/.ssh

See also: Secure remote access using SSH keys

GitLab account

Free sign-up. Just setup the user - step 1 - for now. Groups and git projects can be created as desired.

Add the public *.pub SSH key to the newly-created account. On the GitLab namespace page, click on the avatar in the upper-right corner and select Settings->SSH Keys. Copy/paste the string of characters in the public SSH keyfile and click Add key.

Test …

$ ssh -T
Welcome to GitLab, @YOUR_USERNAME!

GitLab project

Click the plus icon in the navigation bar and select New Project and Create blank project. Add details and click to create.

Now I can push (transfer) the contents of my existing bootsound local folder to the remote GitLab repository …

$ cd bootsound
$ git remote add origin
$ git remote -v
origin (fetch)
origin (push)
$ git push -u origin master

Good to go! GitLab basics guides and the Pro Git book are helpful resources to learn more.

Happy hacking!