21

I have a private repo that had a bunch of sensitive data committed. However, I recently cleaned up all of the sensitive data so that it can be public. If I change a private repo on GitHub to become public, are all past commits visible (i.e. could someone see that sensitive data from the past)?

If so, how do I make the repo public without making the past commit history public?

Hugo y
  • 1,421
  • 10
  • 20
Alex Beals
  • 1,965
  • 4
  • 18
  • 26
  • 4
    Was anyone else using the repo, or was it just you? If only you, you could rebase/squash all the commits into one commit, and then force push the change to the origin repo, and then make it public. – Gary Ewan Park Sep 05 '16 at 20:01
  • @GaryEwanPark why don't you post that as an answer? – Maria Ines Parnisari Sep 05 '16 at 20:30
  • 3
    `git checkout --orphan public_master master` is one of the methods to squash all the commits on master into the index, after which run `git commit` to commit it as the new root of `public_master`. And then you could push `public_master` as you like. – ElpieKay Sep 06 '16 at 00:36
  • @mparnisari because I was first asking the OP whether or not anyone else was using the repo. The answer only makes sense when no-one else has been using the repo. – Gary Ewan Park Sep 06 '16 at 06:14

2 Answers2

13

Go to desired commit:

git checkout <your_commit_hash>

Go down to the initial commit leaving all current changes:

git reset <intial_commit_hash_here> --soft

Then commit with amend option

git commit --amend -m"My new initial commit"

And then you are ready to push to your public repo

git push <your_remote> master

P.S. The original change history will still be available with git reflog but will not be pushed to remote repo


UPD. To get the id of the first commit use the command from this answer:

git rev-list --max-parents=0 HEAD
Teivaz
  • 5,462
  • 4
  • 37
  • 75
  • 1
    I followed your procedure and I am now in a new origin that only has one commit. If I `git pull origin master` I get an error message (`failed to push some refs to "github.com/,foo" updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes` – Pietro Biroli Aug 29 '19 at 13:19
  • @pietro you are on a dangerous ground here. What you are trying to do is erasing the whole history from the original repository. If you **really** want to do this and you know exactly what you are doing then use `--force` flag when pushing. – Teivaz Aug 29 '19 at 13:26
  • I know that's what I was afraid of! not sure what I have done wrong. I didn't checkout the committ because I was already there (it was my last commit) is that it? – Pietro Biroli Aug 29 '19 at 14:08
  • This is my history: 1975 git rev-list --max-parents=0 HEAD 1976 git log 1977 git reset 7142a466feea856d02de1c37abfbceb086e4db83 --soft 1978 git commit --amend -m "Public commit" 1979 git push – Pietro Biroli Aug 29 '19 at 14:11
5

I'd recommend cloning (or just copying all the non .git files) from the current repo into a new repo and then pushing the new repo out as public.

winhowes
  • 7,845
  • 5
  • 28
  • 39