How to "time travel" git repository back in revisions?
Asked Answered
H

3

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.

Headset answered 21/6, 2018 at 17:15 Comment(3)
Possible duplicate of How to undo the most recent commits in Git?Graeme
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.Julissa
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.Julissa
S
22

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!

Silent answered 22/6, 2018 at 4:27 Comment(5)
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 ?Acerb
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.agoSilent
to get commit hash use git rev-parse master@{1.minutes.ago} which gives 1 minute ago commit hash of masterSilent
How to undo this in case I went too far back?Thromboembolism
Thanks a lot. I had messed my code-base on master and it really saved me from headache.Crossman
V
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

Vincentia answered 21/6, 2018 at 17:47 Comment(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?Headset
R
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.

Reasoning answered 4/5, 2022 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.