How to really delete a commit on VSTS / Azure-devops

♀尐吖头ヾ 提交于 2021-01-29 17:12:40

问题


I'm working on VS 2017 and we have a cloud VSTS / Azure-devops.

I recently committed and pushed... and realized that 8 files were not supposed to be in it. after that I committed again a clean one and pushed in to remote.

Now I can see both commit in VSTS website / portail.

The thing is I need to create a PULL REQUEST, and when I try to create a pull request, I found myself with the first commit file inside the pull request... which I don't want.

So I just need to delete the first commit I did, and create a pull request for the second clean commit. How do I go by? cause I searched and couldn't find a clear explanation on how to do?


回答1:


You can try the following steps to remove an entire commit:

  1. Git clone the specific branch to a local repository :

    git clone --single-branch --branch branchname https://xxx@dev.azure.com/xxx/0508-t/_git/0508-t

  2. Get the SHA of the commit which you want to be deleted:

    Navigate to https://dev.azure.com/{organization}/_git/{Project}/commits page -> Select the specific commit -> Click More -> Copy Full SHA . (Reference below sceenshot)

  3. Run this command in git bash to remove the commit in local repository :

    git rebase -p --onto SHA^ SHA

    Just replace "SHA" with the reference you want to get rid of (the one you get from the step 2). The "^" in that command is literal. For example :

    git rebase -p --onto 7e5661e7545013145dc7569edbe7c99f4b8d9671^ 7e5661e7545013145dc7569edbe7c99f4b8d9671

  4. Force git push to origin:

    git push origin +test

Notice the + sign before the name of the branch you are pushing, this tells git to force the push. It is worth to mention that you should be very careful when deleting commits because once you do it they are gone forever. Also, if you are deleting something from a remote repository make sure you coordinate with your team to prevent issues.




回答2:


You do not need to physically delete the commit that you do not want in your project. this is not a friendly solution to the git purpose i.e. its main role to track files changes (or versioning).

Simply try to change your vision about the git to get the correct sense in exploring new features you never tried before.

Here, in your case, I suggest using the revert option that allows you to push a new commit that undoes the change of specific previous commit.

In VS > Team Explorer (right side) > Branches > right-click the target branch > view history. this will open a new vs window with a list of committed modifications

right-click on the commit you want to undo > select revert

then push the reverted commit



来源:https://stackoverflow.com/questions/59837187/how-to-really-delete-a-commit-on-vsts-azure-devops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!