4

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?

CodingValue
  • 145
  • 1
  • 2
  • 12
  • 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 Jan 21 '20 at 08:50
  • 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 Jan 21 '20 at 09:04
  • 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 Jan 21 '20 at 09:10
  • 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 Jan 21 '20 at 09:12
  • 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 Jan 21 '20 at 09:14
  • In fact I'd suggest research in general, there are loads of posts about editing PRs e.g. https://stackoverflow.com/questions/36168839/how-to-remove-commits-from-a-pull-request – jonrsharpe Jan 21 '20 at 09:19
  • but I don't want to create a new branch and commit from it. I want to commit from the current branch. – CodingValue Jan 21 '20 at 09:20
  • example listed on above stackoverflow link doesn't work for me. getting error : **Fatal - Needed a single revision** and ** invalid upstream 'HEAD~c53fd180'** – CodingValue Jan 21 '20 at 09:32

2 Answers2

13

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.

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • 1
    @CodingValue Is the solution working for you? Any update here? – Andy Li-MSFT Feb 04 '20 at 06:09
  • 1
    Well, it worked for me. Saved me a lot of **. I accidently had some sensitive info on there. Eventually deleted the branch to get rid of the updates in "pushes", but cleansing it from the commit allowed me to create a new branch by pushing my changes (minus the sensitive info) again, preserving all other commits. Thank you very much @Andy Li-MSFT! – Jon Koeter Oct 12 '20 at 12:32
  • 3
    Worked for me, however `-p` (`--preserve-merges`) had to be replaced with the new `--rebase-merges` – Chucky Jun 28 '22 at 13:40
1

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

AbuDawood
  • 745
  • 7
  • 22
  • 4
    If there is sensible data in a commit, a revert is not the right way.... Sometimes it is the right way to delete a complete commit – Stefan Oct 09 '20 at 11:41