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:

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:

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

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:

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

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:

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:

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:

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…”:

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:

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

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:

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:

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

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

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

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.
[…] In previous blog post, we learned how to create a git repository and commit first using Visual Studio and then achieve the equivalent of same using the native git commands. In this blog post, we’ll learn how to see the specifics of commit or how git stores this information. […]
LikeLike