11

I know that you can revert back to a previous commit but it doesn't sound like the history will be gone. How can I revert back to a previous commit and make sure the commits that came after are gone forever?

cbrawl
  • 875
  • 1
  • 9
  • 24

2 Answers2

24

Git reset command can achieve this.

You can run the git reset --hard command to revert back to a previous commit. Then run git push --force command to wipe out all the commits came after this commit on server.

git clone <repo_url>  #clone your azure git repo to local
git checkout <branch>

git reset --hard <commithash> #revert back to a the previous commit
git push --force  #push to remote server

After you run above git commands locally. You will see on azure devops git the commits coming after are gone.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • Works like a charm in Azure Devop. – GLP Oct 22 '21 at 21:04
  • This will still leave a record of the commit at least temporarily from what I have found, it won't display in the GUI but if you have the commit ID you can still access it at https://dev.azure.com/{organization}/{PROJECT}/_git/{REPO}/commit/{COMMITID} . – Nick Kimbrough Mar 20 '23 at 20:36
  • Great. Worked seamlessly for me.. Ps For the uninitiated, the is from the previous commit, which can be obtained from AzDevOps/Repository/History. – David Jones Jul 28 '23 at 13:19
13

While code approach is available, Azure DevOps website provides very quick method.

Go to Azure DevOps -> Your Repository -> Switch to the Working branch where you just made the commit that needs to be changed.

  1. Go to History and click on the commit that needs to be reversed. Select commit to be reversed.

  2. Select "revert" option from hamburger icon at top right. enter image description here

  3. It will automatically create a new branch and will ask you to approve a pull request from this new branch to your working branch. Complete this pull request. enter image description here

  4. Verify that your commit has been reversed in the working branch as expected. enter image description here

Note: At times if there are multiple dependencies on a commit, it might throw an error. But usually, it works fine.

Nirupam Nishant
  • 209
  • 3
  • 10
  • This approach will create a new commit that undoes the modifications you did in a single commit, but the commit that you reverted will STILL exist in the history. Reverting will not "make sure the commits that came after are gone forever", as the OP asked. Resetting (not reverting) will delete the commits from the history, and also allows you to go back multiple commits instead of a single one. – Vinícius Queiroz Jul 11 '23 at 12:52