Azure Devops Repos - Revert back to a previous commit like the recent ones never existed
Asked Answered
S

4

16

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?

Shorthand answered 17/12, 2020 at 23:40 Comment(0)
A
28

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.

Aday answered 18/12, 2020 at 4:5 Comment(4)
Works like a charm in Azure Devop.Floatage
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 dev.azure.com{organization}/{PROJECT}/_git/{REPO}/commit/{COMMITID} .Caper
Great. Worked seamlessly for me.. Ps For the uninitiated, the <commithash> is from the previous commit, which can be obtained from AzDevOps/Repository/History.Morra
Where can you enter Git commands in DevOps?Nettle
S
19

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.

Smackdab answered 20/1, 2022 at 8:36 Comment(2)
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.Randirandie
"Encountered conflicts when reverting commit XYZ. This operation needs to be performed locally" :/Duplicator
D
0

Git reset --hard is not safe to run on public branches. It will necessitate a force push, and anyone else that pulled the branch in between the bad commit and the force push will run into issues.

The recommended approach is the revert button in ADO as referenced in another comment. If that fails, this can also be done locally in similar fashion. If you know the hash, you can simply run git revert <yourhash>, and it will do the same revert that ADO does. After this, it's the same push process that you would do anywhere else.

Dacron answered 20/3, 2024 at 20:24 Comment(0)
D
0

How to Remove a Pushed Commit from a Remote Repository

  1. Check Out to a New Branch (Optional):

    git checkout -b temp-branch
    
  2. Identify the Commit Hash:

    git log
    
  3. Reset to the Previous Commit:

    git reset --hard <previous_commit_hash>
    
  4. Force Push the Changes:

    git push origin <branch_name> --force
    

this might need an authentication with you | happened with me in Azure Devops.

Force pushing rewrites the commit history on the remote repo. This can be confusing or cause problems for others working on the same branch. Make sure to communicate with your team before doing this.

Diandrous answered 25/7, 2024 at 6:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.