How to configure Visual Studio to enable git rebase

Software category image

Visual Studio doesn’t support the git rebase workflow by default. I’ll show you the required settings that need to be changed.

The git rebase strategy is commonly used in larger software development projects. One main advantage to the git merge strategy is that if you work with feature branches, all commits of one feature are in line. When traversing the history you will always be in the same feature until back to the beginning. When using the git merge approach, commits of different features can be within your feature which makes it harder to debug in case of errors.

You want your history to look like this (features aren’t divided):

- feature2_commit2
- feature2_commit1
- feature1_commit4
- feature1_commit3
- feature1_commit2
- feature1_commit1

But it should not look like this (features are divided):

- feature1_commit4
- feature2_commit2
- feature1_commit3
- feature2_commit1
- feature1_commit2
- feature1_commit1

A good summary of the approaches (and the git terms in general) can be found in the following article:

When pulling via Visual Studio from a git repository, it will automatically use the git merge pattern by default. We have to change a setting to fix this. If you do a git rebase with a shell and try to push from Visual Studio, the following dialog appears.

Dialog window after a rebase before the changes
Dialog window after a rebase before the changes

When you click on Pull then Push or Pull, a git merge will be performed.

Another problem with Visual Studio is that it doesn’t allow you to push the changes after a git rebase. The commit history is reordered and that requires a push with the argument --force-with-lease which Visual Studio doesn’t allow out of the box. A git merge doesn’t reorder the commit history, so a normal push is sufficient.

Go to ToolsOptionsSource ControlGit Global Settings, set Rebase local branch when pulling to True, and enable the checkbox Enable push --force-with-lease.

Settings in Visual Studio to be changed to support git rebase
Settings in Visual Studio to be changed to support git rebase

On your next failed push after a git rebase, you will be presented a new option Force Push. This is what we want to rewrite the commit history in our favor.

Dialog window after a rebase after the changes
Dialog window after a rebase after the changes

The configuration is completed and should work fine ?

Conclusion

In this article, I showed you how to configure Visual Studio to support git rebase properly. If you keep running into problems, I suggest switching to a command line and executing the git commands from there. The git support of Visual Studio is getting better but is far from being perfect.


Want More Flutter Content?

Join my bi-weekly newsletter that delivers small Flutter portions right in your inbox. A title, an abstract, a link, and you decide if you want to dive in!