Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you force push in Visual Studio 2019?

Note: the title of this question has been changed based on the intent of updating the remote after performing git reset --hard. For context, the original title of this question was:

How do I reset to a prior Git commit in Visual Studio and get the remote branch to reflect the changes?

I know this has been asked before, but I still can't get things working.

There are two commits that I need to get rid of. So the from the top of the tree I have: BadCommit 1 BadCommit 2 GoodCommit

In VS 2019, I right-click on GoodCommit -> Reset -> Delete Changes

It resets to GoodCommit locally. But if I try to push this to the repository, I get a message saying that the local branch is behind the remote branch. If I pull and push, I just get right back where I started from with the 2 BadCommits on top.

How do I get the remote branch back to the GoodCommit?

like image 200
Rico Avatar asked Dec 30 '22 15:12

Rico


1 Answers

Anytime you are replacing commits on the remote, you can't just push, you need to force push. Below I will explain how to force push from Visual Studio 2019 (both the new way and old way which includes VS 2017), and the command line. I'll start with the command line in order to explain the different ways you can force push:

  1. git push --force # blow away this branch on the server and replace it with whatever commits are on my local branch
  2. git push --force-with-lease # blow away this branch on the server and replace it with whatever commits are on my local branch, but only if there are no new commits on the branch that I haven't fetched yet
  3. git push --force-if-includes # blow away this branch on the server and replace it with whatever commits are on my local branch, but only if I have the remote tip commit in my reflog. This is a relatively new option, and is described in more detail here.

I highly recommend using #2 most of the time, and that is also what Visual Studio does when you force push within VS. To enable force push in Visual Studio 2019, go to the main Git menu, choose Settings (or Tools menu, Options), select Source Control, then Git Global Settings:

VS2019 Git Settings

Then check the box for "Enable push --force-with-lease":

How to enable Force Push

Once you have enabled the setting, the next time you try to push and your branch has diverged, you will be presented with a new option:

Force push option displayed

Clicking the "Force Push" button is identical to running the command line:

git push --force-with-lease

Note: The Git menu in VS2019 is new. If you don't see it, and you want to try it, you can enable it from Tools->Options, select Environment, Preview Features, then check the box for "New Git user experience":

How to enable the new Git Menu

Similarly, if you have it checked and don't like it, unchecking that box will set Git up like how it used to be in Team Explorer in Visual Studio 2017.

If you don't have the Git menu enabled (or you are using VS 2017), then the force push setting can be found in Team Explorer, Settings, Git Global Settings, then check the box for "Enable push --force".

Note the --force you see on the screen is a typo in Team Explorer in both VS2019 and VS2017, it actually does --force-with-lease:

enter image description here

like image 180
TTT Avatar answered Jan 05 '23 14:01

TTT