248

I accidentally pushed a staged change in a new branch in Visual Studio 2017 to my local repository. It hasn't been pushed to the remote repository. I want to get rid of it but can't find a way to do this. I rebased from local master branch to the new branch. Then I deleted the new branch. But Outgoing Commits still shows it. How to delete it or revert it?

enter image description here

Tony_Henrich
  • 42,411
  • 75
  • 239
  • 374

9 Answers9

407

Open the history tab in Team Explorer from the Branches tile (right-click your branch). Then in the history right-click the commit before the one you don't want to push, choose Reset. That will move the branch back to that commit and should get rid of the extra commit you made. In order to reset before a given commit you thus have to select its parent.

enter image description here

Depending on what you want to do with the changes choose hard, which will get rid of them locally. Or choose soft which will undo the commit but will leave your working directory with the changes in your discarded commit.

SharpC
  • 6,974
  • 4
  • 45
  • 40
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • 56
    Worth putting in bold the part where ** you have to opt for RESET on the PARENT of the messed up commit ** - bizarrely VS2017, right after you do a commit, offers you a menu option that carries out the reset targetd at the commit you just did (a no-op) - I spent a few mins til I found your post and realised I should have Reset [To] the parent commit.. The menu option being "Reset" is slightly poorly worded - sounds like it will reset a particular commit == implying it should be used on that commit. If it were Reset To I reckon I would have understood it faster – Caius Jard May 01 '18 at 07:55
  • 4
    (@JH) Thanks for the answer. .. atCJ Thank you for the comment. atJH Thank you for bold-ing that part... atEverybody....that's the part I was stuck on....and I mean stuck. #freedom – granadaCoder Aug 22 '18 at 20:35
  • The naming is in line with how git calls the command. If you have edited files, those will get reset as well. If you have staged files, they will get reset as well. That's why there is a reset on the latest commit as well. It resets *to that* commit. – jessehouwing Aug 22 '18 at 20:52
  • Notice: "before the one" – gharel Nov 05 '21 at 17:30
  • Just want to clarify that in VS2019 repo history window I had to mark the previous commit in the Local History to make the reset work properly. – Janspeed Nov 15 '21 at 12:44
137

I could not find a single good answer that helped me get rid of this issue.

Let's say the name of branch, you accidentally committed changes to, is master. Following four simple steps proved like a world to me:

  1. Go to Branches
  2. Choose or create any branch other than master
  3. Delete local/workspace version of master
  4. Switch to master from remotes/origin
Chaos Legion
  • 2,730
  • 1
  • 15
  • 14
  • 14
    This is the answer that works, you don't have to push the `revert` – sojim Nov 29 '18 at 21:41
  • 4
    this works, but you're stuck when you only want to some(not all) specific commits from the Outgoing Commits. – KatariaA Dec 18 '18 at 08:36
  • @KatariaA you may have to take offline backups of the individual files and follow the process written above. Once done, you can replace your concerned files using the offline backups. – Chaos Legion Jan 18 '19 at 04:28
  • 2
    The simplest, best solution if you're happy losing all outgoing commits. – Ian Jul 29 '21 at 11:02
124

TL;DR:

Use git reset --soft HEAD~ in the cmd from the .sln folder


I was facing it today and was overwhelmed that VSCode suggests such thing, whereas its big brother Visual Studio doesn't.

Most of the answers weren't helpful; if I have more commits that were made before, losing them all would be frustrating. Moreover, if VSCode does it in half a second, it shouldn't be complex.

Only jessehouwing's answer was the closest to a simple solution.


Assuming the undesired commit(s) was the last one to happen, Here is how I solved it:

Go to Team Explorer -> Sync. There you'd see the all the commits. Press the Actions dropdown and Open Command Prompt

undesired-commit-solved example

You'll have the cmd window prompted, there write git reset --soft HEAD~. If there are multiple undesired commits, add the amount after the ~ (i.e git reset --soft HEAD~5)


(If you're not using git, check colloquial usage).

I hope it will help, and hopefully in the next version VS team will add it builtin

Tzahi Leh
  • 2,002
  • 1
  • 15
  • 27
  • Also, had to use Git tortoise (or such) to delete the commit. Couldn't do it from VS because the project file didn't have the new files that were added registered. – lionheart Oct 26 '21 at 01:40
  • 1
    I had problems doing this from VS2019 but doing the command sorted it. Not really sure why VS had the problem. – I Bowyer Nov 02 '21 at 09:13
  • 1
    Man, thanks a ton for this! Spent 20 minutes trying to achieve this from VS 2019, did it in 10 seconds from CLI. – Xonshiz Nov 30 '21 at 08:54
  • I renamed my local branch and use the command git reset --soft HEAD~XX where XX is number of unwanted push, that worked well for me. Then I synced and switched to desired branch. Thanks. – Pallavi Kulkarni - Dhepe Jan 28 '22 at 14:46
  • 1
    I'm using VS 2022 and Git in Jan 2023. I had 5 "upcoming" changes, ran this command 5 times and it worked like a charm! Thank you! – Shree Harsha Jan 26 '23 at 15:44
3

You have 2 options here to do that either to discard all your outgoing commits OR to undo specific commit ..

1- Discard all your outgoing commits:

To discard all your outgoing commits For example if you have local branch named master from remote branch, You can:

1- Rename your local branch from master to anything so you can remove it. 2- Remove the renamed branch. 3- create new branch from the master

So now you have a new branch without your commits ..

2- Undo specific commit: To undo specific commit you have to revert the unneeded by:

1- Double click on the unneeded commit. Double click on the unneeded commit 2- Click on revert Click on revert

But FYI the reverted commit will appear in the history of your commits with the revert commit ..

3

Assuming you have pushed most recent changes to the server:

  1. Close Visual Studio and delete your local copy of the project
  2. Open Visual Studio, go to Team Explorer tab, click Manage Connections. (plug)
  3. Click the dropdown arrow next to Manage Connections, Connect to a Project
  4. Select the project, confirm the local path, and click the Connect button.

Once you reopen the project both commits and changes should be zero.

JB4375
  • 31
  • 4
2

Go to the Team Explorer tab then click Branches. In the branches select your branch from remotes/origin. For example, you want to reset your master branch. Right-click at the master branch in remotes/origin then select Reset then click Delete changes. This will reset your local branch and removes all locally committed changes.

2

My solution is insane, but to me the easiest way is to:

  1. Close Visual studio
  2. Open in Visual code
  3. Click undo commit on the commit
  4. Close Visual code
  5. Open again in Visual studio
2

Another possibly insane answer, but worked. I say this only because Git confuses me daily.

For me the outgoing commit was properly merged through a PR to main, and shown correctly in the remote main branch, but my local main showed the same commit as an outgoing needing to be pushed.

  • Can't Push to main...
  • Reset (either hard or mixed) did nothing.

My solution: I deleted the local main branch and then re-checked out the remote main branch.

iGanja
  • 2,374
  • 2
  • 28
  • 32
0

Try to rebase your local master branch onto your remote/origin master branch and resolve any conflicts in the process.