复制没有历史记录的 git repo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29368837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:03:23  来源:igfitidea点击:

Copy a git repo without history

gitgithubgit-fork

提问by Rafe

I currently 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 make public (hardcoded crentials, etc).

我目前在 github 上有一个要公开的私有存储库。但是,一些初始提交包含我不想公开的信息(硬编码凭据等)。

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?

在不包含部分或全部提交历史的情况下,使最新提交公开(我真的不需要或不想要公共存储库中的先前提交)的最简单方法是什么?

回答by Gauthier

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.

如果您想要有限的历史记录,但仍然需要一些,请使用此选项。

回答by Agam Rafaeli

Use the following command:

使用以下命令:

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

Where:

在哪里:

  • depthis the amount of commits you want to include. i.e. if you just want the latest commit use git clone --depth 1
  • branchis the name of the remote branch that you want to clone from. i.e. if you want the last 3 commits from masterbranch use git clone --depth 3 -b master
  • repo_urlis the url of your repository
  • depth是您要包含的提交量。即如果你只想要最新的提交使用git clone --depth 1
  • branch是要从中克隆的远程分支的名称。即如果你想要master分支使用的最后 3 次提交git clone --depth 3 -b master
  • repo_url是您的存储库的网址

回答by J W

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

删除.git文件夹可能是最简单的路径,因为您不需要/不需要历史记录(如 Stephan 所说)。

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,然后运行

git init

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

或者,如果您想重用当前的存储库:使当前提交成为 Git 存储库中唯一的(初始)提交?

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

回答by timo kranz

#!/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

回答by xaxxon

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

这不正是压扁变基所做的吗?只需压缩除最后一次提交之外的所有内容,然后(强制)推送它。