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.
Name it (e.g., "VSCode-Access").
Select the write_repository scope.
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.
| Command | Use Case |
git clone <url> | Use this when the project already exists on GitLab and you want a copy on your laptop. |
git init | Use this when you have a new folder on your laptop and want to start tracking it with Git. |
Step 1: Create a GitLab Project
Log in to GitLab/GitHub.
Click New Project → Create Blank Project.
Project Name:
my-first-lab.Visibility: Private (recommended for beginners).
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
| Step | GitLab | GitHub |
| Create Repo | gitlab.com → New Project | github.com → New Repository |
| Remote URL Format | https://gitlab.com/username/repo.git | https://github.com/username/repo.git |
| Personal Access Token (PAT) | Created via User Settings → Access Tokens | Created via Settings → Developer Settings → Tokens |
| Push Authentication | Use username + PAT as password | Same: username + PAT |
| Git Commands | Same: git init, git add, git commit, git push | Same: identical Git CLI workflow |
| CI/CD Integration | GitLab 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.



