sodcalculator

10 Tech How-To

Clone a Private GitLab Repo on Linux

Guide 10-20838 · Updated · Step by step

This guide walks you through cloning a private GitLab repository onto a Linux server, covering SSH key setup, HTTPS authentication, and common pitfalls. You’ll need a GitLab account with access to the target repository and a Linux server with Git installed.

What You’ll Need

  • A Linux server with git installed (check with git --version; install via sudo apt install git on Debian/Ubuntu or sudo dnf install git on Fedora/RHEL).
  • Your GitLab username and either an SSH key pair or a personal access token (PAT) for HTTPS.
  • Access to the repository: you must be a member of the project or group in GitLab.
  • The repository URL (SSH or HTTPS) from GitLab’s “Clone” button.

Step-by-Step

  1. Install Git if needed. Run sudo apt update && sudo apt install git (Debian/Ubuntu) or sudo dnf install git (Fedora/RHEL). Verify with git --version.
  2. Choose your authentication method. SSH is recommended for convenience and security. HTTPS works with a personal access token. If you already have an SSH key you want to use, skip to step 4.
  3. Generate an SSH key pair (if using SSH). Run ssh-keygen -t ed25519 -C "your_email@example.com". Press Enter to accept the default file location (~/.ssh/id_ed25519) and optionally set a passphrase. This creates a private key (id_ed25519) and a public key (id_ed25519.pub).
  4. Add the public key to GitLab. Display it with cat ~/.ssh/id_ed25519.pub, copy the output, then in GitLab go to Preferences → SSH Keys, paste the key, give it a title, and click Add key.
  5. Test the SSH connection (if using SSH). Run ssh -T git@gitlab.com (replace gitlab.com with your GitLab instance domain). You should see a welcome message. If you get a host authenticity prompt, type yes to continue.
  6. Copy the repository URL. In GitLab, open the project page, click the blue Clone button, and copy either the Clone with SSH (e.g., git@gitlab.com:username/project.git) or Clone with HTTPS URL (e.g., https://gitlab.com/username/project.git).
  7. Clone the repository. On your Linux server, navigate to the directory where you want the project (cd /path/to/parent) and run git clone <URL>. For SSH, use the SSH URL; for HTTPS, use the HTTPS URL.
  8. Authenticate if using HTTPS. When prompted for username, enter your GitLab username. For password, enter your personal access token (not your account password). If you haven’t created a token, go to GitLab → Preferences → Access Tokens, create one with read_repository scope, and copy it.
  9. Verify the clone succeeded. Run ls to see the project folder, then cd project-name && git status to confirm you’re on the default branch and the working tree is clean.
  10. Set your Git identity (if not already set). Run git config --global user.name "Your Name" and git config --global user.email "your_email@example.com" so your commits are attributed correctly.

Common Mistakes

  • Using the wrong URL. Copying the HTTPS URL when you intended SSH, or vice versa, leads to authentication failures.
  • Entering your GitLab password for HTTPS. GitLab requires a personal access token for HTTPS authentication. Use the token as the password.
  • Not adding the SSH key to GitLab. Even with a valid key on your server, GitLab won’t recognize it unless the public key is added to your account.
  • Forgetting to start the SSH agent. If you used a passphrase, you may need to run eval "$(ssh-agent -s)" and then ssh-add ~/.ssh/id_ed25519 before cloning.
  • Cloning into a non-empty directory. Git will refuse to clone into a folder that already contains files. Choose a fresh directory or remove the existing content first.

Tips

  • Use SSH keys without a passphrase for automated scripts or CI/CD, but keep the private key secure.
  • Cache your HTTPS credentials with git config --global credential.helper cache to avoid re-entering the token frequently.
  • Use a shallow clone (git clone --depth 1) if you only need the latest snapshot and want to save bandwidth and time.
  • Keep the repository path short to avoid issues with deeply nested paths on some filesystems.
  • If you’re behind a firewall, ensure port 22 (SSH) or 443 (HTTPS) is open to your GitLab instance.

Troubleshooting

  • Permission denied (publickey) – This means SSH couldn’t authenticate. Check that your public key is added to GitLab, that the private key exists at ~/.ssh/id_ed25519, and that you’re using the correct SSH URL. Run ssh -T git@gitlab.com to see detailed errors.
  • Host key verification failed – This happens when the server’s host key isn’t in your known_hosts file. Run ssh-keyscan gitlab.com >> ~/.ssh/known_hosts (replace domain) or manually accept the prompt.
  • HTTP Basic: Access denied – For HTTPS, you likely used your account password instead of a personal access token. Create a token with read_repository scope and use it as the password.
  • Repository not found – The URL might be wrong, or you don’t have access to the project. Verify the URL and check with the project owner that your GitLab account is added as a member.
  • SSL certificate problem – If your GitLab uses a self-signed certificate, you may need to configure Git to trust it. This is advanced; ask your admin for the correct CA certificate.
  • Clone succeeded but files missing – Check if the repository uses Git LFS. If so, run git lfs pull to download large files.

Frequently Asked Questions

  1. Can I clone a private repo without SSH or a token? No. GitLab requires authentication for private repositories. SSH keys or personal access tokens are the standard methods.
  2. What if I don’t have a GitLab account? You need an account and must be added as a member of the project. Contact the project owner to request access.
  3. How do I update my clone later? Run git pull inside the repository directory to fetch and merge changes from the remote.
  4. Can I clone a specific branch? Yes, use git clone -b branch-name <URL>. To clone all branches, use git clone --branch '*' <URL> (or just clone and then git fetch --all).
  5. What is the difference between SSH and HTTPS? SSH uses key-based authentication and doesn’t require entering credentials each time. HTTPS uses a token and is often easier to set up behind corporate proxies.
  6. How do I change the remote URL if I cloned with the wrong method? Run git remote set-url origin <new-URL> to switch between SSH and HTTPS.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →