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?
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:
git push --force
# blow away this branch on the server and replace it with whatever commits are on my local branchgit 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 yetgit 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:
Then check the box for "Enable push --force-with-lease":
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:
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":
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".
--force
you see on the screen is a typo in Team Explorer in both VS2019 and VS2017, it actually does --force-with-lease
:If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With