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
gitinstalled (check withgit --version; install viasudo apt install giton Debian/Ubuntu orsudo dnf install giton 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
- Install Git if needed. Run
sudo apt update && sudo apt install git(Debian/Ubuntu) orsudo dnf install git(Fedora/RHEL). Verify withgit --version. - 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.
- 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). - 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. - Test the SSH connection (if using SSH). Run
ssh -T git@gitlab.com(replacegitlab.comwith your GitLab instance domain). You should see a welcome message. If you get a host authenticity prompt, typeyesto continue. - 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). - Clone the repository. On your Linux server, navigate to the directory where you want the project (
cd /path/to/parent) and rungit clone <URL>. For SSH, use the SSH URL; for HTTPS, use the HTTPS URL. - 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_repositoryscope, and copy it. - Verify the clone succeeded. Run
lsto see the project folder, thencd project-name && git statusto confirm you’re on the default branch and the working tree is clean. - Set your Git identity (if not already set). Run
git config --global user.name "Your Name"andgit 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 thenssh-add ~/.ssh/id_ed25519before 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 cacheto 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. Runssh -T git@gitlab.comto see detailed errors. - Host key verification failed – This happens when the server’s host key isn’t in your
known_hostsfile. Runssh-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_repositoryscope 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 pullto download large files.
Frequently Asked Questions
- 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.
- 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.
- How do I update my clone later? Run
git pullinside the repository directory to fetch and merge changes from the remote. - Can I clone a specific branch? Yes, use
git clone -b branch-name <URL>. To clone all branches, usegit clone --branch '*' <URL>(or just clone and thengit fetch --all). - 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.
- 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
- Fix Outlook Inbox Corruption with DISM in Windows
- How to Set Up Apple Pay on iPhone
- How to Choose a Refurbished iPhone With Years of Support
- Voice Dictation in Google Docs: How to Use It
- Turn Off Message Notifications on Mac
- Spotting Outside Windows: Step-by-Step Guide