22

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
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
me9867
  • 1,519
  • 4
  • 25
  • 53

3 Answers3

50

Use --force :

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

I am assuming origin is the remote of bitbucket

khrm
  • 5,363
  • 1
  • 22
  • 26
  • 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" – me9867 May 25 '16 at 10:24
  • @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 that – khrm May 25 '16 at 10:32
  • This post here seems to sort it **http://stackoverflow.com/questions/4114095/revert-git-repo-to-a-previous-commit?lq=1** ```git reset 56e05fced, git reset --soft HEAD@{1}, git commit -m "Revert to 56e05fced" , git reset --hard``` – me9867 May 25 '16 at 10:42
  • @merch89 It won't be change in bitbucket. You need to use --force while pushing. – khrm May 25 '16 at 10:47
  • And --hard does that. I have verified it. Are you giving --hard and then force? – khrm May 25 '16 at 10:48
  • The exact code posted above does the job both locally and bitbucket. Didn't use the --force command either. Thanks for your help. – me9867 May 25 '16 at 11:18
  • 1
    If you are pushing something in bitbucket and it conflicts, bitbucket won't allow. – khrm May 25 '16 at 11:19
  • This is great, if I only knew that when Bitbucket rejects, only thing you need to do is force... :) – Line Feb 22 '18 at 16:09
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.

logworthy
  • 1,218
  • 2
  • 11
  • 23
Nicola
  • 851
  • 7
  • 11
  • Totally agree that reset and force push is not recommended, especially if many other people are working on the same repo. – рüффп Mar 28 '18 at 15:54
0

Use revert for rollback your past commit and push.

$git revert <commitId>
$git push
  • 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](/help/how-to-answer). – Community Dec 16 '22 at 19:33