Skip to main content

Git GitLab Integration

Introduction

GitLab is a complete DevOps platform that builds upon Git's version control capabilities. While Git provides the fundamental version control system for tracking changes in your codebase, GitLab extends these capabilities with a web-based interface and additional collaborative features like issue tracking, CI/CD pipelines, and project management tools.

This guide will walk you through the process of integrating Git with GitLab, from setting up your repository to collaborating with team members using GitLab's powerful features.

Prerequisites

Before we begin, ensure you have:

  • Git installed on your local machine
  • A GitLab account (either on gitlab.com or a self-hosted instance)
  • Basic understanding of Git commands (commit, push, pull)

Setting Up GitLab Integration

Creating a New Project on GitLab

  1. Log in to your GitLab account
  2. Click on the "New project" button on the dashboard
  3. Choose "Create blank project"
  4. Provide a project name, description, and visibility level (public, internal, or private)
  5. Click "Create project"

Once created, GitLab will display instructions for pushing an existing repository or creating a new one.

Connecting an Existing Local Git Repository to GitLab

If you already have a Git repository on your local machine, you can connect it to GitLab using these commands:

bash
# Navigate to your local repository
cd your-project-directory

# Add GitLab as a remote
git remote add origin https://gitlab.com/username/repository-name.git

# Push your code to GitLab
git push -u origin main

The -u flag sets up tracking, so in the future, you can simply use git push without specifying the remote and branch.

Cloning a GitLab Repository

To start working with an existing GitLab repository:

bash
# Clone the repository to your local machine
git clone https://gitlab.com/username/repository-name.git

# Navigate into the project directory
cd repository-name

Authentication Methods

HTTPS vs SSH

GitLab supports both HTTPS and SSH for repository access:

  • HTTPS: Easier to set up but requires entering your username and password (or personal access token) for each interaction
  • SSH: Requires initial key setup but allows for secure password-less authentication

Setting Up SSH Authentication

  1. Generate an SSH key pair if you don't have one:
bash
ssh-keygen -t ed25519 -C "your_email@example.com"
  1. Copy your public key:
bash
# On macOS
pbcopy < ~/.ssh/id_ed25519.pub

# On Linux
cat ~/.ssh/id_ed25519.pub

# On Windows (Git Bash)
cat ~/.ssh/id_ed25519.pub | clip
  1. Add the key to your GitLab account:

    • Go to your profile settings
    • Select "SSH Keys"
    • Paste your public key in the "Key" field
    • Add a title and click "Add key"
  2. Test the connection:

bash
ssh -T git@gitlab.com

Using Personal Access Tokens

For HTTPS authentication, personal access tokens are more secure than passwords:

  1. In GitLab, go to your profile settings
  2. Select "Access Tokens"
  3. Create a new token with appropriate scopes (at minimum, "read_repository" and "write_repository")
  4. Use this token instead of your password when pushing or pulling
bash
git clone https://oauth2:YOUR_ACCESS_TOKEN@gitlab.com/username/repository-name.git

Basic Workflow with GitLab

The GitLab Flow

GitLab Flow is a simplified Git workflow that combines feature branches with an issue tracking system:

Creating a Feature Branch

Always create a branch for your changes:

bash
# Create and switch to a new branch
git checkout -b feature/amazing-feature

# Make changes to your code
# ...

# Stage and commit your changes
git add .
git commit -m "Add amazing feature"

# Push the branch to GitLab
git push -u origin feature/amazing-feature

Creating a Merge Request

Once your feature is ready:

  1. Go to your repository on GitLab
  2. Navigate to "Merge Requests" and click "New merge request"
  3. Select your feature branch as the source and main branch as the target
  4. Add a title, description, and assign reviewers
  5. Click "Create merge request"

Reviewing and Merging

Reviewers can:

  • Comment on specific lines of code
  • Start a discussion
  • Suggest changes
  • Approve the merge request

Once approved, you can merge the changes:

  1. Click "Merge" on the merge request page
  2. Choose whether to delete the source branch after merging
  3. The changes will be incorporated into the main branch

Advanced GitLab Features

CI/CD Integration

GitLab CI/CD allows you to automate your build, test, and deployment processes:

  1. Create a .gitlab-ci.yml file in your repository root:
yaml
stages:
- build
- test
- deploy

build-job:
stage: build
script:
- echo "Building the application..."
- npm install

test-job:
stage: test
script:
- echo "Running tests..."
- npm test

deploy-job:
stage: deploy
script:
- echo "Deploying application..."
only:
- main
  1. Push this file to your repository, and GitLab will automatically start running your CI/CD pipeline

Issue Tracking and Project Management

GitLab provides robust issue tracking:

  1. Create issues to track bugs, features, or tasks
  2. Assign issues to team members
  3. Link issues to merge requests
  4. Organize with labels and milestones

To reference an issue in a commit message:

bash
git commit -m "Fix login bug, closes #42"

This will automatically close issue #42 when the commit is merged into the main branch.

GitLab Wiki

Each GitLab project includes a wiki for documentation:

  1. Navigate to your project
  2. Click on "Wiki" in the sidebar
  3. Create pages using Markdown
  4. Link pages together to create comprehensive documentation

Troubleshooting Common Issues

Authentication Failed

If you encounter authentication issues:

bash
# Check your remote URL
git remote -v

# Update to use the correct URL
git remote set-url origin https://gitlab.com/username/repository-name.git
# or
git remote set-url origin git@gitlab.com:username/repository-name.git

Merge Conflicts

When GitLab indicates a merge conflict:

bash
# Pull the target branch
git checkout main
git pull

# Switch to your feature branch
git checkout feature/amazing-feature

# Merge main into your feature branch
git merge main

# Resolve conflicts and commit
git add .
git commit -m "Resolve merge conflicts"

# Push updated branch
git push

Summary

Integrating Git with GitLab enhances your development workflow by providing a centralized platform for collaboration. Key benefits include:

  • Web-based repository management
  • Streamlined code review process with merge requests
  • Integrated CI/CD pipelines
  • Issue tracking and project management
  • Enhanced security with multiple authentication options

By combining Git's powerful version control with GitLab's collaborative features, you create a robust environment for efficient software development.

Additional Resources

Exercises

  1. Create a new project on GitLab and push an existing local repository to it.
  2. Set up SSH authentication for your GitLab account.
  3. Create a feature branch, make changes, and create a merge request.
  4. Set up a simple CI/CD pipeline using .gitlab-ci.yml.
  5. Create an issue, reference it in a commit message, and observe how it's automatically closed when merged.


If you spot any mistakes on this website, please let me know at feedback@compilenrun.com. I’d greatly appreciate your feedback! :)