Get Started with GIT Repository and VSTS
This article cover steps required for using GIT Repository with Visual Studio Team Services (VSTS)
First question, what is the GIT: GIT is a distributed version control system, which means your local copy of code is a complete version control repository, and is quickly becoming the standard for version control. These fully functional local repositories make it easy to work remotely or offline. You commit your work locally, and then sync your copy of repository with the copy in the server. Every time you edit and save your work, GIT creates a commit which is a snapshot of all your files at a point in time. Commits create links to other commits which forms a graph of your development history. In GIT, commits are identified by a unique cryptographic hash of the contents of commit. Now because everything is hashed, it is impossible to make changes, lose information or corrupt files without GIT detecting it.
Second Question, what is a version control: They are the software that helps you track changes you make in your code over the time. As you make any change in your code, you tell the version control system to take the snapshot of your files. The version control system save that snapshot files permanently, so you can rollback it later if needed. Use GIT version control to save your work and coordinate code changes across your team. Even if you’re a single developer, version control helps you stay organized as you can fix bugs and develop new features. Version control also keeps a history of your development work so that you can review and even rollback to any previous version of your code with a simple process.
VSTS IDE integrations are available for Visual Studio, Visual Studio Code, Eclipse and Intellij. Here I am going to use Visual Studio for integration.
In this article I will cover these steps:
- Creating a GIT Repository in your local and Syncing to VSTS using Visual Studio Command Line.
- Cloning an already existing GIT Repository to your local Visual Studio.
- 5 steps of GIT in Visual Studio for Staging, Syncing and Committing your code
Creating a GIT Repository in your local and Syncing to VSTS using Visual Studio Command Line
If you don’t have your code in VSTS Git repository then you need to use Visual Studio Command Prompt for sharing your local code into VSTS Git. However, you would need to have a VSTS account for these services. Visit here for creating new account https://www.visualstudio.com/ and follow these steps for creating local repo:
1. Open VS Command Prompt and go to where your project’s solution file is.
2. Execute “git init” to create an empty repository.
3. Execute “git config --global user.name “your name”” For example git config --global user.name “John Doe”
4. Execute “git config --global user.email “your email”” For example git config --global user.email John.Doe@Developers.com
5. Create .gitignore file (search github for VisualStudio.gitignore, select it, go to Raw and copy and paste into Notepad) Save the file as x.gitignore (make sure to not be saving as .txt).
6. Rename the x.gitignore file to .gitignore (you have to do this as Windows will throw up an error naming a file with only an extension)
7. Execute “git commit -m “git ignore”” This will commit the .gitignore file to your local repository.
8. Execute “git add *” This will add all the files in your solution to the local repo but will ignore the files specified in .gitignore (the Bin folder as a rule).
9. Execute “git commit -m “Initial Commit”” This will commit the added files to your local repo
10. Open the solution in Visual Studio 2017 and go to the Team Explore tab (in the Solution Explorer window)
11. Click on the Manage Connections toolbar button (shown below) and make sure the local repo is connected to the proper VSTS project—sometimes this has issues and you need to verify it is connected.
12. Once you have verified the connection to the VSTS project and repo, click the Home button and then Sync (shown below)
13. After clicking the Sync button you will be able to PUSH the Outgoing Commits to the VSTS Repo by clicking the Push link as shown below (the example has no outgoing commits, but if you have been following these steps you will have commits and Push will be available for you to click):
When done and you are informed that all outgoing commits are sync’d you can go to VSTS and select your project and then the Code option to see the sync’d sources as shown below:
Note that the \Bin folder is not included since we excluded this in the .gitignore file in the repo.
Complete local copy of an existing GIT repository by cloning it. Cloning a repo means download all commits and branches in the repo and sets up a named relationship with the existing repo you cloned. Use this relationship to interact with the existing repo, pushing and pulling changes to share code with your team. There are two ways of cloning a repo using Visual Studio or Command Line. For cloning it via command line you would need a clone URL which you will get in web portal. You can check these options when you’re viewing your repo from the CODE tab in the interface, select CLONE in upper right.
Cloning via Command line
1. You should have GIT command line package for your platform.
2. You would need the right GIT credential manager.
3. Now pass this Clone URL to “git clone” to make a local copy of repo like: “git clone https://opsarc.visualstudio.com/_git/Bienvenue”. This will clones the repository from the URL in a folder under the current one. However you can create a repo at specific location also by passing a folder name after the URL like: “git clone https://opsarc.visualstudio.com/_git/Bienvenue C:\Repos\MyProjects”.
Cloning via Visual Studio (I prefer this approach)
1. Login to VSTS and select the proper project—like “https://opsarc.visualstudio.com” (that is my local URL) and login with your Microsoft ID.
3. Once you have selected the correct project, click the Code menu option from the menu bar (as shown below) which will take you to the VSTS Repo.
4. From the project repo screen simply select Clone button (in the upper right-hand corner) as shown below:
6. You will be prompted by your browser if you want to switch apps to Visual Studio (you do) and then you can specify where you want to clone the repo locally as shown in the screen below:
7. Click Connect and the repo in VSTS will be cloned to your local system. When done, browse to the local path and open the solution file and you are good to go—completely working locally on your own copy of the repo.
NOTE: You can change local path for your local repos here.
5 Steps of GIT in Visual Studio
Files in the GIT are in three states: modified, staged or commited. When you first modify a file, the changes exist only in your working directory. They are yet not a part of commit or your development history. You must stage the changed files you want to include in your commit. Now the staging area contain all the changes that you will include in your next commit. Once you are done with staging, then you must commit them with a message which will describe what you have changed in this commit. Now this commit will become a part of your development history. Below is a pointer description for all steps to follow.
Git usage consists 5 simple steps that you will use over and over once you have cloned a repo from VSTS to your local system. These steps are:
· Stage-stages your changes to your local repo.
· Commit-commits the changes to your local repo. You always need to commit any local changes before instituting a Pull.
· Push—pushes your changes from your local repo to the server repository.
· Fetch-looks for any changes in the server repo but does NOT retrieve those changes. You get a list of changes and files that have changed only.
· Pull-pulls any changes from the server repo to your local repo. Will not work and error out if you have any uncommitted changes in your local repo.
· Pull-pulls any changes from the server repo to your local repo. Will not work and error out if you have any uncommitted changes in your local repo.
A few things to note about the screenshot above:
· You can see from the padlock next to the files and folders in the Solution Explorer that the project is under source control.
· Next to the file that the comment has been added to (Program.cs) you will see a red check mark in the Solution Explorer. This means the file hasn’t been saved and committed to the local repo.
Once you have completed all changes, you can right-click on the file and then select Commit from the popup menu.
You will then be flipped over to the Team Explorer tab where you can elect to Stage all the changes or go back and commit other changes in other files. When you are ready to Stage all the changes, click the + sign in the Changes subsection in the Team Explorer as shown below:
The changes will now be moved to Staged Changes. You can now enter a commit message at the top of the Team Explorer and click the Commit All button. When you do this you will get the commit message at the top of the Team Explorer as shown below:
It is critical to note that this only commits the changes to your local repo! To Push your changes to the server repo you can either click the Sync link in the commit message or click the Home button and then click Sync to arrive at the Sync screen:
You can see from the screenshot above that you have an outgoing commit ready to be Pushed to the server. Click the Push link to push the commit to the server. Visual Studio will initiate the push and begin the synchronization. When complete you will get a message at the top of the Team Explorer that the synchronization was successfully pushed to the server repo.
When you want to check for changes and synchronize your local repo with the server repo and incorporate changes from other members of the team, Visual Studio also makes this a simple process. In the Team Explorer go to the Sync screen where you can select the Fetch option which will fetch a list of files and changes but will not update your local repo. You will be presented with a list of any files and associated changes to review if you chose to do so.
When you are ready to pull changes from the server repo to your local repo, simply click the Pull option—BE CAREFUL as pulling will overwrite any changes locally. While it should error out and not pull changes to your local repo if there are uncommitted changes in your local repo it is always a good idea to backup your project folder if you have been doing a lot of work (your choice). When you click Pull, your local repo will be updated with all changes from other members of the team and you will be working on the latest and greatest code. You should get a message the pull was successful or your local repo is already up to date as shown below:
NOTE: Rarely you may get a message that the request timed out in the Output Window; this can happen when bandwidth fluctuates are isn’t consistent. Simply retry your Pull request and you should be able to proceed.
Comments
Post a Comment