Revert to an old commit in Bitbucket
Asked Answered
T

3

23

I have git setup on my web hosting and on an account at Bitbucket which are both linked.

How can I revert back to my first commit on both my hosting (which I am logged into via SSH and has git installed ready and working) and on Bitbucket?

I have tried: git checkout 965a793

Then I tried with a dot on the end: git checkout .

but nothing seems to change on the Bitbucket side, when I got to git push. It says everything is up to date, even though Bitbucket is on commit cf08232

Here is a list of my three commits:

cf08232 remove the txt file
096d08f test.txt edited online with Bitbucket
965a793 Initial Commit
Thromboplastin answered 25/5, 2016 at 10:9 Comment(0)
P
52

Use --force :

 git reset --hard commitID
 git push origin branchName --force

I am assuming origin is the remote of bitbucket

Pinery answered 25/5, 2016 at 10:14 Comment(8)
Tried "git reset --hard 965a793" then "git push origin master --force". Still Bitbucket appears unchanged. My contents of git branch -a are "master" and "remotes/origin/master"Thromboplastin
@merch89 Reconfirm again. I check it in bitbucket now, it works for me. When you do git log, does it show anything else apart from 965a793 commit. Before you do git push origin master --force Confirm thatPinery
This post here seems to sort it #4114595 git reset 56e05fced, git reset --soft HEAD@{1}, git commit -m "Revert to 56e05fced" , git reset --hardThromboplastin
@merch89 It won't be change in bitbucket. You need to use --force while pushing.Pinery
And --hard does that. I have verified it. Are you giving --hard and then force?Pinery
The exact code posted above does the job both locally and bitbucket. Didn't use the --force command either. Thanks for your help.Thromboplastin
If you are pushing something in bitbucket and it conflicts, bitbucket won't allow.Pinery
This is great, if I only knew that when Bitbucket rejects, only thing you need to do is force... :)Parttime
W
20

Please note git reset is dangerous. I personally am not a fan because it deletes/modifies change history. If you want to rollback your changes to a specific commit without modifying the change history, I suggest using git revert instead:

git revert cf08232
git revert 096d08f

Each time you run git revert, it will create a new commit that undoes the changes introduced by a specific prior commit, without modifying the change history.

Here's a good article that compares reset, checkout & revert.

Waterhouse answered 30/1, 2018 at 14:41 Comment(1)
Totally agree that reset and force push is not recommended, especially if many other people are working on the same repo.Michel
M
1

Use revert for rollback your past commit and push.

$git revert <commitId>
$git push
Minoan answered 14/12, 2022 at 6:43 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Communal

© 2022 - 2024 — McMap. All rights reserved.