Create, Connect, and Publish using Visual Studio with Git

Create, Connect, and Publish using Visual Studio with Git

Rate This
  • Comments 27

By: Andy Lewis and Matthew Mitrik (MS)

In the few days since we announced Visual Studio and Team Foundation Service support for Git, it's been exciting to see so much interest, along with a lot of great feedback and questions. We will take your feedback to help us guide how we evolve the tools and the service; so please keep it coming. And as for the questions, we hope that you will find in this post some answers to help you make the most of our Git tools in Visual Studio and our Git service in TFS.

Before we begin, let's recall a key point from Brian Harry:

Both client and server are standard implementations of Git. Our client will work with pretty much any Git repository – local, enterprise, Codeplex, GitHub, BitBucket, …. And TFS will work with pretty much any Git client – existing Git command lines, XCode, Eclipse’s Git support, …. This was a core principle from day 1. This is not about lock in – it’s about providing a good and interoperable Git capability. From Git init VS

In this post we will demonstrate Visual Studio's flexibility to help you get your work done in the places you need to do it: locally, on TFS, or on a third-party service such as those Brian mentions above.

We're here today to help you:

Create new local repositories

One of the key advantages of Git is your ability to do more things locally. Visual Studio provides a few convenient ways to create a local repository:

Create an empty local repository

You just want to create an empty local repository. In Visual Studio you can do this in a few seconds.

Go to the Connect page and create the new repository.

 

Open the repository.  This will set the repo as the active repo in Team Explorer.

Go ahead and commit the default .gitattributes and .gitignore files that Visual Studio created for you. (We'll talk more about these and other Git settings in a future post).

Your new local Git repository is now ready for you to use.

When you are ready, you can publish and share it with your team.

Create a new solution under local Git version control

Whenever you create a new code project, you can put it under local Git version control.

We cover this angle from start to finish in our Welcome Portal tutorial.

Put an existing solution under local Git version control

If you've already got a solution in which you've been tinkering on your local dev machine, before you tinker any further, why not go ahead and add it to a lightweight local Git version control repository?

Now that your repository is created, go ahead and commit your files.

Add an existing local repository

Maybe you've got other repositories you've been using with other client tools. Regardless of whether the local repo has a remote or not, you can add it and then work with it in Visual Studio.  You can add a single repo at a time, or, as we show below, point Visual Studio at a directory that contains multiple repositories and add them all in one shot.

After you add the repository, you can open it and get to work in Visual Studio.

BTW, notice that nifty "Open in Command Prompt" command? Eventually you will likely need it to perform some tasks. If you don't already have command-line tools installed, you can install some:

We'll talk more about common scenarios for using these tools in a future post.

Clone a remote Git repository to your dev machine

Doesn't matter where your Git repository is hosted: TFS, CodePlex, GitHub, Bitbucket, or somewhere else. You can use Visual Studio to work with it. To begin collaborating with others on code in a remote repository, you clone it to your dev machine.

Clone a TFS Git repository to your dev machine

Whether you are new to a team project or just setting up a new dev machine, to get working in a Git team project, clone it to your dev machine.

Clone a third-party Git repository to your dev machine

Does your team have some code in GitHub or another service such as CodePlex or Bitbucket? You can clone the code down to your dev machine and get working in Visual Studio.

Publish your local repository

When you are ready to begin sharing some code on your dev machine with others, you publish the branch to the remote repository.

Publish to TFS

You can publish your code into Team Foundation Service.

Publish to a third-party service

You can publish your branch to a remote repository hosted on a third-party service such as CodePlex, GitHub, or Bitbucket.

 

Did you get the "Branch master does not exist" error? Click here.

After you publish

Having published your branch, Visual Studio has done two things for you:

  • Set the remote of the local repository
  • Pushed your active branch (in most cases this is "master") to the remote repository

After this is done, to collaborate with your team, you can as needed pull their changes and push your changes to the remote repository.

Manage your connections

The Connect page is a new feature in Visual Studio 2012 Update 2. From here you can see and manage all the version control repositories you care about.

Just right-click the repository you want to work and then choose Open or another command.

Tell us what you think

We are experimenting with ways to be more agile and effective in delivering guidance to you. We welcome your feedback not only on our new Git capabilities, but also on the content and format of this post. As we noted above, stay tuned for a few more posts coming on topics such as working with command-line tools, and adjusting Git settings. Thanks for your time and for trying out Visual Studio and TFS with Git!

Leave a Comment
  • Please add 6 and 1 and type the answer here:
  • Post
  • Hi guys, great job with the how-to.

  • The message of the last months has arrived and now bears first fruits.

    When will the local git-tfs server is available?

  • Manu: Our plan is to include Git support in the next major release of TFS.  No date has yet been announced.

  • I have VS without Team Explorer. Can I use Git?

  • @Lex - all VS 2012 editions should have Team Explorer installed, even if you're not using TFS.  You should be able to open it from the View menu.

    -Matt

  • This is a really good start.  Hopefully by the time update 2 is out of preview and goes live the few little kinks I've experienced will go away.  Right now, the one that is the most annoying to me is as soon as I open a .sln file that is under git control, Team Explorer marks it as changed (red check mark) and wants a commit message.  Diff indicates that the file is unchanged.  Running "git status" from the command line returns:

    # On branch master

    nothing to commit, working directory clean

    In an attempt to make Team Explorer happy, I enter a commit message and commit.  The red check clears, then comes back asking for another commit message.  That experience just absolutely rubs my mild OCD the wrong way and would drive me to the consumption of too many adult beverages.

  • Hi Andy, that is a great tool/plugin and an awesome article.

    Do you know if there are any plans to implement functionality to associate Git check-ins (commits) with TFS work items? I understand that is an early stage of Git support, but are there any nearest/long term plans for that?

    Thank you very much in advance for an answer!

  • @Alex: Yes, you can associate a work item. See tfs.visualstudio.com/.../commit-vs-git

  • I want to publish to an external repository on GitHub, but I don't get the "publish" option as explained in this article. What am I missing? Do I need to install anything else besides "Visual Studio Tools for Git". I appreciate any help. Thanks

  • @Chris: You can't do this from VS, but you should be able to do it from the command prompt.

  • Nice compilation..Thanks

  • I was able to get VS working with ProjectLocker in no time. I like how you get to visualize commits and branches before you pull anything. For MS developers new to git, I would recommend performing the same commands in the command line (just type git --help at a prompt.) That way you understand exactly what the VS Tools for Git are doing behind the scenes. You can also cross-platforms with git that way.

  • I've followed all the steps, have a local GIT repo and am connected to a remote TFS GIT Repository...

    BUT...

    I can't push my local to the server. When I right-click the local repo, there is no option to "Publish to..." the server, only "Open", "Open in File Explorer", and "Open Command Prompt"

    How do I connect them?

  • @ERobishaw: Make sure the Git team project is empty. See msdn.microsoft.com/.../hh850445.aspx

  • I'm having a problem with cascading .gitignore files.  I have nested git repos with this structure.

    Core

      \.git

      \.gitignore

      \Code Files...

      \CodeToIgnoreByLocalIgnore\...

       \CodeToIgnoreByGlobalIgnore\...

       \MoreCodeToIgnoreByGlobalIgnore\...

    \Clients

          \ClientA

              \.git

              \code...

        \ClientB

              \.git

              \code...

    My global (c:\users\{users}) .gitignore file has following entries:

    Clients/

    CodeToIgnoreByGlobalIgnore/Setup/

    My local .gitignore file has following entry:

    CodeToIgnoreByLocalIgnore/

    When looking at the Changes section of Git in VS 2013 Team Explorer, it shows untracked files of

    CodeToIgnoreByGlobalIgnore/Setup/*.*

    When I go look at Git settings is VS 2013 Team Explorer, it says the 'Ignore File' is /.gitIgnore (the local one).

    So if all the formatting of my comments display correctly and you can easily see the file structure I have, I am getting weird behavior.  It is almost as if VS is obeying some of the settings in global .gitignore (i.e. the MoreCodeToIgnoreByGlobalIgnore is correctly ignored) but not all of them (CodeToIgnoreByGlobalIgnore/Setup).

    Hopefully I explained it well.  Let me know if you have any suggestions.

Page 1 of 2 (27 items) 12