254
votes

I have a private repository on GitHub that I want to make public. However, some of the initial commits contain information that I don't want to publicize (hard-coded credentials, etc).

What is the easiest route to make the latest commit public (I don't really need or want the previous commits in the public repository) without including some or all of the commit history?

6
Why don't you just create a new repository?Stephan
@Stephan I'm fine creating a new repository, but how do I snag the latest state from the old repository and commit it to the new?Rafe
you can just delete the .git folder and do a git init again on the folder your sources areStephan
Deleting the .git folder will make your two repositores incompatibles, you wont be able to merge from one to the otherArnold Roa
Can't you rebase it? You literally just squash all the old commits and then (force) push.xaxxon

6 Answers

421
votes

You can limit the depth of the history while cloning:

--depth <depth>
Create a shallow clone with a history truncated to the specified 
number of revisions.

Use this if you want limited history, but still some.

379
votes

Use the following command:

git clone --depth <depth> -b <branch> <repo_url>

Where:

  • depth is the amount of commits you want to include. i.e. if you just want the latest commit use git clone --depth 1
  • branch is the name of the remote branch that you want to clone from. i.e. if you want the last 3 commits from master branch use git clone --depth 3 -b master
  • repo_url is the url of your repository
33
votes

Deleting the .git folder is probably the easiest path since you don't want/need the history (as Stephan said).

So you can create a new repo from your latest commit: (How to clone seed/kick-start project without the whole history?)

git clone <git_url>

then delete .git, and afterwards run

git init

Or if you want to reuse your current repo: Make the current commit the only (initial) commit in a Git repository?

Follow the above steps then:

git add .
git commit -m "Initial commit"

Push to your repo.

git remote add origin <github-uri>
git push -u --force origin master
6
votes
#!/bin/bash
set -e

# Settings
user=xxx
pass=xxx
dir=xxx
repo_src=xxx
repo_trg=xxx
src_branch=xxx

repo_base_url=https://$user:[email protected]/$user
repo_src_url=$repo_base_url/$repo_src.git
repo_trg_url=$repo_base_url/$repo_trg.git

echo "Clone Source..."
git clone --depth 1 -b $src_branch $repo_src_url $dir

echo "CD"
cd ./$dir

echo "Remove GIT"
rm -rf .git

echo "Init GIT"
git init
git add .
git commit -m "Initial Commit"
git remote add origin $repo_trg_url

echo "Push..."
git push -u origin master
0
votes

Isn't this exactly what squashing a rebase does? Just squash everything except the last commit and then (force) push it.

0
votes

You could set the GitHub repository to be a template (by going to settings and selecting the option just under the repository name). A button saying "Use this template" will then appear on the Code page. This copies over all the files but removes all history and if you keep the original repo as private, this doesn't show any details under the repo name (note that it will show on your site as you own both but not to anyone else). It's only if the repo is public that a link to the original repo appears under the repo name.