Shaban Shaame
Shaban Shaame

Reputation: 62

bitbucket rebase a branch on a previous commit

I'm using bitbucket with source tree. I would like to rebase my remote branch to a previous commit.

There are some change that I made locally that was merged with remote. The remote is on a non working state. I would like to make a simple operation of getting back to the previous state. If I rebase on the remote branch my local version becomes operational but the remote version is still on the wrong state. If I try to push it will refuse because Updates were rejected because the tip of your current branch is behind

I can't reverse commit because a merge occurred. I can't also force push because the button is disabled.

Upvotes: 1

Views: 7100

Answers (1)

Michael Kang
Michael Kang

Reputation: 52867

First, backup your repository:

Copy it and save it somewhere.

Second checkout your remote branch:

git checkout -b mybranch origin/mybranch -u

If you want to revert back to a previous state, you have three options:

 1. Revert - git revert <previous_commit>
 2. Rebase - git rebase -i <previous_commit>
 3. Reset - git reset <previous_commit>

Option 1 is the safest because it creates a new commit that reverts the current commit.

Option 2 re-writes history by interactively rebasing commits starting from a previous commit. You can selectively choose which commits are part of the new history.

Option 3 also re-writes history by forcing the branch HEAD to point to a previous commit (as if the future commits did not happen). You may need to run this with the --hard option if you have checked out files (take care to not lose your work!)

Since Option 2 and Option 3 re-writes history, you should take care that your team is notified of the change. They will need to force get the latest branch (or just re-clone the repository from the source). Once you've made the changes and you're satisfied, push the changes to your remote branch:

git push origin mybranch --force

Or simply

git push --force

since there is an upstream branch already setup.

Upvotes: 1

Related Questions