Git for Visual Studio and .NET developers
Join the DZone community and get the full member experience.Join For Free
a couple of weeks ago i wrote an introductory article ”
git explained for beginners
” which aimed at introducing git to beginners on the basis of git’s underlying tree structure. since that article turned out to be quite popular and since i’m currently planning an introduction for our .net devs on that topic, i decided to write this article, using a similar approach, but show-casing the use of git from within visual studio.
for getting introduced to the git terminology, please refer to my previous article .
workstation/visual studio setup
recently microsoft seems to recognize git as a valuable alternative to its proprietary tfs (team foundation server) to what regards version control and as such it started to release its own visual studio extension which is currently in “preview” phase. you can find it here:
visual studio tools for git (microsoft)
scott hanselman wrote about it as well. i quickly tried the plugin and although it perfectly integrates with visual studio (mostly like tfs), it is still too much beta for my taste.
currently the best alternative i found is to install git extensions for windows and git source control provider visual studio plugin. the following sections go through their corresponding installations.
install git extensions for windows
the first step is to download git extensions from the corresponding google code repository .
its installation wizard will install everything you need to have a complete git setup, installing git (from git-scm ) to the different unix tools to the git bash.
after you have installed everything, make sure all entries of the git extension’s checklist pass for having a trouble-free git experience.
you find this checklist when opening the git extensions application, and then by navigating to
plugins > settings
installing git source control provider
git source control provider is an open source extension that uses your machine’s git installation and integrates it into visual studio.
if you have successfully setup git (following the procedure mentioned before), you can go ahead and install the git source control provider extension for visual studio. the easiest way is probably through its “extensions and updates” dialog where you have to simply search for “git source control”.
as one of the next steps you need to correctly set the source code provider within visual studio as you might have more of them (i.e tfs). this is done in visual studio’s preferences in the “source control” section:
you should see an entry saying “git source control provider”. choose that one and confirm your setting. after that you should also make sure it references your git and git extensions installations correctly:
configuring your ssh key
many git server repositories allow for different authentication models:
- through https or
- by generating an ssh key .
personally, i prefer the latter as i hate to continuously enter my credentials.
to get a guide on generating a public ssh key just consult the documentation on github which is quite detailed and well explained.
well, that’s it for the installation part. you should now be ready to get started.
let’s get started: create a new git repository
first of all, i simply create a console project, as the focus here is not on creating something nice and working, but rather to demonstrate git’s integration into visual studio. once the project is created we can set up the local git repository.
this is done by right-clicking on the solution and then choose “create git repository”:
after that operation you should see the repository to be setup successfully:
moreover, you should see some files listed in git’s pending changes window:
just click them all, add a meaningful comment and commit them by pressing the “commit” button
git scp (from now on to refer to the git source control provider extension for vs) provides a very nice and handy mechanism for viewing the actual situation of your git repository, namely by visualizing its underlying git tree. just click the “history” button…
…in the “pending changes” window and a new window will open, displaying a nice graph:
so far nothing spectacular, but it shows that our initial commit created a note to which (as expected) head and master point to. clicking on the node reveals further details such as the involved files and according diffs.
an important aspect i didn’t mention in my previous git tutorial is the concept of the .gitignore file. this file basically contains a set of lines specifying which artifacts should be ignored by git. normally these are ide specific files or compiled binary files.
out of the box git scp already creates one that is suitable for visual studio. otherwise you can refer to the gitignore github project which is a collection of .gitignore files for the different kind of ides and programming languages.
create and commit a new file
simply add a new file to your visual studio project. i added
with some dummy content. you should immediately see the changes listed in the pending changes window.
note: you can open the pending changes window by right-clicking the visual studio project or solution file and then choosing “git (master)” followed by “pending changes”.
again, just like before, select the files you want to include, add a meaningful comment and commit them.
our tree after the commit looks like this:
create a (feature)branch
to create a new branch, select the git extensions button on the pending changes window and then create branch
this action will open a new dialog window, allowing you to first of all select the point in history from which you’d like to branch and then to specify its name:
note: we also select the checkbox “checkout after create” s.t. we immediately switch to the new branch. this would somehow be similar to a
git checkout -b <branch-name>command on the shell.
a confirmation dialog shows the success of the operation and the executed command on the shell.
moreover, in the pending changes window we now see the current branch we’re on, which is the just created “my-feature-branch”.
we can now
an existing file - say our
- and commit it. the pending changes window exactly shows the diff of the changes before we commit them
after committing the changes, the tree has advanced and note that now
point to different locations.
is on our feature branch as it is the current active one.
merge and resolve conflicts
let’s switch back to master . we can do this - again - by using the checkout branch button from the “git extensions” menu.
we have to select
as our branch and continue.
note: there are options on how you’d like to treat any local changes that have not yet been committed, i.e. to stash them.
the git tree reflects that switch to the
branch as its
now correctly points to
what i’d like to demonstrate in this section is on how to resolve a merge conflict. for that to work, we need to create another commit on
that conflicts with changes on
. as such, let’s modify
our tree now reflects the existence of our feature branch more prominently.
performing the merge
consider that we want to merge the changes made in
. this process is initiated by using the
button from the git extensions menu.
a dialog opens, allowing us to specify the details of the merge.
first of all, we choose the branch name which is
. then, there are two other options:
“keep a single branch line if possible (fast-forward)” - a fast-forward is explained as follows
[…] to phrase that another way, when you try to merge one commit with a commit that can be reached by following the first commit’s history, git simplifies things by moving the pointer forward because there is no divergent work to merge together — this is called a “fast forward”. git scm: basic branching and merging
obviously, this only works when there are no conflicts.
“always create a new merge commit” - this will simply create a new commit message explicitly specifying that something has been merged.
as expected, fast-forwarding fails and we get a merge conflict notification
the pending changes window shows the merge conflict
git scp will ask you on whether to solve or ignore the conflicts. if you answer positively, a dialog will open, showing all conflicting files.
click on “start mergetool” to open your configured merge tool. in my case this openes “kdiff3”. this might vary on your system, depending on how you configured your git installation.
after you have resolved all conflicts…
…you can continue with the commit procedure.
again, the git tree reflects the merge nicely
jump to a certain commit
jumping to a certain commit is quite easy and can be done by using the browse command from the git extensions menu. this opens an external gui visualizing the git tree.
from there you can use the context menu and click on checkout a selected revision”.
as a result you will transition into a detached head mode . in fact, take a look at the tree:
doesn’t point to a branch any more, but to a specific commit in history. to go back you can always simply perform a checkout on
or some arbitrary branch.
a rollback can be performed in different ways and with different consequences.
files not staged for a commit
undoing local changes that have not yet been committed is quite easy and can be done by simply performing a right-click on the changed file and by selecting “undo file changes”
oops, i forgot to include…
if you have already committed a change to your repository and you recognize that you have forgotten something to include in that same commit (like the comment on that line of code)…
…then git has a nice and handy functionality: the amend last commit command. just add your forgotten change and press the button.
as a result, your last commit will include those changes as well, without leaving a trace that you actually made two distinct commits.
“revert” already committed changes
but now, assume we want to go back entirely, reverting that last change we made. we want, however, to explicitly show that “reverting” of the changes.
note , reverting changes in this way is highly recommended if you synched your changes already with a remote repository.
to use the “revert” functionality, use “browse” from the git extensions menu, right-click on the commit you’d like to revert and select revert commit :
this will stage the reverted changes which you can then commit back to your git repo.
the tree after committing the reverted changes:
discard already committed changes
let’s have a look on what we did before:
we added a property to the
- we performed a revert of adding the property
basically, our two override each other. so if we are in our local repository and we have not synched these changes with any remote counterpart, we might think to completely remove both commits from our history. this doesn’t have any kind of benefit other than doing some housekeeping. for this to happen, we can use the reset command. we select the point in time (i.e. the commit) up to which we want to reset and click the “reset current branch to here”:
you will have different kind of possibilities
in this situation i prefer doing a hard reset as i don’t care to keep the changes in the directory. after the reset, our git tree has no trace about the previous commits:
sharing/synching your repository
so far we have worked exclusively on our local git repository. to publish our repository in order to share it with our mates, we need to push it to some remote location. this can be done from the git extensions menu, choosing the push command.
a new dialog window opens from which you can choose the remote location.
in case you have not yet configured any remote locations you have to do so by using the “manage remotes”.
note: by convention, the “git remote” pointing to the origin repository is named origin .
after the successful configuration of the remotes, you can select one of them and push to it.
the files are now on the remote repository, in my case on our company-internal gitlab server:
similarly, your team mate has to do the opposite operation, namely to download (clone) the repository you just shared, to his local workstation.
to clone an existing repository, use the visual studio menu and choose the ” clone repository ” command
a dialog will open, where you can then enter the git repository url, the clone destination as well as the branch you want to checkout (normally
the tree now also shows the references to the remote counterparts (origin/head and origin/master).
i’d like to migrate my tfs project to git
if you have an existing project already hosted on tfs and you’d like to migrate it to git, then you can use microsoft’s git-tf tool.
git-tf is a set of cross-platform, command line tools that facilitate sharing of changes between tfs and git. git-tf codeplex
as described on the project’s website you can simply perform a clone of your tfs repository like
git tf clone http://myserver:8080/tfs/collectionname $/teamprojecta/main --deep
don’t forget the
addition to ensure you’re copying the entire history. otherwise you’ll just clone the last tfs changeset. after you have successfully cloned the tfs repository you can add a remote to your new git server repository just normally.
git remote add origin firstname.lastname@example.org:mygituser/myreponame.git
..and then perform a
git push -u origin master
-uis needed as you’re publishing the
masterbranch for the first time to your remote.
resources and links
Published at DZone with permission of Juri Strumpflohner. See the original article here.
Opinions expressed by DZone contributors are their own.