[git] How to remove an unpushed outgoing commit in Visual Studio?

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

This question is related to git visual-studio visual-studio-2017

The answer is


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.

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.


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

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 it's big brother Visual Studio doesn't.

Most of the answers were 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


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.


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


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.


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 ..


I fixed it in Github Desktop Application by pushing my changes.


Questions with git tag:

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to know the git username and email saved during configuration? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 fatal: ambiguous argument 'origin': unknown revision or path not in the working tree HTTP Basic: Access denied fatal: Authentication failed npm notice created a lockfile as package-lock.json. You should commit this file Do I commit the package-lock.json file created by npm 5? Abort a Git Merge key_load_public: invalid format git - remote add origin vs remote set-url origin Visual Studio 2017 - Git failed with a fatal error Get git branch name in Jenkins Pipeline/Jenkinsfile Changing the git user inside Visual Studio Code How to compare different branches in Visual Studio Code Git checkout - switching back to HEAD Clear git local cache Deleting a local branch with Git Rebuild Docker container on file changes Cloning specific branch How to add chmod permissions to file in Git? Git copy changes from one branch to another Git merge with force overwrite Project vs Repository in GitHub How to add a file to the last commit in git? Getting permission denied (public key) on gitlab Delete commit on gitlab gpg failed to sign the data fatal: failed to write commit object [Git 2.10.0] Remove a modified file from pull request Updates were rejected because the tip of your current branch is behind its remote counterpart Can't push to the heroku How to discard local changes and pull latest from GitHub repository In Visual Studio Code How do I merge between two local branches? error: RPC failed; curl transfer closed with outstanding read data remaining Change drive in git bash for windows Checkout Jenkins Pipeline Git SCM with credentials? How to fix git error: RPC failed; curl 56 GnuTLS Trying to pull files from my Github repository: "refusing to merge unrelated histories" Visual Studio Code how to resolve merge conflicts with git? merge one local branch into another local branch Can't push to remote branch, cannot be resolved to branch

Questions with visual-studio tag:

VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to download Visual Studio Community Edition 2015 (not 2017) Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio Code pylint: Unable to import 'protorpc' Open the terminal in visual studio? Is Visual Studio Community a 30 day trial? How can I run NUnit tests in Visual Studio 2017? Visual Studio 2017: Display method references Visual Studio 2017 errors on standard headers Where is NuGet.Config file located in Visual Studio project? Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state Visual Studio 2017 - Git failed with a fatal error Unity Scripts edited in Visual studio don't provide autocomplete Equivalent to AssemblyInfo in dotnet core/csproj How to enable C++17 compiling in Visual Studio? Nuget connection attempt failed "Unable to load the service index for source" Change language of Visual Studio 2017 RC Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies Visual Studio Code how to resolve merge conflicts with git? Getting "project" nuget configuration is invalid error "Object doesn't support property or method 'find'" in IE localhost refused to connect Error in visual studio Why is the Visual Studio 2015/2017/2019 Test Runner not discovering my xUnit v2 tests Build error, This project references NuGet Visual Studio Code always asking for git credentials Stuck while installing Visual Studio 2015 (Update for Microsoft Windows (KB2999226)) Visual Studio error "Object reference not set to an instance of an object" after install of ASP.NET and Web Tools 2015 C# - How to convert string to char? Multiple Errors Installing Visual Studio 2015 Community Edition Why Visual Studio 2015 can't run exe file (ucrtbased.dll)? Where can I read the Console output in Visual Studio 2015 Go To Definition: "Cannot navigate to the symbol under the caret." CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found How to remove error about glyphicons-halflings-regular.woff2 not found NuGet Packages are missing Visual Studio Community 2015 expiration date How to solve ERR_CONNECTION_REFUSED when trying to connect to localhost running IISExpress - Error 502 (Cannot debug from Visual Studio)? Where is the visual studio HTML Designer? error C4996: 'scanf': This function or variable may be unsafe in c programming What are the differences between Visual Studio Code and Visual Studio? Is Xamarin free in Visual Studio 2015? What's the difference between Visual Studio Community and other, paid versions? Visual Studio 2015 installer hangs during install? How to use class from other files in C# with visual studio? 'cannot find or open the pdb file' Visual Studio C++ 2013 How to build x86 and/or x64 on Windows from command line with CMAKE? Python Pip install Error: Unable to find vcvarsall.bat. Tried all solutions How to enable C# 6.0 feature in Visual Studio 2013?

Questions with visual-studio-2017 tag:

Assets file project.assets.json not found. Run a NuGet package restore How to remove an unpushed outgoing commit in Visual Studio? The current .NET SDK does not support targeting .NET Standard 2.0 error in Visual Studio 2017 update 15.3 Create Setup/MSI installer in Visual Studio 2017 VS 2017 Metadata file '.dll could not be found Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error Visual Studio 2017 does not have Business Intelligence Integration Services/Projects How can I run NUnit tests in Visual Studio 2017? How to integrate SAP Crystal Reports in Visual Studio 2017 Unit Tests not discovered in Visual Studio 2017 Visual Studio 2017: Display method references Visual Studio 2017 errors on standard headers Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Where is NuGet.Config file located in Visual Studio project? How can I install the VS2017 version of msbuild on a build server without installing the IDE? Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state Visual Studio 2017 - Git failed with a fatal error How to download Visual Studio 2017 Community Edition for offline installation? Class Diagrams in VS 2017 Equivalent to AssemblyInfo in dotnet core/csproj No templates in Visual Studio 2017 Reportviewer tool missing in visual studio 2017 RC Change language of Visual Studio 2017 RC Predefined type 'System.ValueTuple´2´ is not defined or imported Visual Studio 2015 or 2017 does not discover unit tests Force uninstall of Visual Studio How can I fix the Microsoft Visual Studio error: "package did not load correctly"?