Skip to main content

Command Palette

Search for a command to run...

Push Code from VS Code to GitHub/GitLab

Updated
3 min readView as Markdown
Push Code from VS Code to GitHub/GitLab

Prerequisites

Before we begin, ensure you have:

  • Git Installed: Download here if you haven't.

  • GitLab/GitHub Account: Sign up at GitLab.com or GitHub.com

  • Personal Access Token (PAT): 1. Go to User Settings → Access Tokens.

    1. Name it (e.g., "VSCode-Access").

    2. Select the write_repository scope.

    3. Copy the token immediately—you won't see it again!


Which Command Should I Use?

Choosing the right starting point is the most common hurdle for beginners.

CommandUse Case
git clone <url>Use this when the project already exists on GitLab and you want a copy on your laptop.
git initUse this when you have a new folder on your laptop and want to start tracking it with Git.

Step 1: Create a GitLab Project

  1. Log in to GitLab/GitHub.

  2. Click New Project → Create Blank Project.

  3. Project Name: my-first-lab.

  4. Visibility: Private (recommended for beginners).

  5. Click Create project.


Step 2: Connect Your Local Folder to GitLab

Open VS Code, open your project folder, and open the terminal (Ctrl + ~).

Run these commands in order:

# 1. Initialize the local repository
git init

# 2. Configure your identity (Only needed once per PC)
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

# 3. Connect your local folder to your GitLab project
git remote add origin https://gitlab.com/your-username/my-first-lab.git

# 4. Stage and commit your files
git add .
git commit -m "Initial commit: Adding my first files"

# 5. Push to GitLab
git push -u origin main

Step 3: Authentication (The "Password" Trick)

When you run git push, a popup or terminal prompt will appear. Important: Your standard GitLab password will not work here if you have 2FA enabled or for security reasons.

  • Username: Your GitLab username (e.g., bilal-amjad).

  • Password: Paste your Personal Access Token (PAT) that you created in the prerequisites.


💡 Best Practice: Using .gitignore

You don't want to push "junk" files or sensitive secrets to GitLab. Create a file named .gitignore in your root folder and add these lines:

# Ignore Terraform state files (Security Best Practice)
.terraform/
*.tfstate
*.tfstate.backup

# Ignore OS-specific files
.DS_Store
Thumbs.db

Summary of the "Big Three" Commands

  • git add . → "Prepare these files for a snapshot."

  • git commit -m "message" → "Take the snapshot and give it a name."

  • git push → "Send my local snapshots to the GitLab cloud."

🔄 GitLab vs GitHub: Push Workflow Comparison

StepGitLabGitHub
Create Repogitlab.com → New Projectgithub.com → New Repository
Remote URL Formathttps://gitlab.com/username/repo.githttps://github.com/username/repo.git
Personal Access Token (PAT)Created via User Settings → Access TokensCreated via Settings → Developer Settings → Tokens
Push AuthenticationUse username + PAT as passwordSame: username + PAT
Git CommandsSame: git init, git add, git commit, git pushSame: identical Git CLI workflow
CI/CD IntegrationGitLab has built-in CI/CD (.gitlab-ci.yml)GitHub uses GitHub Actions (.github/workflows)

If you are facing error like this:

If you're encountering a 403 error, try checking your credentials by navigating to Control Panel →

Credential Manager → Windows Credentials.