如何将过去的历史记录导入到新的 git 存储库中

How to Import past history into new git repo

提问人:Cyberninja 提问时间:10/8/2023 最后编辑:Cyberninja 更新时间:10/8/2023 访问量:54

问:

我有很多旧脚本要导入 GitHub。我想知道是否有办法添加在 git 开始跟踪更改之前发生的历史记录。

现在发生了什么:我在 Github 或本地 git 上制作了一个存储库。我将旧的脚本文件添加到其中,看起来我刚刚在 git 中创建了该文件,但该文件最初是几年前创建的。

示例文件

!/bin/bash
# <date> created this script to do this
# yyyymmdd added feature 
# yyyymmdd fixed something 

我正在跟踪我正在更新的文件标题中的更改。有没有办法在新的 git 存储库中添加版本控制信息?

如果有更好的方法来添加旧脚本的历史记录,请告诉我。

Git GitHub 历史

评论

0赞 knittl 10/8/2023
你能澄清一下吗?我不知道你想做什么。是否要导入不同存储库的旧提交?是否要在现有提交之前插入单个提交?你是在问你是否可以(神奇地)找回非版本控制文件的旧版本吗?
0赞 Antonio Petricca 10/8/2023
你自己回答:“......在 Git 开始跟踪更改之前。
0赞 Cyberninja 10/8/2023
@knittl 在使用 git 或任何其他版本控制应用程序之前,我会手动将信息信息放入脚本的标题中。这就是我试图展示的。有没有办法将这个旧版本控制信息添加到 git 存储库中?
0赞 knittl 10/8/2023
@Cyberninja但是你把剧本的旧内容放在哪里呢?或者你只对“提交消息”感兴趣,而不关心实际内容?您不能凭空构建文件的旧版本。
0赞 Cyberninja 10/8/2023
@knittl我只是在我的主目录中有脚本。当我换工作或换电脑时,我只会在 Google Drive 上玩它们。哦,更新了问题。我希望你现在能更好地理解它

答:

0赞 knittl 10/8/2023 #1

可以只使用单个存储库来执行此操作,但在此过程中使用两个单独的存储库会更容易,并且出错的机会更少。

首先,创建一个新的空 Git 存储库并添加文件:

git init old-history
cd old-history
# copy your file, make sure you use the expected directory structure
git add your_file
git commit -m 'adding file' # optionally specify --date=... if you require that
# change file content / add more files / create more commits, as desired
# note down the commit id of the last commit! (git rev-parse HEAD)

一旦你有了这个,就该把两个存储库嫁接在一起了:

git remote add existing ../path/to/your/existing/new-repo
git fetch existing
# find the root commit of your existing repo (assuming a "master" branch):
root="$(git log --oneline existing/master | tail -1)"
# replace the root commit with a commit that links both histories:
git replace --graft "$root" "$(git rev-parse HEAD)"

此时,当您运行 or 时,您应该会看到嫁接的历史记录,该历史记录看起来您的脚本始终是存储库的一部分。git loggitk

最终,您希望保留嫁接的历史。请注意,这将更改现有存储库的所有提交的提交哈希值!git filter-branch

git filter-branch --tag-name-filter cat -- --all

最后,将重写的历史包推送到现有的存储库/上游或获取重写的历史记录。如果这个 repo 是共享的,那么每个拥有克隆的人(以及你,如果你有同一个 repo 的多个克隆)都需要扔掉他们的本地版本,并用新的、重写的历史记录替换它。

评论

0赞 Cyberninja 10/8/2023
哇,你付出了很多努力。我会试一试,让你知道。谢谢