2

I unwittingly added api key information to a public bitbucket repo, and thought I would learn how to remove it down the road. 6 commits later, I'm now getting around to it, and don't quite understand how to even after reading things like - http://goo.gl/Lf463C.

I don't want to lose my HEAD commit, esp. if I'm learning by trial by fire, so I thought I'd ask the experts. I actually don't have a problem wiping the public repo, and recommitting my latest local repo as the initial either, but hate to lose the learning curve history that I accumulated.

So to summarize, lets say I have 20 commits, and I need to remove a file from the 10th commit, without messing up the other commits, how would I do this? Thanks!

gamengineers
  • 537
  • 1
  • 8
  • 22
  • I've never used bitbucket, but can't you just generate a new key? If you can then you can just fix the file (don't worry about deleting it out of history). – onionjake Aug 03 '13 at 04:20

3 Answers3

1

Supposing no one has downloaded that unwanted file on their local machine(s) you can remove the file.

Use the following while you are on the branch in question: git rebase -i <branch_base>

When you are given a list of words adjacent to a list of commits, you can change the word pick to edit on the problematic commit. (pick means to use the commit, edit means you want to make some changes on that commit before continuing.)

From there you can use git reset HEAD^ when it stops you on that commit. Then add back in the files that you want to exist in your history and don't add the file that you don't want in your history. Followup with git commit --amend (add a commit message) and then git rebase --continue

At the end of your rebase your history shouldn't include that file.

BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
  • 1
    Rebase is one solution, but for a repo with a large number of commits, and with the file in question buried deep in the history, rebase becomes impractical. In such cases, people use `filter-branch` to remove files from a repo's history. –  Aug 03 '13 at 02:19
  • 1
    @Cupcake Oooh! Please, do tell! – BlackVegetable Aug 03 '13 at 02:21
  • Look at http://stackoverflow.com/questions/872565/remove-sensitive-files-and-their-commits-from-git-history – punchagan Aug 03 '13 at 05:18
1

Given that the repo is public, you can't regard that API key as private any more - you need to get that api key invalidated and start using a new one supplied by a config file that is not checked into your repository (use .gitignore if you decide you want the properties file to live within the file hierarchy of your project).

It's reasonable to use rebase to remove the bad stuff on such a small project (only ten commits in since the bad stuff happened), otherwise you need a more powerful tool to rewrite Git history for you:

...not git filter-branch

Seriously, don't use git filter-branch, it's a painful experience for a task like this. There's a much better, less well-known tool called The BFG. On a big repo it's several hundred times faster- and much easier to use.

Roberto Tyley
  • 24,513
  • 11
  • 72
  • 101
-1

Assuming history is published -

you can use

 git revert commit-name 
forvaidya
  • 3,041
  • 3
  • 26
  • 33