Remove intermediate commit in Visual Studio Online

2020-02-12 19:00发布

问题:

I edited my README.md multiple times because I didn't see the preview button. Now my commit history is full of useless commits.

Can I remove some of them, or at least hide them ?

回答1:

CAUTION

History rewriting can get your source control system in a pretty messed up state. Make sure you have a good backup of your sources in case anything goes wrong.

Git

Depending on your Team Project setup, the Readme.md is stored in a Git repository you can absolutely rewrite history and force push the squashed commits back to TFS, essentially making it forget the in-between data. This is done using git rebase and cannot be done through the Visual Studio Online site nor through the Visual Studio Tools for Git. You will need to do it from the commandline.

The whole process is explained very well in the Git-SCM wiki. You'll need to do the following steps:

  1. From an account with Force Push permissions clone your repository containing the readme.md.
  2. use git rebase -i HEAD~6 (6 being the number of commits to rewind)
  3. use Squash to merge the commits together
  4. use git push --force origin master to force the history rewrite on the remote

Note: this will change the hash of the commit and every commit that came after. After doing this, either warn all other contributors to resync or be sure that no others have worked on the repo after you made these commits.

TFVC

If your Team Project is configured using TFVC, then the process is slightly different.

  1. Make sure you have a copy of the file you want to keep.
  2. Destroy the file in source control using tf destroy $/Teamproject/readme.md, If need be you can use the /keephistory /stopat:C12345 option to destroy the data in specific change sets at the end of a file's history.
  3. Now copy your backed-up file back into place
  4. and check it in as you'd usually do or from the commandline tf add $/teamproject/reqadme.md followed by tf checkin. If you kept history around, TFVC will reconnect it. If you completely destroyed the history, TFVC will just add a new file.