Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
Visual Studio 2019 | Visual Studio 2022
Git uses commit metadata like parent links, author details, and timestamps to track the history of changes in a repo. You can review the Git history to find out when files changed, who changed them, and what changes were made.
When people create and merge feature branches into a target branch using pull requests, the development history of the target branch might not be a straight chronological line. So, when you review the history of changes to a file on the target branch, keep in mind that the order of commits is influenced by merge strategy and merge date, not just the original date of the changes. For example, the most recent commit on the main branch may introduce a change that was made weeks ago in a feature branch that was only just merged into the main branch using a three-way merge.
When you want to figure out how and when a particular file change occurred, you might need to compare different versions of the same file from different commits, possibly in different branches.
The Azure DevOps team project site lets you compare two versions of the same file from commits in the same branch, but doesn't support comparing file versions across branches.
From your web browser, open the team project for your Azure DevOps organization. In the Repo > Files view, select a file and choose the Compare tab.
In the Compare tab, choose the two commits that contain the file versions you want to compare. The diff view shows any new, deleted, or modified file lines.
Note
GitHub lets you compare two versions of the same file from different commits across different branches. To compare, append /compare/<commit1>..<commit2> to your GitHub repo URL to navigate to the comparison page. The comparison page contains a diff view of each file that differs. For more information on commit comparison in GitHub, see Comparing 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.
Visual Studio lets you compare two versions of the same file in the same branch, but doesn't support comparing file versions across branches.
In Solution Explorer, select a file and choose Git > View History from the file's context menu to open the Git History tab for the selected file.
In the Git History tab, choose Compare with Previous from a commit's context menu to open a Diff tab that compares the selected commit with the preceding commit.
Or, select two commits and choose Compare from either commit's context menu to open a Diff tab that compares the two selected commits.
The Diff tab shows new, deleted, or modified file lines.
Visual Studio 2019 provides a Git version control experience by using the Git menu, Git Changes, and through context menus in Solution Explorer.
Visual Studio lets you compare two versions of the same file in the same branch, but doesn't support comparing file versions across branches.
In Solution Explorer, select a file and choose Git > View History from the file's context menu to open the Git History tab for the selected file.
In the Git History tab, choose Compare with Previous from a commit's context menu to open a Diff tab that compares the selected commit with the preceding commit.
Or, select two commits and choose Compare from either commit's context menu to open a Diff tab that compares the two selected commits.
The Diff tab shows new, deleted, or modified file lines.
Team Explorer doesn't provide support for this feature.
The git diff command can compare different versions of the same file from different commits across different branches. The git log command can help you identify the commits that contain the file versions you want to compare.
Use git log and specify a file to list the commits that changed the file:
git log <file>
By default, the command output starts with the most recent commit in the current branch, and then iterates backward through ancestor commits (regardless of branch) by following the parent links in each commit's metadata.
Here's an example of output for the command git log index.html:
The output shows that one line was deleted and one line was added.
Limit Git log output
To limit] the commits that git log lists, you can filter by author, date, message, changed content, and more. For example:
git log --author=frank@fabrikam.com index.html only lists commits by the specified author.
git log --since="2022-5-1" only lists commits created after the specified date.
git log --before="yesterday" only lists commits created before the specified relative date.
git log --grep="css change" only lists commits with the specified text in their message.
git log -S"myVariable" only lists commits that introduce or remove the specified string.
git log -G"myVar.*" only lists commits that introduce or remove the specified regex string.
git log -3 only lists the last three commits.
Format Git log output
You have several format options for the commit list. For example:
git log --abbrev-commit lists commits using an abbreviated ID (SHA-1 checksum).
git log --oneline lists each commit in a single-line abbreviated format.
git log --patch index.html lists each commit together with a diff of the changes.
Restore files
You can restore a specific version of a file from Git history, even if the file was edited, deleted, or renamed in a later commit. Restoring an older version of a file doesn't create a new commit with the change. To update your branch with the restored file version, you'll need to commit the change.
The Azure DevOps team project site lets you revert all changes made by a specific commit, but doesn't support reverting changes to a specific file within the commit.
Visual Studio lets you compare two versions of the same file in the same branch, but doesn't support comparing file versions across branches.
In Solution Explorer, select a file and choose Git > View History from the file's context menu to open the Git History tab for the selected file.
In the Git History tab, choose Compare with Previous from a commit's context menu to open a Diff tab that compares the selected commit with the preceding commit.
Or, select two commits and choose Compare from either commit's context menu to open a Diff tab that compares the two selected commits.
The Diff tab shows new, deleted, or modified file lines.
Visual Studio lets you compare two versions of the same file in the same branch, but doesn't support comparing file versions across branches.
In Solution Explorer, select a file and choose Git > View History from the file's context menu to open the Git History tab for the selected file.
In the Git History tab, choose Compare with Previous from a commit's context menu to open a Diff tab that compares the selected commit with the preceding commit.
Or, select two commits and choose Compare from either commit's context menu to open a Diff tab that compares the two selected commits.
The Diff tab shows new, deleted, or modified file lines.
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.
In Solution Explorer, select a file and choose Git > View History from the file's context menu to open a Git History tab for the selected file.
In the Git History tab, select a commit and choose View Commit Details from the commit's context menu to open the Commit Details view.
In the Commit Details view, select the file and choose Open from the file's context menu to open the previous version of the file in a new tab.
Choose File > Save As from the menu bar to save the restored version of the file.
You can use the git checkout or git show commands to restore a specific version of a file from Git history.
git checkout reverts a file to a previously committed version if you specify the file and a commit:
git checkout <commit> <file>
For example, git checkout 85435fac src/app.ts will revert the src/app.ts file to its version in commit 85435fac.
git show prints the contents of a previously committed file version, which you can redirect to an output file:
git show <commit>:<file> > <output file>
For example, git show 85435fac:src/app.ts > /archive/oldapp.ts will write the contents of app.ts in commit 85435fac to /archive/oldapp.ts.
You can compare any local or remote branches to review the changes that will result from a merge or rebase. Branch comparison lets you check for merge conflicts and see how changes by others might affect your work.
Visual Studio 2019 and earlier versions don't support branch comparison, so if you're using one of those versions you can compare branches on the Git command line or using your web browser—if your repo is hosted in Azure Repos or GitHub. Visual Studio 2022 supports branch comparison, as described in Compare branches.
From your web browser, open the team project for your Azure DevOps organization. In the Repos > Branches view, select the ellipsis for any branch and choose Compare branches to open the Branch compare view.
In the Branch compare view, choose the two branches that you want to compare. Select the Files tab for a diff view of the new, deleted, or modified lines in each changed file.
Note
GitHub supports branch comparison. To compare two branches, append /compare/<branch1>...<branch2> to your GitHub repo URL to navigate to the comparison page. The comparison page contains a diff view of each file that differs. For more information on branch comparison in GitHub, see Comparing branches.
To compare a branch with the current branch, right-click a branch in the Branches pane of your repository, and then select the compare option. The context menu specifies the names of the current and the target branches:
Visual Studio 2019 doesn't support branch comparison. However, you can compare branches on the Git command line or using your web browser—if your repo is hosted in Azure Repos or GitHub.
Tip
You can access the web portal from the Team Explorer Home view by choosing Web Portal.
Visual Studio 2019 doesn't support branch comparison. However, you can compare branches on the Git command line or using your web browser—if your repo is hosted in Azure Repos or GitHub.
Tip
You can access the web portal from the Team Explorer Home view by choosing Web Portal.
To compare any two local or remote branches, you can use the Git diff command specifying the branch names:
git diff <branch1> <branch2>
Git compares the commit at the tip of one branch with the commit at the tip of the other. The diff output will show the deletions and additions between each file in the two branches.
Here's an example of output for the command git diff users/frank/feature origin/main, which compares a local branch with a remote branch:
index 36843b8..03afc4b 100644
--- a/tsapp/index.html
+++ b/tsapp/index.html
@@ -4,7 +4,7 @@
<head>
<meta charset="utf-8" />
<title>TypeScript HTML App</title>
- <link rel="stylesheet" href="fabrikam-test.css" type="text/css" />
+ <link rel="stylesheet" href="fabrikam.css" type="text/css" />
<script src="app.js"></script>
</head>
...
--- a/tsapp/app.ts
+++ b/tsapp/app.ts
constructor(element: HTMLElement) {
this.element = element;
- this.element.innerHTML += "The time is: ";
+ this.element.innerHTML += "The time is now: ";
this.span = document.createElement('span');
this.element.appendChild(this.span);
this.span.innerText = new Date().toUTCString();
To narrow down comparison to a specific file, specify the file in the diff command:
git diff <branch1> <branch2> <file>
For example, git diff users/frank/feature origin/main index.html will only generate a diff for the index.html file.