1

I made a small commit and pushed it to our main branch in Azure DevOps. I realized that this commit was obsolete, as it was based on wrong assumptions and miscommunication with my colleague, who made that last commit to the main.

However, I decided to revert my commit via a pull request, but I must have done it wrong. I reverted that revert, and I am basically back to the status of my original commit - which in a next step I would revert again (this time "right").

So the code base and all files would be fine in the main branch afterwards, it's just that our Azure DevOps history looks quite 'messy'. E.g. the commit line (graphical representation of commits with lines and dots).

I am quite aware that one of the purposes of using Git is full transparency, but we are a small team and communicate very open and agreed, that if possible we would like to get rid of that little 'intermezzo' of mine, if possible.

Is there any way to achieve this?

5
  • 1
    you can use reset + push --force-with-lease Commented Jul 8, 2024 at 13:49
  • If you have a really small team and care more about a nice history than accountability, you can try this: tag the last good commit on the main branch, delete (or rename) the main branch and push to server, recreate the main branch at the tag location and push. Tell the team to pull. Commented Jul 8, 2024 at 14:19
  • I doubt the azure tag is relevant here. If this question comes down to simply reset and force push, then I don't think the azure-devops tag is relevant either. (Unless you need additional help with bypassing policies temporarily for the force push.) Commented Jul 9, 2024 at 4:38
  • @TTT removed the azure tag. kept the azure-devops for now, because I was not sure, if I can achieve the target state without doing something in dev.azure.com, but purely via git commands. Commented Jul 9, 2024 at 8:50
  • dani-vta's answer should work. Alternately you can try to remove the entire messed commit by referencing this ticket : stackoverflow.com/questions/59837187/… Commented Jul 12, 2024 at 9:40

1 Answer 1

3

To get rid of one or more commits from the commit history, you could use git reset --hard or git rebase --interactive and then push force to the remote repository.

In the first case, you would reset your repository to a specific commit, dropping the last n commits. In your case, the last three commits.

# make sure to be on the right branch
git checkout my_branch

# resetting to the third to last commit
git reset --hard HEAD~3

# or alternatively, resetting to a specific commit via its SHA1
git reset --hard <SHA1-third-to-last-commit>

# aligning the remote's branch' history and tip to the local branch
git push --force

In the second case, you could perform an interactive rebase where you would drop the last three commits.

# make sure to be on the right branch
git checkout my_branch

# starting the interactive rebase
git rebase --interactive HEAD~3

# aligning the remote's branch' history and tip to the local branch
git push --force

During the rebase, an editor will be opened, showing you your last three commits with the pick option pre-selected. Replace pick with drop, save, and finally close the editor.

drop d790c9e my undesired commit
drop 61f4eb1 first revert
drop f33cd8c revert of the first revert

Warning: as a general rule of thumb, before force pushing, make sure that all developers are aware that you're rewriting the branch' history and that none of their works is based on the dropped commits.

Sign up to request clarification or add additional context in comments.

1 Comment

The solution as described in case 1 was exactly, what I was looking for! Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.