0

A commit was pushed to branch random-tests that included some sensitive information by accident. The branch was not going to be used again, so I just force deleted from remote and local, thinking that was going to clear out any commits in that branch. I was wrong. The commits are still there even though the branch itself is gone.

This was the first commit to that branch before it split off from master. Since the branch is deleted and that was the first commit on that branch, how do I delete that problematic commit without affecting anything else?

eComEvo
  • 11,669
  • 26
  • 89
  • 145
  • Do you have access to the server running BitBucket? See https://confluence.atlassian.com/bitbucket/maintaining-a-git-repository-321848291.html – Owen Aug 03 '16 at 02:19
  • What do you mean by "the commits are still there"? – larsks Aug 03 '16 at 02:19
  • 1
    By "commits are still there", do you mean the "unreachable" commits? If so, then you can simply [garbage collect them](http://stackoverflow.com/q/1904860/211627). – JDB Aug 03 '16 at 02:58
  • Also keep in mind that if anyone else cloned the repo or the branch in between when you pushed the branch and then deleted it, they could get the branch as well and repush it. – David Neiss Aug 03 '16 at 04:04
  • @larsks All our commits are pushed to Slack. So I can go into Slack and click the link for the commit and it opens right up on BitBucket. – eComEvo Aug 03 '16 at 14:31

1 Answers1

0

If you're still seeing those commits, then they must not have only been on that random-tests branch. Anyway, here's an option:

Say here's your list of recent commits:

  • bad commit
  • good commit
  • good commit

To erase the 'bad commit' from Git history, you could run git reset --hard HEAD~1, which will reset the tree back one single commit, and forget the 'bad commit' ever existed.

To do this with a little more caution, you could instead run git reset --soft HEAD~1, do a git status go make sure the correct files are unstaged, and then run git reset --hard to reset to the next commit, which would be the most recent 'good commit.'

Might not be the best solution out there, but it should work. Hope that helps & makes sense!

UPDATE: Because you've modified Git history, you'll have issues when pushing this to your remote, so you'll have to do a git push origin branch -f to force it up.

Alex MacArthur
  • 2,220
  • 1
  • 18
  • 22
  • Yes, they were indeed on the `random-tests` branch. When I go into BitBucket, they don't show a branch anymore. The only way I can view them is by going directly to the link shown in our Slack #commits channel such as: https://bitbucket.org/ouraccount/app/commits/e4de5130xfc3e57b79111a87d336582a710bc04e – eComEvo Aug 03 '16 at 14:34
  • Also, how can I run `git reset` when I can't pull the branch in question? Further, the commit I want to erase is the first commit on that now deleted branch. – eComEvo Aug 03 '16 at 14:36