How to Push Code to GitLab: A Step-by-Step Guide

GitLab is a powerful DevOps platform that supports Git repositories, CI/CD pipelines, and project management tools. If you’re new to GitLab or Git in general, pushing your local code to a GitLab repository can seem intimidating—but it’s quite straightforward.

In this guide, you’ll learn how to push your code to GitLab using Git on the command line, step by step.


🛠 Prerequisites

Before you begin, ensure the following:

  • Git is installed (git --version)
  • You have a GitLab account
  • You’ve created a new project (repository) on GitLab
  • You have access to your GitLab repository via HTTPS or SSH

🔹 Step-by-Step: Push Code to GitLab

1. Create a New GitLab Project

  1. Log in to GitLab
  2. Click the “New Project” button
  3. Choose “Create blank project”
  4. Fill in the project name, visibility, and other details
  5. Click “Create project”
    👉 Do NOT initialize the repository with a README if you’re pushing an existing project.

2. Initialize Git in Your Local Folder

Open your terminal (Git Bash, macOS Terminal, or Linux shell) and navigate to your project folder:

cd path/to/your/project
git init

3. Add and Commit Your Code

git add .
git commit -m "Initial commit"

This stages and commits your current files.


4. Link Your Local Repo to GitLab

✅ Using HTTPS (easiest method):

git remote add origin https://gitlab.com/your-username/your-repo.git

🔐 Using SSH (recommended for frequent use):

First, ensure you have an SSH key added to GitLab:

  • Generate one using ssh-keygen
  • Add the key to GitLab via User Settings > SSH Keys

Then:

git remote add origin gi*@gi****.com:your-username/your-repo.git

5. Push Code to GitLab

git branch -M main
git push -u origin main

This pushes your local main branch to the GitLab repository and sets it as the upstream.


📁 Uploading Future Changes

Once your remote is set, pushing new changes is simple:

git add .
git commit -m "Describe your change"
git push

🧠 Tips & Best Practices

  • Use meaningful commit messages.
  • Always pull the latest changes if you’re collaborating: git pull origin main
  • Use .gitignore to exclude unnecessary files from being tracked.
  • For large files, consider using Git LFS.

✅ Summary

TaskCommand Example
Initialize repogit init
Add filesgit add .
Commit changesgit commit -m "message"
Add remotegit remote add origin <repo-URL>
Push to GitLabgit push -u origin main

🚀 Final Thoughts

Pushing code to GitLab using Git ensures your project is versioned, backed up, and ready for collaboration. Once set up, your workflow becomes faster and more professional. Whether you’re working solo or with a team, mastering this process is a key step in becoming a confident developer.

Sharing Is Caring:

Leave a Comment