Introduction
In this post, I am going to show some basic Git operations using Visual Studio without using the Git command line tool.
Git
Git is an open source version control system which keeps the source code and also tracks the changes in it. Git is mainly used for source code management in software development, where we keep source code in the git repository/directory/branch and it gives the centralized access to the team. Git also keeps track of changes. Git has a set of commands and git cmd to perform git operations, like create repository in git, create a local copy of the repository, create branches, add files, commit the changes, push the changes to a remote repository, and many more. Here is the list of all the git operations -
git operation list.
Microsoft Visual Studio has Git client built-in directly into the IDE. We can perform most of the source control operations on git from Visual Studio directly. Visual Studio 13 update 1 has great support with git.
Create a repository
Git repository, also called repo, is a folder on the git which is used to keep and track project files and changes. The new repository can be created while creating a new solution in VS, simply by selecting "Create new Git repository".
Also, we can create a repository from an existing solution. Go to Team Explorer and click on the "Publish" button in the bottom. It will create a new Git repository and open a "Publish view" to push the code to the remote Git repository.
Clone remote repository
To get the code from the git remote repository, we clone the git repository and initialize local repository. Go to team explorer; there is a plug icon on the top. Click on the plug icon and select the repository. Enter the repository path, then click on the "Clone" button. It will clone the repository and download all the commits and branches in the local copy of the repository.
Now, we have the copy of the repository. Make changes to the branch in files and perform another git operation.
View changed files
To see the uncommitted changes and files, go to Team Explorer and click "Changes".
Stage (index) file
To stage (or index) uncommitted changes, select the file, enter the comment, and click "Stage".
Commit/Push modifications
To Commit/Push, you need to put some text in the comment area before committing the changes.
You can choose from three options.
Options | Actions performed |
Commit Staged | Commit locally |
Commit Staged + Push | Commit locally + Push to a remote repository |
Commit Staged + Sync | Commit locally + Pull (get updated info from a remote repository) + Push to a remote repository |
Result
The committed files will disappear from the change list and the commit id will be displayed.
Push modifications
To push locals commits to a remote server, go to Team Explorer Synchronization and click "Push".
Stashing code
Visual Studio does not implement this feature for now. You have to either stash in the command line or with a third-party software (SourceTree, …) or you can install a VS extension.
Change the current branch
Go to Team Explorer and click on the Branches.
You can change the current branch by double-clicking on the branch (All the files are instantly updated).
But if you still have modified files in the current branch, you cannot change the branch first. You have to commit, stash, or cancel the modifications.
Your recently used branches are available in the bottom shortcut of Visual Studio.
Create a new branch
To create a new branch, go to the Team Explorer, click on “New branch”, and you will have to indicate -
- the branch name
- the branch to be used to create the new one
If you want to, switch to the branch you create (“Checkout branch”, but not like the TFS meaning)
Conclusion
In this article, we have seen some basic Git operations which we can perform with Visual Studio. We also saw that there are some limitations, such as stashing, which is not possible yet with Visual Studio.