How to Push a Local Repository to GitHub Using VS Code

Introduction

Git and GitHub are essential tools for developers to manage and share code efficiently. If you have a project in your local system and want to upload it to GitHub using Visual Studio Code (VS Code), this guide will help you achieve that step by step.

Prerequisites

Step 1: Open VS Code and Initialize Git

  1. Open VS Code and navigate to your project folder.
  2. Open the Terminal in VS Code:
    • Use the shortcut: Ctrl + ~ (tilde)
    • Or go to View > Terminal
  3. Initialize Git in the project directory (if not already initialized):
    git init

Step 2: Add and Commit Files

  1. Add all project files to Git for tracking:
    git add .
  2. Commit the files with a meaningful message:
    git commit -m "Initial commit"

Step 3: Connect to a GitHub Repository

  1. Go to GitHub and create a new repository.
  2. Copy the repository URL (e.g., https://github.com/yourusername/repository-name.git).
  3. Link your local repository to the GitHub repository:
    git remote add origin https://github.com/yourusername/repository-name.git

Step 4: Push to GitHub

  1. Rename the default branch to main (if required):
    git branch -M main
  2. Push the committed files to GitHub:
    git push -u origin main
  3. If main does not work, try using master instead:
    git push -u origin master

Troubleshooting

  • Git Not Recognized: If git commands don’t work, ensure Git is installed and properly configured by running:
    git --version
  • Authentication Issues: If you face authentication errors, try setting up SSH keys (Guide) or use a personal access token instead of a password.
  • Remote Repository Not Found: Double-check the GitHub repository URL.

Here's the reference video for you to watch and better understand the topic:

Conclusion

You have successfully uploaded your local project to GitHub using VS Code! Now, you can easily collaborate, track changes, and maintain version control for your project. Happy coding! 

Next Post Previous Post
No Comment
Add Comment
comment url