Freigeben über


Git command reference

TFS 2018

Visual Studio 2019 | Visual Studio 2022

In Visual Studio 2019, Team Explorer lets you do most common Git tasks that you'll need for your daily work. From the Visual Studio View menu, open Team Explorer or use the Ctrl+, Ctrl+M hotkey.

Visual Studio 2019 version 16.8 and later versions provides a Git version control experience while maintaining the Team Explorer Git user interface. To use Team Explorer, uncheck Tools > Options > Preview Features > New Git user experience from the menu bar. You can exercise Git features from either interface interchangeably.

Visual Studio and the Git command-line work great together. When you make updates and run commands through one interface, you'll see those changes reflected in the other.

Git Installation instructions are available if you don't have Git installed on your computer.

Tip

Windows users: If you aren't using Visual Studio, install Git for Windows to set up the Git Credential Manager. The credential manager makes it easy to authenticate with Azure Repos.

While in Visual Studio, open a command prompt in your repo from Team Explorer's Connect view. Right-click your local repo and select Open Command Prompt.

Open a command prompt to a repo from inside Visual Studio

Important

Some commands require having specific Git permissions in Azure Repos to complete.

Repos


How do I?

Git command-line

Visual Studio Team Explorer

Visual Studio Git menu


Create a repo in a new folder

git init foldername

Select the Connect button ( Team Explorer plug icon to open the Connect page ) in Team Explorer to open the Connect view, then select New under Local Git repositories

Use the command line


Create a repo with code in an existing folder

git init foldername
git add --all
git commit -m "Initial commit"

Create the repo from the command line, then open Team Explorer's Connect view and select Add under Local Git repositories

Use the command line


Create a repo from an existing Visual Studio solution

git init foldername
cd foldername
git add --all
git commit -m "Initial commit"

Open the solution and select Publish ( Publish button on the status bar in Visual Studio 2015 Update 2 ) from the status bar in the lower right

Choose Git > Create Git Repository from the menu bar to launch the Create a Git repository window


Create a new repo in your Project

Not applicable

From the web, select Repos (or Code if you haven't enabled the new navigation preview), then select the drop-down next to the current repo name and choose New Repository...

Choose Git > Create Git Repository from the menu bar to launch the Create a Git repository window


Clone a repo into a local folder

git clone URL foldername

Select Clone under Local Git repositories in Team Explorer's Connect view

From the Git menu on the menu bar, choose Clone Repository to open the Clone a repository window


Clone a repo in your Project

git clone URL foldername

Open the Connect view in Team Explorer and right-click the Git repo in your Project under the account name. Select Clone.

From the Git menu on the menu bar, choose Clone Repository to open the Clone a repository window


Add an existing repo to Visual Studio

Not applicable

Open the solution file in Visual Studio (this action automatically adds the repo to Team Explorer) or select Add under Local Git repositories in the Connect view

Choose Git > Local Repositories > Open Local Repository from the menu bar


Delete the Git repo and history, but keep the current version of the files

Delete the hidden .git folder created at the root of the repo

Delete the hidden .git folder created at the root of the repo from Windows Explorer or the command line


Delete a local repo and all files

Delete the folder containing your repo from your computer's file system

Close any open solutions using files in the repo, then delete the folder containing your repo from your computer's file system


Delete a repo in your Project

Not applicable

Not applicable, instead see Delete an existing repo


Add a remote

git remote add name url

Open the repository using the Connect view in Team Explorer, then open the Settings view in Team Explorer. Select Repository Settings, and select Add under Remotes.

From the Git menu on the menu bar, select Push to Git service to open the Create a Git repository dialog.


Update a remote

git remote set-url name url

Open the repository using the Connect view in Team Explorer, then open the Settings view in Team Explorer. Select Repository Settings, and select Edit under Remotes.

From the Git menu on the menu bar, select Manage Remotes, and make changes in the Options dialog.


For more information, see the following resources:

Branches


How do I?

Git command-line

Visual Studio Team Explorer

Visual Studio Git menu


Create a new branch

git branch branchname

Open the Branches view in Team Explorer, then right-click a branch and choose New Local Branch From...

From the Git menu on the menu bar, select Manage Branches, then right-click a branch and choose New Local Branch From...


Swap to a different branch

git checkout branchname

Open the Branches view in Team Explorer, then double-click a local branch. Alternatively, select the current branch name from the status bar and select a different branch.

From the Git menu on the menu bar, select Manage Branches, then right-click a branch and select Checkout


Create and switch to a new branch

git checkout -b branchname

Open the Branches view in Team Explorer, then right-click a branch and choose New Local Branch From...

From the Git menu on the menu bar, select Manage Branches, then right-click a branch and choose New Local Branch From... and select Checkout branch


Delete a local branch

git branch -d branchname

Open the Branches view in Team Explorer, then right-click the branch and select Delete. You must be checked out to a different branch than the one you want to delete.

From the Git menu on the menu bar, select Manage Branches, then right-click a branch and select Delete


Delete a remote branch

git push origin --delete branchname

Open the Branches view in Team Explorer, expand the remote that has the branch you want to delete. Right-click the remote and select Delete Branch from Remote.

From the Git menu on the menu bar, select Manage Branches, then right-click a remote branch and select Delete


Lock a branch, preventing updates to it

Not applicable

From the web, select the Branches tab while viewing your repo. Select the ... next to the branch you want to lock and choose Lock. Unlock the branch with Unlock.


Set a default branch in your Azure DevOps repo

Not applicable

Select the settings icon on the web ( Gear icon on the top navigation bar in Azure DevOps Services ), then select the Version Control tab. Select your Git repository, then select the ... next to the branch name and choose Set as default branch.


Set a compare branch for pull requests in Azure DevOps repo

Not applicable

From the web, select the Branches tab while viewing your repo. Select the ... next to the branch you want to lock and choose Compare branch.


For more information, see the following resources:

Commits


How do I?

Git command-line

Visual Studio Team Explorer

Visual Studio Git menu


Create a new commit

git commit -m "message"

Open the Changes view in Team Explorer. Stage changes by right-clicking on the modified files and selecting Stage. Enter a commit message and select Commit Staged.

From the Git menu on the menu bar, select Commit or Stash to view Git Changes. Stage changes by right-clicking on the modified files and selecting Stage. Enter a commit message and select Commit Staged.


Amend the last commit with staged changes

git commit --amend -m "Updated message"

Open the Changes view in Team Explorer, stage your changes, then select Amend Previous Commit from the Actions drop-down.

From the Git menu on the menu bar, select Commit or Stash to view Git Changes. stage one or more files, enter a commit message, select Amend, and then choose Commit Staged.


Stage all file changes

git add --all

Open the Changes view in Team Explorer. Select the + icon in the Changes list to stage all changes for the next commit.

From the Git menu on the menu bar, select Commit or Stash to view Git Changes. Select the + icon in the Changes list to stage all changes for the next commit.


Stage a specific file change

git add filename

Open the Changes view in Team Explorer. Stage changes by right-clicking on the changed file and selecting Stage.

From the Git menu on the menu bar, select Commit or Stash to view Git Changes. Stage changes by right-clicking on the changed file and selecting Stage.


Review unstaged changes

git status --untracked

Open the Changes view in Team Explorer. Unstaged changes are listed under Changes section.

From the Git menu on the menu bar, select Commit or Stash to view Git Changes. Unstaged changes are listed under Changes section.


Delete a file

git rm filename
git commit -m "filename"

Delete the file through Solution Explorer, the command line, or any other means. Right-click the deleted file in Team Explorer's Changes view and select Stage. Select Commit Staged to commit the deletion.

Delete the file through Solution Explorer, the command line, or any other means. From the Git menu on the menu bar, select Commit or Stash to view Git Changes and select Stage. Select Commit Staged to commit the deletion.


Move a file

git mv filename
git commit -m "Moved filename"

Move a file from one location to another in your repo through Solution Explorer, the command line, or any other means. Right-click the moved file in Team Explorer's Changes view and select Stage. Select Commit Staged to commit the move.

Move a file from one location to another in your repo through Solution Explorer, the command line, or any other means. From the Git menu on the menu bar, select Commit or Stash to view Git Changes. Select Commit Staged to commit the move.


Tag a commit

git tag -a tagname -m "description"

Open the Changes view in Team Explorer, then choose View history..." from the Action drop-down. Locate the commit in the History view, then right-click and select Create Tag.

From the Git menu on the menu bar, select Manage Branches, then right-click a branch and select View History. Locate the commit in the History view, then right-click and select New Tag.


To learn more, see Save your work with commits.

Compare files and versions


How do I?

Git command-line

Visual Studio Team Explorer

Visual Studio Git menu


Compare the current contents of a single file and the contents in the last commit

git diff HEAD filename

Right-click the change in the Changes view in Team Explorer and select Compare with unmodified

From the Git menu on the menu bar, select Commit or Stash to view Git Changes. Right-click the change and select Compare with unmodified.


Compare your current version with a branch

git diff branchname

Right-click a file in Solution Explorer and select View History..., then select both on the latest commit on your current branch and the latest commit on the remote branch. Right-click and select Compare.


Compare changes between two branches

git diff branchname1 branchname2

Right-click a file in Solution Explorer and select View History..., then select the most recent commits for both branches. Right-click and select Compare


To learn more, see Compare versions and review history.

Sync changes


How do I?

Git command-line

Visual Studio Team Explorer

Visual Studio Git menu


Download new branches and commits from a remote repo, but do not merge them into your local branches

git fetch

Open the Sync view from Team Explorer and select Fetch

From the Git menu on the menu bar, select Commit or Stash to view Git Changes. Select the Fetch arrow.


Merge updates from a remote repo into your local repo

git pull remote branchname

While working on the branch in your local repo, open the Sync view in Team Explorer, then select Pull

From the Git menu on the menu bar, select Commit or Stash to view Git Changes. Select the Pull arrow.


Publish a local branch to a remote repo

git push -u remote branchname

Open the Sync view in Team Explorer and select Publish under Outgoing Commits

From the Git menu on the menu bar, select Commit or Stash to view Git Changes. Select the Push arrow


Synchronize your local branch with a remote branch, pushing your local changes and pulling remote ones

git pull remote branchname
git push -u remote branchname

Open the Sync view in Team Explorer. Select Sync

From the Git menu on the menu bar, select Commit or Stash to view Git Changes. Select the Sync icon


Force push a branch, rewriting the remote branch history with the history of your current branch

git push --force -u origin remote_branchname

Use command line

Use command line


For more information, see the following resources:

Merge and rebase


How do I?

Git command-line

Visual Studio Team Explorer

Visual Studio Git menu


Merge a branch into the current branch

git merge branchname

In the Team Explorer Branches view, right-click the branch you want to merge and select Merge From... Verify the options set and select Merge.

From the Git menu on the menu bar, select Manage Branches, right-click the branch you want to merge and select Merge <local branch> into <current branch>


Merge a remote branch into the current branch

git pull origin branchname

In the Team Explorer Branches view, right-click the remote branch you want to merge and select Merge From... Verify the options set and select Merge.

From the Git menu on the menu bar, select Manage Branches, right-click the remote branch you want to merge and select Merge <remote branch> into <current branch>


Rebase your current branch onto the history of another branch

git rebase branchname

In the Team Explorer Branches view, right-click the branch you want to rebase your current branch changes onto and select Rebase Onto...

From the Git menu on the menu bar, select Manage Branches, right-click the remote branch you want to rebase and select Rebase <current branch> onto <selected branch>


Do an interactive rebase of the last n commits

git rebase -i HEAD~n (Linux and macOS)
git rebase -i "HEAD^n" (Windows)

Not applicable

Not applicable


Cherry-pick a commit into the current branch

git cherry-pick commitID

Open the Changes view in Team Explorer, then choose View history... from the Action drop-down. Locate the commit in the History view, then right-click and select Cherry-pick.

From the Git menu on the menu bar, select Manage Branches, locate the History view, then right-click and select Cherry-pick


For more information, see the following resources:

Undo

Warning

If you are not an experienced Git user, exercise caution when using the reset command. Learn more


How do I?

Git command-line

Visual Studio Team Explorer

Visual Studio Git menu


Revert all changes and roll back to the most recent commit

git reset --hard HEAD

Open the Changes view in Team Explorer. Select Actions and choose View History from the drop-down. Right-click the commit where the branch is currently located and select Reset and Delete changes.....

From the Git menu on the menu bar, select Manage Branches, right-click the commit where the branch is currently located, and select Reset > Delete Changes (--hard)


Revert staging of files, but keep file changes

git reset --mixed HEAD

Open the Changes view in Team Explorer. Select Actions and choose View History from the drop-down. Right-click the commit where the branch is currently located and select Reset and Keep changes....

From the Git menu on the menu bar, select Manage Branches, right-click the commit where the branch is currently located, and select Reset > Keep Changes (--mixed)


Delete untracked files

git clean -f

In the Changes view in Team Explorer, right-click the files to remove under Changes marked with [add] and select Delete

From the Git menu on the menu bar, select Commit or Stash to open Git Changes, right-click the files to remove under Changes marked with A and select Delete


Reset your local branch to the most recent commit on a remote branch

git reset --hard remote/branchname
(for example, git reset --hard origin/main)

Right-click the branch from Team Explorer's Branches view and select Reset and Delete changes....

From the Git menu on the menu bar, select Manage Branches, right-click the branch, and select Reset > Delete Changes (--hard)


Revert a commit pushed to a remote repository

git revert commitID

Open the Changes view in Team Explorer. Select Actions and choose View History from the drop-down. Right-click the commit to revert and select Revert.

From the Git menu on the menu bar, select Manage Branches, right-click a commit in the history, and select Revert