Introducing new Git features to Visual Studio 2022
We continue to enhance the Git experience in Visual Studio, and we are excited to announce some long-awaited updates in version 17.1 Preview 2. Download the latest Visual Studio Preview and check out the following new Git features.
|New Git features
|Compare your checked out branch with any local or remote branch.
|Checkout the tip commit or any previous commit of remote and local branches.
|Manage and create new branches on all of your active repositories at the same time.
|Line-staging (interactive staging)
|Split your changes across different commits by staging sections of code changes.
Are you new to Git and source control?
Start utilizing Git and Visual Studio to their full potential by signing up for the Git learning series.
Comparing branches provides an overview of differences between two branches which can be very helpful before creating a pull request, merging, or even deleting a branch.
To compare your currently checked out branch with other branches using Visual Studio, you can utilize the branch picker hosted in the status bar and the Git changes tool window to choose any local or remote branch to compare with. Right click the branch you are targeting and select Compare with Current Branch. Alternatively, you can utilize the branch list on the Git Repository window to access the same command.
Selecting Compare with Current Branch opens the compare branches experience where you can navigate the Changes list and select the file that you would like to compare.
Tip: use your preferred diff layout
If you prefer an inline diff, you can use the diff configuration options gear and switch to an inline diff view.
Checking out a commit can be beneficial in multiple ways. For example, it allows you to go back to a previous point in your repository’s history where you can run or test your code. It can also be helpful if you would like to review code from a remote branch (a colleague’s branch, for example). That way you don’t need to create a local branch if you are not planning on contributing to it. In this case, you can just checkout the tip of the remote branch that you would like to review.
To checkout a previous commit in Visual Studio, open the Git Repository window View > Git Repository, right click on the commit you would like to go back to and select checkout (–detach).
Visual Studio shows a confirmation dialog explaining that by checking out a commit you will be in a detached HEAD state. Meaning that the HEAD of your repository is going to point directly to a commit instead of a branch.
Now that you are in a detached head state, feel free to run and test your code or even explore and commit changes. When you are done exploring and want to go back to your branch, you can choose to discard your changes by checking out an existing branch or choose to keep your changes by creating a new branch first.
Important Tip: to keep your changes, create a new branch before exiting the detached HEAD state
Commits created in a detached head state are not associated with any branch and may be garbage collected by Git after you checkout a branch. That is why to keep your changes, it is recommended to create a new branch before checking out a branch. For example, commits C5 and C6 will be garbage collected if we check out Main without creating a new branch.
To learn more about the detached head state view the following Git documentation: Detached Head
Checking out the tip of a remote branch can be helpful if you would like to quickly review a pull request and evaluate the latest updates. To do that in Visual Studio, first make sure to fetch and get the latest updates from your remote repository Git > Fetch. Then right click on the remote branch you would like to review and select Checkout Tip Commit.
Note: Uncommitted changes might conflict with the commit you are checking out
You can checkout commits and branches when you have uncommitted changes. Visual Studio detects if there are conflicts between uncommitted changes and the commit/branch you are checking out and provides an option to keep your changes by reapplying them after the checkout.
During this release we continued improving the multi-repo support preview feature we introduced in Visual Studio 2022 Preview 3 by enhancing the inner loop branching experience. The easiest way to enable multi-repo support is to use CTRL+Q, type “preview” and open the preview features pane. Scroll to “Enable multi-repo support” and toggle the checkbox.
Note: Open a solution with projects hosted on different Git repositories to get started
Once you enable the multi-repo feature flag and open a solution File > Open > Project/Solution with projects hosted on different Git repositories, Visual Studio will automatically activate up to 10 repositories at the same time.
The branch pickers on both the status bar and the Git Changes tool window now support multi-repo in Visual Studio. You can utilize them to quickly switch between branches and do lightweight branch management across all of your active repositories. To quickly switch between branches of any active repository, expand the repository tree on the branch picker and click on the branch you would like to checkout.
You can use both branch pickers for lightweight branch management across your active repositories. Common inner loop branching operations including merge, rebase, rename, delete and compare branches are accessible by right clicking branches on this list. For more advanced repository and branch management operations please use the Git Repository window.
A popular use case for this lightweight branch management experience is to include updates from different remote branches. For example, to merge changes from a colleague’s branch, right click on the branch under the Remotes tab and select Merge into Current Branch.
The create new branch experience now supports creating branches across all active repositories. All you need to do is provide a branch name and click the Create branches button to create the same new branch on all active repositories! You can also choose to create your new branch on a subset of active repositories by utilizing the checkboxes.
If you need your new branches to be based on branches other than your currently checked out branches, you can utilize the branch drop-down to do that. For example, let’s say we need our new branch to be based on the subtract_feature branch on the MathLib repo and based on a different branch on the Multi-rep Calculator repo. In this case, we would use the branch drop-down to select a different branch under the Multi-rep Calculator repo.
Line-staging (interactive staging)
Line-staging support, a.k.a. interactive staging is one of our most popular Git suggestion tickets. Line-staging can be helpful when you need to split changes across different commits. This preview includes few of the Line-staging features that we are still working on enhancing. The easiest way to enable this early version of line-staging support is to use CTRL+Q, type “preview” and open the preview features pane. Scroll to “Enable line-staging support” and toggle the checkbox.
Note: line-staging is still a preview feature
This functionality is still a preview feature, which means we are working hard to add more support in the coming releases. In the meantime, we’re depending on your feedback, the community, to build what you need
Once you switch on the line-staging preview flag and restart your Visual Studio, you can start staging chunks of your changes by clicking on files in the Git Changes window. Then hover over the sections of code you would like to stage and click Stage Change.
Tip: use line-staging with your preferred diff layout
Line-staging is supported on both inline and side-by-side diff modes:
This early version of line-staging support has a number of known issues and limitations.
- Missing the ability to stage specific lines within chunks or sections of changes
- Can’t stage lines at the first and last three lines of code in documents
- Ability to stage lines and chunks of code directly from the editor
- Ability to un-stage lines and chunks of code
- Refresh latency enhancements
Let us know what you think
We have benefitted greatly from all the rich feedback we’ve received from you – thank you! We hope you’ll help us continue to improve by giving the new multi-repo preview feature a try and letting us know what you think by taking the following quick survey: