Save work with commits
VSTS | TFS 2018 | TFS 2017 | TFS 2015 | VS 2017 | VS 2015 Update 2
Git does not automatically snapshot your code as you make edits to files in your repo. You must tell Git exactly which changes you want to add to the next snapshot by staging those changes. After staging your changes, create a commit to save the snapshot to your repo.
In this tutorial you learn:
- 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
How Git tracks changes
Git tracks file changes in your repo as you work, and separates the files in your repo into three categories:
- Unmodified files – These files haven’t changed since your last commit.
- Modified files – These files have changes since your last commit, but you haven't yet staged for the next commit.
- Staged files – These files have changes that will be added to the next commit.
When you create a commit, only the staged changes and unchanged files are used for the snapshot. Changes to unstaged but modified files are kept, but the commit uses the unmodified version from the previous commit in its snapshot.
Commits are created in your local Git repository, so you don't have to worry about your changes being perfect. Continue to create commits as you work, pushing your changes to the team when they are ready to share.
What's in a commit
Commits include the following information:
- A snapshot of the files saved in the commit. Git snapshots the contents of all files in your repo at the time of the commit—this makes switching versions very fast and helps Git merge changes.
- A reference to the parent commit(s). Commits with multiple parents occur when branches are merged together.
- A short and to the point message describing the changes in the commit. You enter this message when you create the commit.
Git uses the references between commits along with the file snapshots to maintain a complete record of development in your repo.
Stage your changes
Git does not automatically add changed files to the snapshot when you create a commit. You must first stage your changes to let Git know which updates you want to add to the next commit. Staging lets you to selectively add files to a commit while excluding changes made in other files.
Ignore temp files, logs, and other files that might change on your local machine but you don't want to add to version control.
Visual Studio 2015 (Update 2) and 2017
Visual Studio keeps track of file changes to your project as you do your work. When you are ready to stage changes, open up the Changes view in Team Explorer.
Stage individual file changes by right-clicking a file in the Change view and selecting Stage. Staging a change creates a Staged Changes section in Team Explorer. Only changes in the Staged Changes section are added to the next commit.
The Team Explorer Changes view had Included Files and Excluded Files sections before Visual Studio 2015 Update 2. The Changes view was updated in Visual Studio 2015 Update 2 for better compatibility with other Git tools.
Stage multiple files or folders by selecting them then right-clicking and choosing Stage or by dragging and dropping files from the Changes list into the Staged Changes list.
Ignore files by right-clicking and selecting Ignore this local item or Ignore this extension. This adds an entry to the the file to the .gitignore file in your local repo. If the ignored file was added to your repo in an earlier commit, ignoring the file will not remove it from the Changes list. See excluding and ignoring files section for more information on how to ignore files already tracked by Git.
Create a commit
Open the Changes view in Team Explorer.
Enter a commit message describing your changes and select Commit Staged to create a new commit that includes the changes listed in the Staged Changes section.
Skip staging files if you just want to commit all changes listed by entering a commit message and selecting Commit All when you have no staged changes.
Update your last commit
Amend your last commit to correct small errors without making a new commit.
- Open the Changes view in Team Explorer and stage your updated changes. You can amend just the commit message by leaving the staged changes empty.
Enter the updated commit message and select Amend Previous Commit from the Actions drop-down.