How to Use Git and GitHub with Visual Studio Code
Learn to manage your code and share it online using Git, GitHub, and VS Code. Think of Git as your project’s smart history tracker, and GitHub as its online home for backup and collaboration.
What You’ll Need
- GitHub Account: Sign up for free at github.com.
- Visual Studio Code (VS Code): Download from code.visualstudio.com.
- Git: Install it for your OS from git-scm.com.
The Process: From Local Code to GitHub
1. Tell Git Who You Are
Git needs to know your identity for each change you make. Open your Terminal (or Git Bash on Windows) and run the following commands, replacing the placeholders with your actual name and email:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
2. Create Your Project’s Home on GitHub
This is where your code will live online.
- Go to github.com and log in.
- Click the
+icon in the top right corner of the page, then select “New repository.” - Repository Name: Enter
my-project(or a descriptive name for your project). - Description: Provide a brief summary of your project (optional, but recommended).
- Visibility: Choose Public (anyone can see your code) or Private (only you and those you invite can see it).
- Initialize this repository with a README: Check this box. A README file is crucial for explaining your project to others (and your future self!).
- Add a .gitignore: If your project is in Node.js or JavaScript, select “Node” from the dropdown. This file tells Git to ignore specific files (like temporary files or sensitive API keys) so they aren’t accidentally committed.
- Click the green “Create repository” button.
Keep this GitHub page open; you’ll need its URL shortly.
3. Get Your Project Ready in VS Code
- For an Existing Project: Open VS Code, navigate to
File>Open Folder..., and select your project’s directory. - For a New Project: Create a new folder on your computer for your project. Then, open VS Code and go to
File>Open Folder...and select your newly created folder.
4. Initialize Git Locally in VS Code
Tell Git to start tracking changes within your project folder.
- Open the Source Control view in VS Code. You can find it by clicking the icon that looks like three connected dots on the left sidebar.
- If you see a button labeled “Initialize Repository,” click it. This is VS Code’s way of running the
git initcommand in your project folder, making it a Git repository.
5. Stage and Commit Your Files (Using Terminal Commands)
Saving a “snapshot” of your project’s current state is called a commit. Before you can commit, you need to “stage” the files you want to include in that snapshot.
-
What are Staging and Committing?
- Staging (
git add .): Think of this as selecting which changes you want to group together for your next save point.git add .is a shortcut to stage all modified or new files in your current directory and its subdirectories. - Committing (
git commit -m "Your Message"): This is the act of saving the staged changes as a distinct version in your project’s history. Each commit has a unique identifier and a descriptive message explaining what changed.
- Staging (
-
How to do it in VS Code:
- Open VS Code’s Integrated Terminal by pressing
Ctrl +` (that’s Ctrl and the backtick key). - Stage all changes: Type the following command and press Enter:
This tells Git to prepare all the current changes for the next commit.git add . - Commit your staged changes: Type the following command, replacing
"Your descriptive message"with a clear explanation of what you’ve done (e.g., “Initial commit: Add project structure and config”), and press Enter:
You’ve now created a save point with your changes and a description.git commit -m "Your descriptive message"
- Open VS Code’s Integrated Terminal by pressing
6. Link VS Code to Your GitHub Repository and Push Your Changes
Now, you’ll connect your local project to its online home on GitHub and upload your commits.
-
What is
git push?- Pushing (
git push origin <branch-name>): This command uploads your local commits to a remote repository. When you first set up, you usually push to a remote namedoriginand a branch calledmainormaster.
- Pushing (
-
How to do it in VS Code:
- Copy your GitHub Repository URL: Go back to your GitHub repository page. Click the green “Code” button, and copy the HTTPS URL provided (it will look something like
https://github.com/your-username/my-project.git). - Add the Remote in VS Code (if you haven’t already):
- In the Integrated Terminal (remember,
Ctrl +), paste the following command, replacing<YOUR_GITHUB_REPO_URL>` with the URL you just copied:git remote add origin <YOUR_GITHUB_REPO_URL> - Press Enter. This command tells your local Git repository where its remote counterpart (on GitHub) is located, usually named
origin.
- In the Integrated Terminal (remember,
- Push your committed changes to GitHub:
- In the Integrated Terminal, type the following command and press Enter:
(Note: If your default branch is namedgit push origin mainmasterinstead ofmain, usegit push origin master.) - VS Code might prompt you to log in to your GitHub account if you haven’t already. Follow the on-screen instructions.
This command uploads the commits you made locally to your GitHub repository.
- In the Integrated Terminal, type the following command and press Enter:
- Copy your GitHub Repository URL: Go back to your GitHub repository page. Click the green “Code” button, and copy the HTTPS URL provided (it will look something like
7. Verify on GitHub
Refresh your GitHub repository page. You should now see your project files, the README, and the commit message you created!
What’s Next?
You’ve successfully set up Git, made your first commit using terminal commands, and pushed your code to GitHub using VS Code! This forms the fundamental workflow for saving your progress, tracking changes, and collaborating with others. Practice these steps with every new feature or fix you implement.