I have commited a code to my master branch in bitbucket.But mistakenly i have added my credit card number as a echo statement of a php file.So now i need to delete complete commit from bitbucket.I saw there are several ways to do such as git rebase or reset.But still no luck for it.So is there a way to do that?Sometimes i could have been followed wrong steps for that.
-
Possible duplicate of [Delete commits from a branch in Git](http://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git) – Andy Ray Jan 03 '17 at 04:42
-
Nope there's only one branch call master and only me working on it.But in the future there will be more developers work on it.So before that i need to remove my commit. – CodeCanyon Jan 03 '17 at 04:44
-
"But still no luck for it." is spectacularly unclear. – jthill Jan 04 '17 at 05:27
1 Answers
Normally deleting a commit from a published branch is a bad idea, because it can cause problems for anyone else sharing that branch. But in this case, leaving your credit card number is probably worse.
If the commit containing the CC number is the HEAD of the branch, then you can amend the commit by editing that PHP file and then committing via:
git commit --amend
If the bad commit is not the HEAD of the branch, then you can try doing an interactive rebase via:
git rebase -i HEAD~10 # replace 10 with however far you need to go back
This will bring up a list of commits on your branch, oldest to newest. Find the commit containing the credit card, and change pick
to edit
for every commit from that point down the list. You need to remove that information from every commit since you first added it to the history.
Note that since you have rewritten the history of your branch, you will have to force push it to the remote using:
git push --force origin master

- 502,043
- 27
- 286
- 360
-
OK, i followed your steps.So you're asking to change pick to amend in the text editor is it?That's it? – CodeCanyon Jan 03 '17 at 05:18
-
@CodeCanyon You need to change `pick` to `amend` for _every_ commit from (and including) the commit where you accidentally added the credit card number _as well as_ every commit appearing _below_ that commit in the list. Then complete the rebase, and at each of those commits Git will give you a chance to make changes. Remove the credit number and type `git rebase --continue` until you have finished. – Tim Biegeleisen Jan 03 '17 at 05:19
-
Ok, i did that way. But i think i have missed one step which is git rebase --continue. So will it be an issue if i follow the same steps again? – CodeCanyon Jan 03 '17 at 05:37
-
Just do `git rebase --abort` and stop over. Yes, after each commit which was marked for `amend` is applied, you will have to manually enter `git rebase --continue` afterwards to continue the rebase. – Tim Biegeleisen Jan 03 '17 at 05:38
-
Ok, then i have to remove that relevant code from my local file and push it or just push it? Because i noticed once rebase, i can see that relevant credit card number line is in my code although i deleted it from the code. – CodeCanyon Jan 03 '17 at 05:41
-
Then the rebase was not done properly, perhaps you neglected to remove it from every commit. Or, maybe your working directory is dirty, in which case doing `git reset --hard HEAD` should fix it. – Tim Biegeleisen Jan 03 '17 at 05:42
-
-
I mean that when you do an interactive rebase, you are shown a list of commits, let's say 10 of them. Suppose that halfway down the list is where the bad commit with the CC# is. Well, you need to `amend` _every_ commit from that one all the way down to the present. In other words, you need to whitewash every commit between now and then. – Tim Biegeleisen Jan 03 '17 at 05:44
-
Whitewash every commit means i just have to change the pick to amend.Is it?Nothing else to do is it? – CodeCanyon Jan 03 '17 at 06:08
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132151/discussion-between-tim-biegeleisen-and-codecanyon). – Tim Biegeleisen Jan 03 '17 at 06:11