次の方法で共有


Save your work with commits

TFS 2017 | TFS 2015 | TFS 2013

Visual Studio 2019 | Visual Studio 2017 | Visual Studio 2015

Git doesn't automatically take snapshots of your work as you modify files in your repo. You tell Git what file changes you want to capture in a snapshot by staging specific changes. After staging, you save the snapshot by making a commit.

This article provides procedures for the following tasks:

  • How Git tracks changes
  • What's in a commit
  • How to stage your changes
  • How to create a commit
  • How to update your last commit

For an overview of the Git workflow, see Azure Repos Git tutorial.

How Git tracks changes

As you work in your repo, Git tracks changes to all tracked files. Tracked files are files that are currently staged or are included in the previous commit. Git separates tracked files into three categories:

  • Unmodified files - The files you haven't changed since your last commit.
  • Modified files - The files you've changed since your last commit but haven't staged for the next commit.
  • Staged files - The files you've changed since your last commit and staged for the next commit.

Screenshot showing the lifecycle of files in your repo between the three states.

When you create a commit, only staged files and unmodified files are used for the snapshot. Unstaged changes to the modified files are kept, but the snapshot contains the unmodified version of those files. Git doesn't track changes to new files or include new files in the snapshot until you stage them.

Commits are created in your local Git repo. Each commit doesn't have to be perfect, and it might take several commits to accomplish an intended change. Create commits as you work, and when you're done you can push your commits to a remote repo to share your work with others.

What's in a commit

Each commit includes the following information:

  • A snapshot of all tracked files in your repo at the time of the commit. A snapshot isn't the difference between commits, although Git uses snapshots to compute the difference. Snapshots enable fast switching between branches and support merging branches.
  • A reference to the parent commit(s). Most commits have one parent, but the next commit after a branch merge has multiple parents and the first commit in a repo has none.
  • A message describing the changes in the commit. You enter the message when you create the commit.

Git uses the snapshot and parent reference(s) of each commit to maintain a complete record of development in the repo. To investigate changes in your code, you can review the Git history of your repo.

How to stage your changes

To create a snapshot for a commit:

  • Stage new files to let Git know you want them added to the snapshot, and you want Git to track changes to those files going forward.
  • Stage edited files to let Git know you want the modified file version in the snapshot, not the unmodified file version.
  • Stage deleted files to let Git know you want them removed from the snapshot and no longer tracked.

To exclude temp files, log files, or other files from your snapshot, you can configure Git to ignore specific files.

Note

Git supports interactive staging of edited files so you can choose to stage specific changes within a file. This is a useful feature when you want different file edits in different commits.

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 use Git features from either interface interchangeably. Below, we provide a side-by-side comparison of how to stage file changes.

Visual Studio Git

  1. In the Git Changes window, right-click a file in the Changes section and choose Stage to add it into the Staged Changes section.

Screenshot of the Changes option in the 'Git Changes' window in Visual Studio 2019.

Or, you can stage a changed file by selecting the plus sign next to the file. To stage all changed files in a folder, select the plus sign next to the folder. To stage all changed files in your repo, select the plus sign in the top-right corner of the Changes section.

Visual Studio Team Explorer

  1. In Team Explorer, select the Home button and choose Changes.

Screenshot of the Changes option in Team Explorer in Visual Studio 2019.

  1. In the Changes view, right-click a file in the Changes section and choose Stage to add it into the Staged Changes section.

Screenshot of context menu with the 'Stage' option for files in the 'Change' view of Team Explorer in Visual Studio 2019.

Or, you can stage a changed file by dragging it from the Changes section to the Staged Changes section. To stage all changed files in a folder, right-click the folder and choose Stage. To stage all changed files in your repo, select the plus sign in the top-right corner of the Changes section.

You can tell Git to ignore a file by right-clicking it and selecting Ignore this local item or Ignore this extension. Either command creates a .gitignore file in your repo if it doesn't exist, and adds an entry to it. Ignored files won't appear in the Changes section in Visual Studio. However, the .gitignore file has no effect on tracked files. For information on how to configure Git to ignore tracked files, see Ignore files. To save time, you can download .gitignore templates for various development environments from the GitHub gitignore repo.

Note

Visual Studio 2019 doesn't support staging partial changes within a file. Visual Studio 2022 introduces that feature.

How to create a commit

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 use Git features from either interface interchangeably. Below, we provide a side-by-side comparison of how to create a commit.

Visual Studio Git

In the Git Changes window, enter a message that describes your staged changes and then select Commit Staged.

Screenshot showing the commit information link in Visual Studio 2019.

Select the commit link for commit details.

Screenshot showing the commit details link the 'Git Changes' window in Visual Studio 2019.

Visual Studio Team Explorer

In the Changes view of Team Explorer, enter a message that describes your staged changes and then select Commit Staged.

Screenshot of commit message text and 'Commit Staged' button in Visual Studio 2019.

Select the commit link for commit details.

Screenshot showing the commit details link in 'Team Explorer' in Visual Studio 2019.

Note

If all your changes are unstaged, you can skip staging and directly commit by choosing Commit All.

Visual Studio Git

Screenshot of the 'Commit All' option in the 'Git Changes' window in Visual Studio 2019.

Visual Studio Team Explorer

Screenshot of the 'Commit All' option in the  'Changes' view of Team Explorer in Visual Studio 2019.

How to update your last commit

Git supports changing the staged files or message of your last commit. This operation replaces your last commit with a new commit that combines the staged files from both commits and uses the new commit message. Amending a commit is useful if you forgot to stage a file, or your last commit message has a typo.

Warning

Don't amend an already pushed commit because that will cause sync issues with your remote repo. For a pushed commit, use one of these strategies:

  • Create and push another commit that fixes the issues caused by the prior commit.
  • Undo the prior commit that was pushed, by using git revert to create a new commit that reverts all changes made by the prior commit. Then push the new commit.

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 use Git features from either interface interchangeably. Below, we provide a side-by-side comparison of how to amend a commit.

Visual Studio Git

In the Git Changes window, optionally stage one or more files, enter a commit message, select Amend, and then choose Commit Staged.

Screenshot showing the 'Amend Previous Commit' option in the 'Git Changes' window of Visual Studio 2019.

The Git Changes window supports amending either the commit message, staged files, or both. When you select Amend, the identifier SHA for the previous commit is displayed.

Visual Studio Team Explorer

In the Changes view of Team Explorer, stage one or more files, enter a commit message, and then choose Actions > Amend Previous Commit.

Screenshot showing the 'Amend Previous Commit' option in the 'Changes' view of Team Explorer in Visual Studio 2019.

Team Explorer doesn't support amending the commit message without also amending one or more staged files.

Next steps