Git Distributed Version Control System (DVCS) is been used in over 90% of Software Projects. With the ever growing of Visual Studio Code over the past couple of years, most of us learning VS Code may have a question, how do I use Git with Visual Studio Code?
PrerequisitesNote: We assume that you are very new to git and do not have set-up GitHub with VS Code, accordingly some steps can be skipped.
- Visual Studio Installed (either of Mac, Linux, Windows)
- Git installed on your Device.
- Should have basic understanding of Git Commands - init, config, commit, add, status, log, push e.t.c.
- A GitHub account
How to create a git local repository using VS Code
- Open VS Code,
- Open Terminal (Can use any shell zsh, bash or CMD, PowerShell)
- Now move to a location where you want to create your project folder and create your project folder, and move into that folder, and go a git init to make it as Git Project.
Example:% git init
- Now to go File -> Open Folder -> Choose the project folder you created.
✏️ Note you can even create a local git repository outside of VS Code and then import.
Setup Github email and user
- If you are using git for the first time, and have not set user and email, follow below steps,
- Open Terminal,
- Add User: git config --global user.name "your-user-name"
- Add Email: git config --global user.email "your-email@your-domain.com"
How to stage and commit code into git local repository using VS Code
- For the demo purpose, lets create a simple file hello.html and add a h1 tag saying hello,
<h1>Hello Git + VS Code!</h1>
- Execute command: git add hello.html to stage the file,
- Now, execute the command: git add -m "initial commit"
How to push local git repository using VS Code
- In Terminal, set the remove branch details by running the command: |HSgit remote set-url origin https://github.com/your-user/my-git-proj.git|
- Run command: git push origin main
- You will get a pop-up to add your GitHub credentials in VS Code.
- You will be redirected to GitHub website to provide access,
- on GitHub, create a project with the same name as your local git project (note if your repo contains sensitive info, make it a private repo)
- Now run the command git push -u origin master
% git push -u origin main
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 489 bytes | 489.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/bladerunr2049/my-git-proj.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
-
Have Questions? Post them here!
More Posts related to Git,
- How to ignore files in git using .gitignore file
- How to update expired GitHub token on local git remote config
- How to undo last Git Commit on Local Repository?
- Git Commit - Author identity unknown, Please tell me who you are email
- Git: Step-by-Step - How to Push Local Brach to GitHub
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- Remove git config at Local, Global or System Levels?
- Change the default git branch name from master to main
- fix fatal: --local can only be used inside a git repository error
- Git Remove Untracked Files using Command
- How to remove or unstage a file from git staged area
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows
- Fix [oh-my-zsh] Cant update: not a git repository
- Get List of all local branches git command
- Github: fatal: Authentication failed Support for password was removed on August 13, 2021
- [fix] fatal: this operation must be run in a work tree in git
- How to Install Git on Windows
- Perform an Empty Commit in Git without anything in Staging Area
- Rename git branch on Local and GitHub Remove using Command
- Fix: error: could not lock config file /etc/gitconfig: Permission denied
- Get the file location of git config values
- Git: Delete Branch Locally and Remotely at Origin
- fatal: Unable to create '/c/git_repo/.git/index.lock': File exists. If no other git process is currently running, this probably means a git process crashed in this repository earlier.
- Github: How to Invite Collaborators
- How to change directory in Git bash
More Posts:
- Convert JSON to Gson with type as ArrayList - Java
- How to write JSON file in Python Program - Python
- Permanently Set or Change $JAVA_HOME on Mac (macOS) - MacOS
- How to redirect SharePoint Site Collection to different URL - SharePoint
- How to know list of images available on your device - Docker
- Fix: Ubuntu (Linux) - bash: sudo: command not found error - Ubuntu
- How to take screenshot on Android - Android
- Android Parsing Data for android-L failed Unsupported major.minor version 51.0 Error - Android