Condividi tramite


Set up Git on your dev machine (configure, create, clone, add)

When you start using Visual Studio with Git, choose the way that works best for you and the kind of project you’re working on. For example, you can start an experimental solo effort in a new or an existing local repository and continue developing there as long as you want. Or you can join a collaborative effort in a remote Git repository, hosted either in Team Foundation Server (TFS) or on another service.

Before you start

What do you want to do?

  • Start from a local repository Start from your dev machine with a new solution or an existing solution, add an existing local repository, or create a new (init) empty repository

  • Start from a remote repository Start from a remote repository (hosted on either TFS or on another service) and then clone it to your dev machine

  • Customize Git settings on your dev machine Customize (config) how you identify yourself, specify the kinds of files you manage, and customize other settings

  • Get answers to common questions

Start from a local repository

You can create a local repository on your dev machine—whether or not you have a network connection—and start developing right away: coding, committing, branching, and merging code. When you’re ready to collaborate with your team, you can publish one or more branches from your local repository into a team project.

  • Create a new solution under local Git version control

  • Put an existing solution under local Git version control

  • Create or add a local repository

  • Publish your local repository into TFS

Create a new solution under local Git version control

You've got an idea for a new app, so you want to experiment on your dev machine. In less than a minute you can use Visual Studio with Git to create a new code project under local version control. (And no Internet required!)

Create a new code project (Keyboard: Ctrl + Shift + N). We suggest that you put your new project in c:\Users\YourName\Source\Repos\.

New ProjectChoose Git Source Control

Put an existing solution under local Git version control

You’ve already got an app in progress and you want to start working on it under local Git version control.

Tip

Before you add the solution to Git version control, we recommend you first move the solution to the TFS Git default location: c:\Users\YourName\Source\Repos\

  1. If you have not already done so, open your solution, (Keyboard: Ctrl + Shift + O) and then open Solution Explorer (Keyboard: Ctrl + Alt + L).

  2. Add your solution to source control.

    Adding a solution to version control

  3. On the Choose Source Control dialog box, choose Git.

  4. Now that your repository is created, you are ready to commit your files. Go to the Changes page (Keyboard: Ctrl + 0, G) and commit.

    Open changes page

    Committing the new solution

    (If you are prompted to configure your user name and email address, do that now. See Configure Git settings.)

    The commit succeeded

Create or add a local repository

You can create an empty local repository and add files later. It’s possible to track your changes to the files whether or not they are part of a solution. Or, if you already have a local repository, just start working with it in Visual Studio.

Open the Connect page (Keyboard: Press Ctrl + 0, C).

Team Explorer Connect page

To create an empty local repository, choose New. To open a local repository that already exists on your dev machine, choose Add.

Creating a new local Git repository

Specify the local path and then choose Create or Add.

Publish your local repository into TFS

When you are ready to share your code and collaborate with your teammates, publish your local repository into TFS.

  1. Make sure you have committed all your changes in the local repository. See Manage and commit your changes.

  2. If you haven’t already done so, create a new a new team project (choose Git version control) or create a new Git repository in an existing Git team project.

  3. From the Connect page (Keyboard: Ctrl + 0, C), connect to the empty Git repository and publish the local repository to it.

    Publishing a local repository into TFS

Start from a remote repository

Your friends have invited you to work with them on a new project. Or maybe you are setting up a new project or a new dev machine. You can use Visual Studio and Git to collaborate on TFS (on-premises or in the cloud), on CodePlex, or on a third-party service such as GitHub or Bitbucket.

What do you want to do?

  • Open and clone a repository in a Git team project

  • Clone a remote Git repository from a third-party service

Open and clone a Git team project

If you haven’t already done so, go ahead and create or get access to a Git team project.

From Visual Studio: Go to the Team Explorer Connect page (Keyboard: Press Ctrl + 0, C) and then connect to the team project.

Connect to the Git team project

(If the team project you want to open is not listed, choose Select Team Projects and then connect to the team project.)

From the web: Open a team project from its home page in your web browser (Keyboard: Ctrl + 0, A).

Open a team project from web access

After you connect to the Git team project, if you have not already done so, you must clone it to your dev machine before you can work in it.

Prompt to clone the remote repository

Cloning a Git repository in a team project

Just specify the local path and choose Clone.

Clone a remote Git repository from a third-party service

Does your team have some code in GitHub or another service such as CodePlex or Bitbucket? To start working in Visual Studio, clone the code to your dev machine.

Cloning a remote third-party repository

Note

You can use Visual Studio’s Git capabilities with services other than TFS. However, if you use these repositories, you will not be able to use TFS features such as project planning and tracking and Team Foundation Build.

Customize Git settings on your dev machine

To customize your Git settings, you must be connected to a local or remote Git repository. Open the Git Settings page.

Opening the Git Settings page

  • Apply global settings Apply global Git settings to control aspects of how Git functions for the current user on the dev machine. For example, you can specify how you identify yourself on the changes you commit.

  • Apply repository settings Apply settings to control how Git functions in each individual local repository on your dev machine. For example, you can fine tune how the system blocks clutter from entering your user experience and repository.

  • Apply more settings Visual Studio respects all Git settings but provides you with control over only a few of them. Use the Git command prompt to customize all Git settings.

Apply global settings

Git global settings

User Name and Email Address: Git associates each commit you create with your name and email address. When you start using Visual Studio with Git on your dev machine, if you connect to a Git team project first, then Visual Studio fills in your name and email address for you.

Default Repository Location: Specify the default root directory where you want to create or clone new local Git repositories.

Author images: Use images to more easily see the author of each commit.

  • If your Git repo remote origin is in a TFS Git team project, team members can specify their images in their TFS profiles. How? See tips below.

  • If your Git repo remote origin is in a non-TFS Git service (such as CodePlex, GitHub, or Bitbucket), select Enable download of author images from 3rd party source, and then ask team members to set up Gravatar accounts for their email addresses.

Note

Enable download of author images from 3rd party source also works for TFS Git team projects in cases where the author has not supplied a profile image.

An example of how author images enhance the collaborative experience:

Git author image examples: branches and history

Apply repository settings

Adding Git repository setting files

If your repository does not have settings files, you should probably use Visual Studio to add some default files that apply the most typically useful settings. You’ll avoid distraction and potential clutter in your repository from non-source files such as locally-built binaries.

.gitignore file: See Use the Git ignore file to avoid file clutter in your work and in your repository.

.gitattributes file: To specify options such as how the system handles line-breaks, specify a .gitattributes file. See Customizing Git - Git Attributes

Commit your repository settings files: In most cases you should commit and push these files so that everyone else on your team uses the same repository settings on their dev machines.

Committing settings file changes

Apply more Git settings

You can specify three kinds of Git settings, listed in order of supersedence:

  • Repository settings apply to the work done in the local repository.

  • Global settings apply to the work done by the current user on the dev machine.

  • System settings apply to all work done on the client dev machine. (Visual Studio respects these settings, but does not expose them.)

If you need to modify system settings, or if you prefer the command prompt, then modify your Git settings from there. See Work from the Git command prompt, Customizing Git - Git Configuration, and git-config command.

Q & A

Q: I’m really new to all this. How can I get more help?

A:Follow a step-by-step walkthrough to get started using Git to work locally on a new project and then to begin collaborating with a team on Visual Studio Online.

Q: I was blocked by the system because I don’t have permission. How do I get it?

A: Permission reference for Team Foundation Server

Q: What kinds of names should I use for my folders?

A: In most cases, it’s best to use a short, understandable folder path. For example: C:\Users\YourName\Source\Repos\FabrikamGit\SolutionName\.

Some tips on effective folder names:

  • Keep all folder, sub-folder, and file names short to simplify your work and avoid potential long-path issues that can occur with some types of code projects.

  • Avoid whitespace if you want make command-line operations a little easier to perform.

Q: How do I specify my TFS author image?

A: If your Git repo remote origin is in a TFS Git team project, you can specify your images in your TFS profile from your web browser (Keyboard: Ctrl + 0, A).

On the Home page, choose Web AccessMy Profile link on Account menu

Q: It seems like I can claim to be anybody I want to be in my Git Settings. How can I know for sure who made a change?

A: Yes, any contributor to your team project can claim any user name and any email address they want when authoring a commit. However, TFS does authenticate who pushes the commit. To see who pushed a commit, open your team project in your web browser (Keyboard: Ctrl + 0, A). Open the commit you want to examine from the Commits section, and then expand the commit details.

Commit 'Pushed by" field

Q: Can I work from the command prompt?

A: Yes. Work from the Git command prompt.

Try this next

Set up a CI build

Dig deeper

Develop your app in a Git repository (track, commit)