1
votes

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?

2
You can't delete a commit. You can rewrite the history, e.g. with an interactive rebase, so that instead of A -> B -> C you have A -> C' (note not quite the same as C, as it has a different parent), but then you'll have to force push as your history won't match the remote. If you make a squash commit when you merge the PR B won't be in the resulting history anyway, so unless it caused other problems (large files, leaked credentials) it might not be worth it. - jonrsharpe
Okay thanks. But how would I create pull request for the second clean commit only? as when I click on pull request now, I see both commit. - CodingValue
Because your PR is from a branch that contains both commits. Either force push over that branch (as long as nobody else will have that history), or create a new one and open a PR from that. - jonrsharpe
How to force push over that branch? create a new one and open a PR from that. create a new what? branch, commit, PR... can you please be more explicit. - CodingValue
1. Google "git force push". 2. New branch, but it will contain a new commit and you'll make a new PR from it, so all three I suppose. - jonrsharpe

2 Answers

5
votes

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://[email protected]/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.

enter image description here

1
votes

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