We’ve all been there, what happens when you commit changes to the master branch and during the sync to the remote you get an error.
Starting point: you didn’t check the branch you where committing to:
So during the sync you get an error:
In the output window this message is shown:
Pushing to https://**********.visualstudio.com/DefaultCollection/CICD/_git/DemoRepo To https://**********.visualstudio.com/DefaultCollection/CICD/_git/DemoRepo Error: failed to push some refs to 'https://**********.visualstudio.com/DefaultCollection/CICD/_git/DemoRepo' Error encountered while pushing to the remote repository: rejected master -> master (TF402455: Pushes to this branch are not permitted; you must use a pull request to update this branch.) This means the remote branch is protected and that you can only update it via a Pull Request.
You changes will not be lost! Here’s what to do:
Step 1. Move your changes to a new branch, by creating a new branch from here:
(note the master branch as the source)!
Step 2. If you switch back to the master branch, you’ll still see those commits on it and you still cannot push…
We need to reset the commit from the master branch. There is no way to do this with Visual Studio, so we need to use the command line for that.
Open a command line and go to your repository folder. I’m using Windows Terminal here.
git status in the folder to check if you are in the correct location and state:
To reset the last N commits, use this command:
git reset --hard HEAD^N
As an example: to reset the last 2 commits:
git reset --hard HEAD^2
Be aware: this is a destructive operation: data from the master branch in those commits will be lost! That’s why we saved them to another branch.