If your project is stored only on your disk, then you are in for trouble when the disk fails. Even a regular backup will not always save you.
Some developers make so much mess in the project and hardly remember what and where was done.
The version control system will help you avoid these problems. If necessary, you can restore or rollback changes, view, confirm or cancel edits. Well, teamwork without a version control system is simply impossible.
I would like to present you a version control system called Git. This article discusses how to use GitHub together with Visual Studio.
You can also install the extension when installing Visual Studio:
Before continuing, you need to learn a few terms. If you know them, then scroll through them.
Push – sending changes from a local repository to a remote repository (in our case it will be located on GitHub).
Fetch – getting changes from the remote repository for comparison and possible subsequent merging.
Merge is merging. Apply changes made in another repository to the current repository. Something like combining two repositories.
Pull is a combination of fetching and merging. First, a list of changes is obtained from the remote repository, and then the changes are applied to the current repository.
That is, if someone besides you has made changes in the GitHub repository, then you can consistently perform 2 actions: Fetch and then Merge. Alternatively, you can immediately execute Pull. After that, your local repository will display the performed changes.
After installing GitHub Extension for Visual Studio, the Team Explorer panel will look like this:
If the Team Explorer panel is hidden, then it can be displayed through the “View” menu. By connecting to GitHub (clicking Connect … and entering a login with password), we will be able to either clone the GitHub repository or create a new one (Clone and Create buttons):
When cloning, a list of repositories you have access to will be displayed:
When creating the repository, you can enter its name, description and select the license, which permits the use of the code:
In this case, Git ignore contains presets for projects of various types. And this .gitignore file is intended to indicate the directories and files required to be excluded from the version control system.
In case you want to very hide the repository, then you can mark it as Private. But this requires a paid subscription.
For students, GitHub offers a special offer – Student Developer Pack, which in particular includes a free unlimited number of private repositories.
After creating the repository, you need to create a project. Personally, I prefer the opposite, first, create a project and only then add it to Git. You can also create a Git repository when creating a project. To do this, just check the box.
If you do not check the box when creating a project, but simply open the project in VS, then the “Add to Source Control” item will be available on the File menu.
After clicking it, the project will be added to the Git version control system, and a local .git folder will be created inside the project folder. In Team Explorer, this will look like this:
By switching between Team Explorer and Solution Explorer, we can make some changes in the project. After making the changes, you can commit which is a kind of recovery point. To do this, go back to Team Explorer, and click a button with a house depicted on it – it will take you to the main menu:
The “Changes” button will allow you to commit changes (it is necessary to specify a comment describing the changes). But all the actions are committed only to the git local repository.
When creating a project, a so-called “Initial commit” is sometimes created, in which you can write something like “The project was created in three days”. If you have just created a project, there are no changes in it yet. And if there are no changes, then the commit cannot be created. I added a line with a text, so in the comment, I tried to describe it briefly, but clearly:
You can view the changes you’ve made. To do this, click the file of interest to call the context menu and select “Compare with Unmodified…”.
We will get this comparison:
In this case, only 2 lines of code were added. Using the same context menu, all the changes that have been made since the last commit can be canceled. A very handy feature.
Now, let’s go back to the main menu by clicking the house. In order to send changes to GitHub, you must click the “Sync” button.
Since our project has not yet been published on GitHub, we will be asked to do this:
By the way, .git can be published not only on GitHub but also on Visual Studio Team Services.
If we published the project earlier, then our commit will be located in the list of outgoing fixes:
Clicking Push will send the changes to the repository located on the GitHub server.
After making some test changes directly through the browser in the repository located on GitHub (yes, that’s also possible), I went back into the sync and clicked Fetch:
Here you can double-click to get the information about the commit:
And by clicking the file you can see the changes:
In the same synchronization window, you can see the history:
The history can be viewed in a simple and in a detailed presentation:
Now, let’s imagine that we are working in a team and someone else has already made some changes to their local repository and sent them to GitHub. You also made changes in the same file and in the same line. In this case, when you synchronize with GitHub, you will get a conflict:
Clicking Conflicts will display the following window, where you can click a file to open the menu with the Merge button:
Now we can select the changes that we want to leave in the final version. On the following screenshot, the final version is displayed at the bottom. You can edit the code there too:
After making the change, you need to click Accept Merge (in the upper left corner), then make a commit:
The extension page on GitHub.
Github Desktop and PowerShell environment for Git
Github Desktop is a utility that is completely independent and is not associated with Visual Studio. You can download it here.
The utility is available for Mac and Windows users. It comes with the Git Shell command line. In fact, this is PowerShell with a set of scripts to integrate with Git. It’s called PowerShell environment for Git. In short, posh-git.
On the GitHub page of the posh-git project, you can find a brief instruction on how to set the posh command line for git manually.
I will discuss the interface of the utility and how to work with it. I think that it is not difficult and you can figure it out by yourself. Let’s play with the command line a little. Unlike the GUI, command line, as a rule, provides much more features. But we will consider only the basic commands.
To view the current configuration and make sure that Git is there, you can run the following command:
git config –list
In order to clone the repository you only need to execute the git clone command. For example:
git clone https://github.com/programmersommer/Barcode_Scanner_UWP.git BarcodeScanner
After executing this command, the folder with the project appears in the current directory. Besides http:// and https://, both, the SSH and git:// protocols are supported. If you switch to the project folder using the cd command (in the case of the cd BarcodeScanner example), the command line will be changed:
The PowerShell status bar will display the posh~git text, which means that you are in the PowerShell environment for Git. You can run the git status command to see if you need to synchronize a local repository. The answer can be:
The most popular commands are those that we have already considered in terms of the VS extension interface: git fetch, git merge, git push. If you go to the directory (I think the name of the PortableGit_xxx directory may be somewhat different):
you will find there a lot of executable files that emulate commands. As it was already mentioned, you can use the git help, but let’s try several commands for an example.
For example, if a new file appears in the project directory, the git status command will display:
So you need to add the file using the git add index.html command. Now you need to confirm the changes using git commit. This command will open the text editor that is installed by default. In the editor, you must enter the text describing the changes made in the first line. If you start the line with the # symbol, then this will be a comment. Comments can be left in the lines below. If you do not write any text describing the commit, then the commit will not pass. You can specify the commit text directly in the command line using the -m parameter. For example: git commit –m “File index.html added”.
Now you can use git push to send changes to the GitHub repository if this repository is yours. You can copy another repository to your computer by creating a fork/copy of the repository – Fork. Having made some changes, you can show them to the author of the original repository by creating a pull request.