How to rollback or delete commit to feature branch in BitBucket

-Dman100-
-Dman100- used Ask the Experts™
on
I'm using Bitbucket for version control we have multiple feature branches. I made a commit to my feature branch, which has not been merged to the master branch. I'd like to rollback/remove the commit that I made to my branch. How do I remove that commit? I don't want that commit that I made to be included in my next pull request to the master branch so I'd like to just remove it. How can I do that?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Entrepreneur & Web developer
Commented:
Dman,

In the first place, if you are working with other people on the same code repository, you should not delete a commit since when you force the update on the repository it will leave the local repositories of your coworkers in an illegal state (e.g. if they made commits after the one you deleted, those commits will be invalid since they were based on a now non-existent commit).

If you are not working with others (or are happy to cause them significant annoyance), then it is possible to remove commits from bitbucket branches.

If you're trying to change a non-master branch:
git reset HEAD^               # remove the last commit from the branch history
git push origin :branch_name  # delete the branch from bitbucket
git push origin branch_name   # push the branch back up again, without the last commit

Open in new window


if you're trying to change the master branch
In git generally, the master branch is not special - it's just a convention. However, bitbucket and github and similar sites usually require there to be a main branch (presumably because it's easier than writing more code to handle the event that a repository has no branches - not sure). So you need to create a new branch, and make that the main branch:
# on master:
git checkout -b master_temp  
git reset HEAD^              # undo the bad commit on master_temp
git push origin master_temp  # push the new master to Bitbucket

Open in new window


On Bitbucket, go to the repository settings, and change the "Main branch" to master_temp (on Github, change the "Default branch").
git push origin :master     # delete the original master branch from Bitbucket
git checkout master
git reset master_temp       # reset master to master_temp (removing the bad commit)
git push origin master      # re-upload master to bitbucket

Open in new window

Now go to Bitbucket, and you should see the history that you want. You can now go to the settings page and change the Main branch back to master.

This process will also work with any other history changes (e.g. git filter-branch). You just have to make sure to reset to appropriate commits, before the new history split off from the old.
-Dman100-Software Consultant

Author

Commented:
Thanks for the reply! Okay, let me ask if there is a slightly different way to handle this issue. So, I committed a change to my branch in bitbucket that I do not want to push to the master branch. I've already reverted the change in my local code.

If I leave the commit in Bitbucket on my branch, can I just make new commits and when I go to make a pull request on my new commits from my branch to master is there a way to not include that bad commit to the master branch? Maybe I'm mistaken, but I think when I tried to do a pull request, it  grabbed my bad commit and tried to update master. I just want that bad commit to go to the master branch.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial