376

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?

5
  • Why don't you just create a new repository?
    – Stephan
    Commented Mar 31, 2015 at 12:38
  • 4
    @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
    Commented Mar 31, 2015 at 12:43
  • 39
    you can just delete the .git folder and do a git init again on the folder your sources are
    – Stephan
    Commented Mar 31, 2015 at 12:43
  • 4
    Deleting the .git folder will make your two repositores incompatibles, you wont be able to merge from one to the other
    – Arnold Roa
    Commented Jun 24, 2018 at 1:10
  • 4
    Can't you rebase it? You literally just squash all the old commits and then (force) push.
    – xaxxon
    Commented Feb 27, 2020 at 0:50

8 Answers 8

557

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.

4
  • 32
    This doesn't address the intended usage implied by the question, which is to be able to push to a remote, public repository. Trying to push a shallow clone results in shallow update not allowed.
    – Brad Knox
    Commented Dec 1, 2018 at 18:16
  • Is the Depth maintained after using git pull commands later or history stacks up again?
    – Salar
    Commented Mar 25, 2021 at 8:40
  • 1
    @Salar The depth is only relevant while cloning. After that, the history is what it is. It sounds like you want to regularly "clean history". I can't see why you'd want to do that, but new clones or squashing are solutions that come to mind.
    – Gauthier
    Commented Mar 26, 2021 at 7:25
  • 1
    @BradKnox There is no such "intended usage implied by the question", that would be a different question. All the OP is asking for is how to publish an existing repository without history, he did not say anything about ongoing changes happening in the existing repository. Indeed that is my use case too, the original repo will not change, so this solves my problem. Commented Feb 1, 2022 at 18:42
498

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
2
  • 33
    How does this answer the question? All this does is create a shallow clone locally, but then what? How do you copy this local copy with limited history to a new repo?
    – BradDaBug
    Commented Jun 12, 2018 at 15:44
  • 9
    @BradDaBug brings up a valid point. I followed the instructions above limiting the depth to 1, then changed the remote origin and then tried to push to my new repo. However, I received an error stating shallow update not allowed. I found this stack overflow answer to be useful in this instance. Commented Aug 19, 2018 at 23:45
84

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
0
6
#!/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
5
  • 5
    the --depth 1 is unnecessary in this case because the history is also deleted with rm -rf .git
    – rayphi
    Commented Jun 1, 2017 at 13:17
  • 3
    I think i would do it like this: git clone --depth 1 -b $src_branch $repo_src_url $dir && cd ./$dir && git commit --amend --author "Some One <[email protected]>" -m "Initial Commit" && git remote set-url origin $repo_trg_url && git push -u origin master
    – rayphi
    Commented Jun 1, 2017 at 13:24
  • 15
    @rayphi yes the --depth 1 is unnecessary but it is also harmless and it saves a little network bandwidth (no point fetching a history that you're about to delete anyway) Commented Feb 16, 2018 at 10:35
  • 1
    I don't recommend doing this if you want to keep your repositories compatible.
    – Arnold Roa
    Commented Jun 24, 2018 at 1:28
  • Removing .git also remove all the git hooks.
    – JudaPriest
    Commented May 29, 2019 at 9:40
6

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.

2

A lot of the answers here use the git clone --depth 1 which keeps the last commit (include the commit message).

If you wish to change the last commit message (from the cloned branch) you can use after the clone the command: git commit --amend -m "UPDATED MESSAGE HERE".

1

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

1

Firstly, I should say that if there are only a few commits containing sensitive information, you'd be better to use git rebase -i to remove them. But that can get complicated when those files have been touched frequently. Also, it's very hard to get rid of the initial commit using git-rebase, so if your initial commit contains the things you want to delete, then things get harder.

But anyway, all of the answers above to fold the entire repo into one commit seem overly complicated, and will also be quite slow involving lots of intermediate directories and possibly trashing all the other configuration stored in .git.

A far quicker approach is to create an orphaned commit that exactly matches an existing commit, for example:

$ TREE=`git cat-file -p master |sed '1,/^$/s/^tree //p;d;'`
$ COMMIT=`echo Truncated tree | git commit-tree $TREE`
$ git branch truncated-master $COMMIT

Replace master and truncated-master as appropriate, and change the commit message as you see fit. If you're feeling really brave, go ahead and:

$ git branch backup-master-just-in-case-i-regret-it-later master
$ git push -f origin truncated-master:master

But, I would really avoid doing this last step if I were you, as it will cause problems for every other person currently using the repo. Instead, just switch to using this new branch (maybe with a better name) and don't publicise the old one.

1
  • Do you know if your quicker approach works with get LFS or with git sub repositories?
    – simpleuser
    Commented Jan 25, 2023 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.