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
- Git installed on your system (Download Git)
- A GitHub account (Sign up for GitHub)
- VS Code installed (Download VS Code)
Step 1: Open VS Code and Initialize Git
- Open VS Code and navigate to your project folder.
- Open the Terminal in VS Code:
- Use the shortcut:
Ctrl + ~
(tilde) - Or go to View > Terminal
- Use the shortcut:
- Initialize Git in the project directory (if not already initialized):
git init
Step 2: Add and Commit Files
- Add all project files to Git for tracking:
git add .
- Commit the files with a meaningful message:
git commit -m "Initial commit"
Step 3: Connect to a GitHub Repository
- Go to GitHub and create a new repository.
- Copy the repository URL (e.g.,
https://github.com/yourusername/repository-name.git
). - Link your local repository to the GitHub repository:
git remote add origin https://github.com/yourusername/repository-name.git
Step 4: Push to GitHub
- Rename the default branch to main (if required):
git branch -M main
- Push the committed files to GitHub:
git push -u origin main
- If
main
does not work, try usingmaster
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!