共用方式為


Work from the Git command prompt

Visual Studio provides most of the fundamental capabilities to develop an app in a Git version-controlled codebase. You might have to use the command prompt for some manual tasks or to automate work using a script.

  • Work from the command prompt

  • Get set up to use the command prompt tools

    Prompt to install Git command prompt tools

    • Install the command prompt tools

    • Launch the Git command prompt

    • Create a personal access token for your Visual Studio Online account

  • Q & A

Work from the command prompt

Warning

If you are not an experienced Git user, use the command prompt carefully. Make sure to research the command thoroughly before you use it.

I want to…

Can I do it in Visual Studio?

How do I do it from the command prompt?

Amend my last commit. Some typical cases:

Yes

git-commit

Apply a tag to a commit

No, but you can view them. See View and manage past versions in Git.

You can use the command prompt to push, edit, and remove tags (see Git-scm: Git Basics - Tagging) from a repository in TFS if you have sufficient permissions.

Branch and merge

Yes (but some conflicts can be resolved only at command prompt).

git-branch, git-merge

Commit my changes

Yes

git-commit

Tip

You can associate a work item with a commit by including the ID in your comment. For example, you apply this comment #35 Catch null exception to your commit. When you push this commit into TFS, the commit will be associated with work item #35.

Copy (clone) a remote repository to my dev machine

Yes

git-clone

Create (initialize) a local repository

Yes

git-init

Create or edit a note

No

You can use the command prompt to push, edit, and remove notes (see Git-scm: Note to Self) from a repository in TFS if you have sufficient permissions.

Get information about my local repository (such as the remotes I am tracking)

If the repository is in a TFS team project, then yes. Otherwise, no.

git-remote

Preview (fetch) and then download (pull) changes from a remote repository

Yes (but some conflicts can be resolved only at the command prompt)

git-fetch, git-pull

Push changes to a remote repository

Yes

Git-scm: git-push

Re-order history or combine (squash) commits.

No

Git-scm: Git Branching - Rebasing, Git-scm: Squashing Commits, git-rebase

Revert a committed change by applying the inverse of the commit. See rolling back changes with revert.

Yes

git-revert(1) Manual Page

Stash changes

No

Git-scm: Git Tools - Stashing

Undo committed changes by returning my local repo to a prior commit and de-referencing the later commit.

Warning

According to Undoing Things, “...this is a dangerous command: Any changes you made to that file are gone — you just copied another file over it. Don’t ever use this command unless you absolutely know that you don’t want the file.”

No

git-reset

View and manage my changes since the last commit

Yes

Git-scm: Git Basics - Recording Changes to the Repository, Git-scm: Git Basics - Undoing Things

View history

Yes

Git-scm: Git Basics - Viewing the Commit History

Get set up to use the command prompt tools

Before you can use the command prompt tools, you have to install them and then create a personal access token or alternate authentication credentials for authentication..

Install the command prompt tools

If you have not already installed some command prompt tools, you can get some quickly from Visual Studio. (One way you can tell that you don’t have the tools is if you try to enter a git command and get the 'git' is not recognized as an internal or external command... message.)

Installing the Git command prompt tools

Tip

  • The install process drops a Git Bash icon on your desktop. We recommend you delete this icon because we don’t believe this entry point leads to the best experience. If for some reason you want to run Git Bash later, you can do so from Windows Start.

  • To make using the command prompt less tedious (for example, to avoid having to enter your credentials every time you push), you might want to also install Windows Credential Store for Git.

  • If you want to run Git commands from PowerShell, install Posh-Git (a PowerShell environment for Git).

Launch the Git command prompt

You can launch the Git command prompt from the Actions menu on the Changes (Keyboard: Ctrl + 0, G), Commits, and Branches pages.

Opening the command prompt

You can also launch the Git command prompt from repositories on the Connect (Keyboard: Ctrl + 0, C) page.

Open the command prompt from a repository

Create a personal access token for your Visual Studio Online account

If your repository is hosted on Visual Studio Online, you must authenticate access to your account before you can use the command prompt to perform Git tasks. To authenticate access, create a personal access token.

  1. Sign in to your Visual Studio Online account (http://[youraccount].visualstudio.com).

  2. Go to your team project's home page and open your profile.

    Account home page, my profile

  3. Create a new personal access token for this account.

    Add personal access token

  4. Enter the details for this token.

    Enter token details

  5. Select the scopes that this token authorizes.

    Select authorization scopes for token

  6. After you finish creating the token, make sure to copy the token. You'll use this token as your password for your Git tools or application.

    Create the personal access token

    Important

    Remember that these tokens are your identity. When used, the token is acting as you. Keep your tokens secret and treat them like your password.

    To help keep your token more secure, consider using the Windows Credential Store for Git so that you don't have to enter your credentials every time you push.

    For example, if you use the Git command prompt to run a Git command, you'll be prompted for a username and password.

    git clone https://[account].visualstudio.com/DefaultCollection/_git/[team project]
    

    Enter a username that does not contain an @ character (for example, Jamal, not fabrikamfiber4@hotmail.com). Use the token that you created as your password.

    Username for 'https://fabrikam-inc.visualstudio.com': Jamal
    Password for 'https://fabrikam-inc.visualstudio.com': [COPY THE TOKEN HERE]
    
  7. When you don't need your token anymore, just revoke the token to remove its access from this account.

Q & A

Q: Where can I learn more commands?

A:http://git-scm.com/docs

Q: Why should I use personal access tokens?

A: Personal access tokens are a more convenient and secure replacement for alternate authentication credentials. You can create a token and limit its use to:

  • The token's lifetime

  • A Visual Studio Online account

  • Scopes of activities that this token authorizes

Q: Can I still use alternate authentication credentials?

A: Yes.

  1. Sign in to your Visual Studio Online account (http://[youraccount].visualstudio.com).

  2. Go to your team project's home page and open your profile.

    Account home page, my profile

  3. Enable alternate credentials for this account. Then provide a secondary username and password.

    Enable alternate authentication credentials

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