1

I've accidentally made a commit that had some sensitive information that should not be committed. Here's what the commit history looks like: Git commits

I've removed some identifying information, but basically I want to delete commit C. Ideally, I would like to keep commit A and B while doing this, but if it is not possible, then going back to commit D would be acceptable.

Note: This is in the master branch if it makes a difference.

I've tried

git rebase D
git push

I've also tried the instructions here: http://www.clock.co.uk/blog/deleting-a-git-commit

But neither of them have provided the desired result. I'm sure that I'm doing something wrong.

  • 2
    The accepted answer definintely does not do what you want. You are looking for something like `git filter-branch`. See http://stackoverflow.com/questions/872565/remove-sensitive-files-and-their-commits-from-git-history. –  Dec 06 '15 at 05:10
  • I agree that filter-branch is another option. updated my answer accordingly. It did helped me few times in the past as well but just keep in mind that you might need a forced push. – CodeWizard Dec 06 '15 at 05:21

1 Answers1

-1

Few options:

Revert

You can simply revert the code in your C commit:

revert <C commit id>

It will revert the code added in this commit.

cherry-pick

Checkout new branch based upon your origin master and then pick the commit you want to keep while skipping the ones you wish to remove.

git checkout origin/master
git checkout -b <new_branch>
git cherry-pick <commit1> <commit2> ... <commitn>

filter-branch

Some day you or a collaborator may accidentally commit sensitive data, such as a password or SSH key, into a Git repository. Although you can remove the file from the latest commit with git rm, the file will still exist in the repository's history. Fortunately, there are other tools that can entirely remove unwanted files from a repository's history. This article will explain how to use two of them: git filter-branch and the BFG Repo-Cleaner.

https://help.github.com/articles/remove-sensitive-data

Remove sensitive files and their commits from Git history

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    I don't think this will permanently eradicate all traces of the code in question from the repo, which is what the OP wants. –  Dec 06 '15 at 04:45
  • cherry-pick will. since you will open a new branch and you will "import" into it only the desired code and not the C commit. revert will leave trace - i agree about that – CodeWizard Dec 06 '15 at 04:49
  • No matter how many new branches you create or how you create them, the previous commit is still in the repo. –  Dec 06 '15 at 05:07
  • nope if you are checking out the remote branch and not the local branch. since he did not pushed his code. – CodeWizard Dec 06 '15 at 05:08
  • There is no indication from the OP that he has not pushed his commit. –  Dec 06 '15 at 05:12
  • If its in the remote repository he can then force push the new branch so his changes will be removed as well. – CodeWizard Dec 06 '15 at 05:13