If the branch was already pushed and shared, the safest way to go back 5 commits without rewriting repo history is to revert the unwanted commits, not reset or rebase.
Recommended for a shared Azure Repo
- Find the commit IDs:
git log --oneline - Identify the last good commit from 5 commits ago.
- Revert the commits after that point, one by one, using:
This creates new commits that undo the changes while keeping history intact.git revert <commit ID> - If Git reports conflicts, resolve them, then run:
Or cancel with:git revert --continuegit revert --abort - Push the new revert commits to Azure Repos.
Visual Studio 2022
- Go to Git > View Branch History.
- In History, right-click each commit to undo.
- Select Revert.
If the branch is not shared A reset is possible, but it changes branch history.
- Keep later changes as local unstaged changes:
git reset --mixed <commit ID> - Discard all changes after that commit:
git reset --hard <commit ID>
git reset --hard can permanently discard work. Do not use reset to an older commit on a branch that has already been pushed and shared with others, because local and remote history will no longer match.
About force push / rebase Rebase and force push can overwrite the server branch, but this is risky and can cause lost work. It also requires Force push or Admin permissions on the repository. This approach should be used with caution.