Compartilhar via


Save your work with commits

TFS 2018

Visual Studio 2019 | Visual Studio 2022

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 2022 provides a Git version control experience by using the Git menu, Git Changes, and through context menus in Solution Explorer. Visual Studio 2019 version 16.8 also offers the Team Explorer Git user interface. For more information, see the Visual Studio 2019 - Team Explorer tab.

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.

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.

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

Starting with Visual Studio 2022 version 17.3, Visual Studio supports staging partial changes within a file. For more information, see Stage lines of code in Visual Studio.

How to create a commit

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.

Select the commit link for commit details.

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

Note

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

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

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.

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.

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.

Next steps