Integration of Git and Visual Studio – Create a repository and commit

I recently came across few blogs discussions where the question was whether you should be using some kind of UI tool for git or live with the command line git and which one is better. I won’t go much into the debate of which one is better, but I can say that you should know what you are doing. Each UI based implementation of git takes away all those commands and command line interface, it provides a nice usable interface to its users. However command line git can be very very powerful if you know what you are doing and you do need to have that kind of control sometime. So we need to understand what is happening behind-the-scenes.

There are a lot of articles about how git works as its a very popular version control system and about Visual Studio as well as its a very popular software IDE. However there are not much articles about how git is implemented in the Visual Studio or what is happening when you push/sync or do other things with source code repository in Visual Studio.

In this blog series, we’ll try to understand the same. In current blog post or first part of this series, we’ll see what requires to setup a repository in Visual Studio and making your first commit. We’ll be using git version 2.14.2.windows.2 and Visual Studio version 15.3.5 for this series. You might see a slightly different behavior with your versions but there should not be much difference.

Create a console application project in Visual Studio

Let’s first create a console application project in Visual Studio. Not much fancy but provides enough of basic platform so that we can work on our requirements. For this, go to File -> New -> Project or press Ctrl+Shift+N in Visual Studio. This should present you a screen like below:

Create a console application project
Create a console application project

Select console application project and provide a name for the project. We’ll left the checkbox for ‘Create new Git Repository’ unchecked.

Check the project directory from Git’s perspective

Let’s open navigate to the project directory in PowerShell prompt and run the Get-ChildItem -force command to get list of all files:

Initial list of files in the project
Initial list of files in the project

If we run git status command, it will show below error:

Error when checking git status command
Error when checking git status command

This seems right as we did not created a git repository while creating the project.

Create a new git repository for project

We can create repository by using a simple git init command. After this, run Get-ChildItem -force command to see files created:

List of files after issuing the git init
List of files after issuing the git init

If we need to know what files are there in .git directory, we can get them as below:

List of files after issuing the git init - 2
List of files after issuing the git init – 2

We can see that it has created all required references in place. We’ll understand the meanings of all these later in the series.

Now, if we check the status of the git repo, we can see status as below:

git repo initial status after creating repo
git repo initial status after creating repo

We can see that since its just created, it is not tracking any files. At this point, we need to tell git which files to keep track of.

If we check for the branches created with git, we can get them as below:

list of initial branches created
list of initial branches created

As expected, there are no branches created initially. If we get the contents of the .git/HEAD file, we can see that it is pointing to .git/refs/heads. However, there are no files in the directory .git/refs/heads:

contents of the heads directory
contents of the heads directory

Create a new git repo using Visual Studio

At this point, we do not need to create new git repo using Visual Studio since we have already created it using commands. However, if we need to create git repo for another project,say consoleapp02, we can create it by right click solution and select “Add solution to Source Control…”:

create git repo using visual studio
create git repo using visual studio

This will create .git folder. However, it also creates few additional files like .gitignore and .gitattributes along with branches. We can verify the same by navigating to the project directory and checking status:

checking git status and branches after adding project to source control
checking git status and branches after adding project to source control

Since there is a branch created already, if we check the status of commit, we would get below output:

git log when added project to source control
git log when added project to source control

So it has performed two commits already for us. In first commit, it added extra files. In second one, it committed project files.

We can also get commit history from within visual studio by right click solution and then select view history:

git history from within visual studio
git history from within visual studio

Do a initial commit for source code

Going back to first project, where we created git repo using command line, we still have not committed our source code files.

Before commit, you need to stage folders and files you want to commit. This is important concept of Git that you have three places for your code.

– Working directory: The place you add, modify your code.
– Staging area: You stage the files and folders you want to commit. By having stating area, you have choice which files and folder to commit. The files and folder you staged once are considered to be “tracked”.
– Local repository: The place you commit the files and folders.

We can use git add . to add all files (You can also do selective add, we are not discussing that here). Once we done that, we can again check status for git repo to see all files it is keeping track of:

check git status after adding all files for tracking
check git status after adding all files for tracking

Now, let’s commit all files by using git commit:

making first commit in git repo
making first commit in git repo

If we now check the git status and branch details, we will see below output:

checking git status and branches after commit
checking git status and branches after commit

We can also use git log to see history of commits:

git log status
git log status

In this post, we have learned what commands are performed, when you click add project to source control in Visual Studio and how we can perform the same with native commands. We’ll explore this further in upcoming blog posts.

One thought on “Integration of Git and Visual Studio – Create a repository and commit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s