Git Operations With Visual Studio
Git Operations With Visual Studio
If you're more inclined to using Visual Studio, check out how you can still use it for your version control with Git.
Join the DZone community and get the full member experience.Join For Free
Git is an open source version control system which keeps the source code and 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 centralized access to the team. Git also keeps track of changes. Git has a set of commands and
git cmd is used to perform git operations, like create a 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.
Microsoft Visual Studio has a 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
A git repository, also called a 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."
We can also 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 a 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 a 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."
To Commit/Push, you need to put some text in the comment area before committing the changes.
You can choose from three options.
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
The committed files will disappear from the change list and the commit id will be displayed.
To push locals commits to a remote server, go to Team Explorer Synchronization and click "Push."
Visual Studio does not support 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.
Read here about Git stashing.
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
- and 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)
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.
Opinions expressed by DZone contributors are their own.