2

I've been working on a project locally for some time, and it's finally at the point that I'd like to upload it to GitHub to share. Early versions of the project contained hard-coded database passwords that I'd like to avoid putting on GitHub, though.

Is there a way for me to push a "shallow" version of the repo -- just the files as they are now, without history -- to GitHub? Something like git clone --depth 1 would be nice, except it seems like you can't "push clone" from localhost to GitHub, and GitHub doesn't seem to have an interface that lets me "pull clone" in the normal way.

I've spent a couple of hours doing web searches for "git push to github without history", "git push to github as shallow clone", "git push clone to github", etc., but nothing comes up that I can decipher as relevant to my problem. The GitHub docs for this warn against committing passwords and things, but the only advice they give is to remove files, not history. Similarly, the closest question I can find on Stack Overflow is this question whose answers also focus on deleting files. I don't want this: I want to keep the files, I just want to remove the history that they once contained hard-coded passwords.

Ideally, I would be able to "shallow push" the project from localhost to GitHub, losing previous history but tracking history from that point forward. Is this possible?

PiotrChernin
  • 441
  • 8
  • 18

3 Answers3

3

One thing you can do is copy the directory to a new folder and remove your .git directory. Then you can initialize the repository as if it were fresh:

git init
git add .
git commit -m "Genesis."
git remote add origin <remote repository URL>
git push -u origin master

This way you can preserve the history in another directory if you ever find the need to look something up.

codenamev
  • 2,203
  • 18
  • 24
  • In a case like this, can I push a diff from the original local repo to the GitHub repo? – PiotrChernin Dec 18 '18 at 21:34
  • 1
    In this case, you would lose any ability of interacting with the new from the old. However, you should be able to create a `patch` if you needed to. – codenamev Dec 18 '18 at 21:56
2

There is no such thing as a "shallow push". There are shallow clones, and you could make a shallow clone and then push to it, but you can't make GitHub itself host a shallow clone. If you could get GitHub to host a shallow clone, it looks like you could do what you want that way: I made one on my own server and did a few simple experiments that seemed to work (but I was not very thorough).

I also tested trying to push from a shallow clone to a new, empty GitHub repository. That was refused:

 ! [remote rejected] master -> master (shallow update not allowed)

Hence, if you want to use GitHub, you'll have to make a new (separate and independent) repository. This new repository is not compatible with your existing repository.

torek
  • 448,244
  • 59
  • 642
  • 775
0

To add to @codenamev answer this is what worked for me

  • clone down repo, rename it
  • create new empty repo in GitHub, then in cloned down repo run:
  git init 
  git add . 
  git commit -m "create new template" 
  git remote set-url origin git@github.com:<your organisation/repo>.git 
  git push -u origin main <or other branch>

in GitHub go to settings and set newly pushed up branch to default branch

C-Dev
  • 425
  • 1
  • 6
  • 15