14

Is it possible to completely revert git repository to previous X revision on bitbucket so that it doesn't keep any changes after that X revision and doesn't contain any source code change newer than X revision.

For example if there are 1,2,3,4,5 revisions in the repo and i want to revert to revision 3, so that it's the last revision and completely wipe out any trace of revisions 4 and 5 as if they never were made and can't be retrieved.

user358448
  • 1,127
  • 3
  • 20
  • 39
  • Possible duplicate of [How to undo the most recent commits in Git?](https://stackoverflow.com/questions/927358/how-to-undo-the-most-recent-commits-in-git) – phd Jun 21 '18 at 19:12
  • You can not reset in bitbucket repo directly. Instead, you can reset in local git repo `git reset --hard 3`, and then force push to bitbucket. – Marina Liu Jun 22 '18 at 06:58
  • If your problems solved, you can mark the answer which helps you solve the problem. And it will also benefit others who meet similar questions. – Marina Liu Jul 02 '18 at 03:46

3 Answers3

20

Yes, you can do this there many ways to do this:

1) you can specify the time like 10.minutes.ago, 1.hours.ago, 1.days.ago ...

Ex: if you want to go back 5 days ago on the master branch git reset --hard master@{5.days.ago}

2) if you know the commit hash then you can go back to it directly git reset --hard <commit_hash>

Thanks!

rahul mishra
  • 1,390
  • 9
  • 16
  • A n-days-back reset ? I never knew you could *do* that. Not choosing precisely which commit you'll be pointing to seems crazy from here but I might just not understand some given context, I guess. What kind of use cases would need that ? – Romain Valeri Jun 22 '18 at 13:36
  • one use case is: suppose I rebase my branch with multiple conflicts and accidentally delete some code and now I can't revert my branch as its branch history is also get change so to go back to previous state i simple use time reset say `30.minutes.ago` – rahul mishra Jun 22 '18 at 13:47
  • to get commit hash use `git rev-parse master@{1.minutes.ago}` which gives 1 minute ago commit hash of master – rahul mishra Jun 22 '18 at 13:49
  • How to undo this in case I went too far back? – tejasvi88 Sep 11 '20 at 14:17
  • Thanks a lot. I had messed my code-base on master and it really saved me from headache. – Ammar Mujeeb May 02 '21 at 14:45
2

You can clone the repo locally, git reset --hard <commit 3>, then git push -f back up to the remote. You would need to do this for all branches.

You should also be very careful when using -f to push - make sure you're pushing to the right repo/branch.

This will rewrite history for the remote repo, so you should inform any other consumers of this repo that there will be conflicts.

Read more here: https://git-scm.com/docs/git-push#git-push---force

sp0gg
  • 3,722
  • 1
  • 17
  • 20
  • 1
    Will this completely wipe out revisions after commit 3 as if they were never pushed to the repo? Would anyone else be able to restore them any any way? – user358448 Jul 03 '18 at 07:39
0

doesn't contain any source code change

completely wipe out any change ... and can't be retrieved

If you really need to delete data that has been uploaded to a third party (Github, Bitbucket) you must follow their instructions for deleting the data. Just because the data is no longer on the main branch (or any branch) doesn't mean it isn't accessible at the commit's SHA identifier.

https://confluence.atlassian.com/bitbucketserverkb/how-do-i-remove-sensitive-unwanted-content-that-was-pushed-to-my-bitbucket-server-instance-1019389998.html

https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository

These involve using either the BFG Repo-Cleaner or git filter-repo to remove all instances from throughout the repository; I recommend following the instructions given carefully.

David McKee
  • 170
  • 8