Getting started with Git and GitLab

Last edited on 2022-09-21 Tagged under  #git   #programming   #linux 

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.

1. Setup

Install git on Debian/Mint/Ubuntu ...

$ sudo apt install git

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

$ git config --global user.name "MY USERNAME"
$ git config --global user.email "MY EMAIL ADDRESS"
$ git config --global --list

Settings are stored in ~/.gitconfig.

2. Local git repository

Example: I create a new project to hold small programs and code experiments in my home directory called homebin. I track my changes by checking my project directory into a git repository ...

$ cd homebin
$ git init --initial-branch=main

A new directory named .git is created.

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

  • .gitignore # just as it says, git will ignore changes to any files listed here
  • LICENSE.md # license used for the code formatted in Markdown
  • README.md # project description

Create, add, and commit the changes ...

$ touch .gitignore LICENSE.md README.md
$ git add .
$ git commit -m 'first commit'

Display the record of project activity ...

$ git log

3. 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

4. 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 git@gitlab.com
[...]
Welcome to GitLab, @YOUR_USERNAME!

5. 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 homebin local folder to the remote GitLab repository ...

$ cd homebin
$ git remote add origin git@gitlab.com:dwarmstrong/homebin.git
$ git remote -v
origin  git@gitlab.com:dwarmstrong/homebin.git (fetch)
origin  git@gitlab.com:dwarmstrong/homebin.git (push)
$ git push -u origin main

Good to go!

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

Thanks for reading! Read other posts?

» Next: Nostromo boot noise

« Previous: Zram swap on Debian and Linux Mint