使当前提交成为 Git 存储库中唯一的(初始)提交? [英] Make the current commit the only (initial) commit in a Git repository?

查看:19
本文介绍了使当前提交成为 Git 存储库中唯一的(初始)提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个本地 Git 存储库,我将其推送到 Github 存储库.

I currently have a local Git repository, which I push to a Github repository.

本地仓库有大约 10 次提交,Github 仓库是同步副本.

The local repository has ~10 commits, and the Github repository is a synchronised duplicate of this.

我想做的是从本地 Git 存储库中删除所有版本历史记录,因此存储库的当前内容显示为唯一提交(因此不存储存储库中旧版本的文件).

What I'd like to do is remove ALL the version history from the local Git repository, so the current contents of the repository appear as the only commit (and therefore older versions of files within the repository are not stored).

然后我想将这些更改推送到 Github.

I'd then like to push these changes to Github.

我已经调查过 Git rebase,但这似乎更适合删除特定版本.另一种可能的解决方案是删除本地存储库,然后创建一个新存储库 - 尽管这可能会产生大量工作!

I have investigated Git rebase, but this appears to be more suited to removing specific versions. Another potential solution is to delete the local repo, and create a new one - though this would probably create a lot of work!

ETA:有未跟踪的特定目录/文件 - 如果可能,我想保持对这些文件的未跟踪.

ETA: There are specific directories / files that are untracked - if possible I would like to maintain the untracking of these files.

推荐答案

这是蛮力方法.它还删除了存储库的配置.

Here's the brute-force approach. It also removes the configuration of the repository.

注意:如果存储库有子模块,这将不起作用!如果你使用子模块,你应该使用例如交互式 rebase

Note: This does NOT work if the repository has submodules! If you are using submodules, you should use e.g. interactive rebase

第 1 步:删除所有历史记录(确保您有备份,这无法恢复)

Step 1: remove all history (Make sure you have backup, this cannot be reverted)

cat .git/config  # note <github-uri>
rm -rf .git

第 2 步:仅使用当前内容重建 Git 存储库

Step 2: reconstruct the Git repo with only the current content

git init
git add .
git commit -m "Initial commit"

第 3 步:推送到 GitHub.

Step 3: push to GitHub.

git remote add origin <github-uri>
git push -u --force origin master

这篇关于使当前提交成为 Git 存储库中唯一的(初始)提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆