The Official DevClouds Blogs

logo-devclouds

Using Git with Visual Studio Code

January 16, 2023

In this article we will learn using git with visual studio code. Source control, version control, backing up whatever you choose to call it. It’s all about keeping track of code and files overtime.

Git is a program for managing source code, while GitHub hosts a copy of that code and repository online. Now, Visual Studio code makes the process of using Git and GitHub easy, and in this article I’ll show you the steps involved.

git

But before we get started, do note that this article assumes you have Git installed, which you can find on this site here.

Initialize repository

With that out the way the first step to use Visual Studio code with Git is to initialize a folder to reflect your repository.

file manager

So, let me open up an existing folder with some files in it.

Initialize Repository

And then go to source control and click initialize repository.

branch name

Rename branch

Now notice at the bottom left hand corner that the branch is named Main. But if you needed to rename it, you can simply open the command palette: Shift+Command+P

Rename Branch

And type rename and let’s say we want to call it test branch and just hit enter.

And now you’ll see it reflected below. But I’m going to rename it back just as easily to main.

source control panel

Now after initialization, you’ll see that in the source control panel that my new file shows up with the letter ‘U’ beside it and ‘U’ stands for untracked file. Meaning a file that is new or changed but has not been added to the repository. So to track it I can just click the + sign next to the index .html.

Committing files

Staged Changes

and now it is what’s referred to as stage. Now once added the letter next to the file will change to “A” which represents a new file has been added to the repository.

And the next step would be too committed and all I need to do is add a message such as first commit.

And in general it will just describe whatever changes that you make. It’s for informational purposes and to submit it I’ll just hit the check mark right above it, or right now we’re on our main branch.

Create new branch

Create Branch

but if this is a real application and we need to add new code, new features. For example, we want to create a separate branch to do that by going to the command Palette, Shift command, P.

And I already have the option here to create branch but you could also just start typing in create branch.

I’ll call this new features. And the bottom left hand corner reflects that now.

New two lines code

And I’ll add 2 lines of code that reflects my new features. One for warning and one for danger.

Gutter overview

Now you’ll notice when I add the code on the left hand side, there’s this area called ‘gutter’ for new lines of data, it’s reflected by a green line.

Changes in script

For change lines of data. It’s reflected by a shaded blue line, so let’s say if I added exclamation points on line 4, you’ll see a shaded blue line. And if I deleted a line like line 10, it’ll be reflected by a red arrow, but I’m going to put that line back.

file name options

Now, right under the word changes, you’ll see I have some icons here to open the file. Discard changes that I’ve made. I can stage my changes and this is all for one file But if I had multiple files then I can go ahead and discard all my changes or stage all my changes.

side by side

Comparing files / inline view

Now Visual Studio code also has the ability to perform a diff on files to compare files side-by-side. Typically you would need to have a separate tool to do this. But it’s built in and to see our changes side by side we just need to click on our file.

inline view option

But this time I also want to show you that. you can see all the changes in one file so if we go to the upper right hand corner here and choose inline view. It consolidates them.

inline view

And within here we can also make changes so I can add some exclamation points here if I wanted to.

Added Features

Now if I want to stage my changes I can come up here and click the + and add a message called added features and commit it. Remember now at the bottom left hand corner it still says new features and that’s the only branch that’s affected.

going back to Main Branch

So I can click here and switch to main branch. And this is how the code was originally. Now, if I want the new code to be reflected in the main branch.

Merging branches

Merge Branch

I need to merge by going to these three dots here, going to Branch the Merge Branch.

New Features Branch

It’ll ask me from where and I want to select from our new features branch and now our main branch has the reflected changes.

Publish to Github

Publish Branch Button

So now let’s say we actually want to publish this to GitHub. All we need to do is go to publish branch.

GitHub Prompt

It’s going to prompt us that this extension GitHub wants to sign in using GitHub. I’ll hit allow and then an authentication process will start. I’ll hit continue at this prompt. And over here I’ll give permission for GitHub to open up Visual Studio Code. That’ll ask me for permission for this extension to open the URL. I’ll click open.

Publish to Public Repository

And now I can select whether I want a public or private repository. We’ll go ahead and choose public.

Publishing Process

And it begins the publishing process to GitHub and then prompts me to open up and there we go.

github file

I could see the file that we’ve been working on.

Clone repository

Now let’s say I wanted to clone a repository and I want it to clone my repository.

github clone url

All I need to do is click on code. Copy the URL.

git clone

Open up the command Palette, Shift+Command+P and enter in clone, select Git clone.

paste git clone url

I’ll paste the URL and clone from this URL and now all I need to do is specify where I wanted clone.

Clone location in file manager

And I’ll select this location here, along with my other projects as the repository location and when it asked me would I like to open the clone repository, I’ll say yes and it’s as simple as that and that’s it.

This is really the most common ways developers use Git. Creating a repository, branch staging, committing changes, pushing code UI, merging branches, cloning existing projects and yes, there are some more involved topics.

But what we covered or what you’ll likely be doing most frequently. But to learn more about using Visual

Studio Code with Git along with other source control topics, check out blog link.

Share This Article

Join our newsletter & get update, It’s That Simple!

Join, Blogs, Discount & Promo code !!!

THERE’S MORE TO READ.

What tools you need to learn to become a devops engineer and what are the tasks and responsibilities of a devops engineer.

| January 17, 2023

kubernetes is an open source container orchestration framework. Which was originally developed by google.

| January 17, 2023

Terraform allows you to automate and manage, your infrastructure, your platform and services that run on that infrastructure.

| January 16, 2023

Whenever there is a change in git repository. You want Jenkins to be triggered and changes builds.

| January 16, 2023

what is git exactly it's a version control system but really what that is a system for managing your files.

| January 16, 2023

In order to build the application in the package you need some kind of build tools.

| January 15, 2023

Leave a Reply

Your email address will not be published. Required fields are marked *

Cyber​​Power Cloud, fast, protected virtual servers, instant setup.

Spin up your choice of virtual machine in just 55 seconds.

OVH

Cloud VPS & Dedicated Servers, advanced hosting solutions

Amazon Lightsail provides easy-to-use cloud resources for web applications

Managed Google Cloud Virtual Machines

Easily deploy cloud servers and storage worldwide

Cloud VPS & Dedicated Servers, advanced hosting solutions

Cloud optimized for performance equipped with fast NVMe drives.

AWS

Amazon EC2, the broadest and deepest compute platform

Managing complex cloud infrastructure made easy

High-Performance Cloud Storage Solutions

10 Clouds 1 Panel

Fully managed by our team

All your clouds and dedi's

All your control panels (CP,DA)

All your wordpress sites&plugins

All of that in one CyberPower

Mega control panel

Reliable Co-Location in ISRAEL Service

Buy and add server hardware components to your co-located servers.

Attacked? We Can Defend You!

Advanced DevOps Services

All about us and what we do in cyberpower

Contact us to activate your custom affiliate plan

Terms & Conditions, Privecy Policy