提问人:Cyberninja 提问时间:10/8/2023 最后编辑:Cyberninja 更新时间:10/8/2023 访问量:54
如何将过去的历史记录导入到新的 git 存储库中
How to Import past history into new git repo
问:
我有很多旧脚本要导入 GitHub。我想知道是否有办法添加在 git 开始跟踪更改之前发生的历史记录。
现在发生了什么:我在 Github 或本地 git 上制作了一个存储库。我将旧的脚本文件添加到其中,看起来我刚刚在 git 中创建了该文件,但该文件最初是几年前创建的。
示例文件
!/bin/bash
# <date> created this script to do this
# yyyymmdd added feature
# yyyymmdd fixed something
我正在跟踪我正在更新的文件标题中的更改。有没有办法在新的 git 存储库中添加版本控制信息?
如果有更好的方法来添加旧脚本的历史记录,请告诉我。
答:
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 log
gitk
最终,您希望保留嫁接的历史。请注意,这将更改现有存储库的所有提交的提交哈希值!git filter-branch
git filter-branch --tag-name-filter cat -- --all
最后,将重写的历史包推送到现有的存储库/上游或获取重写的历史记录。如果这个 repo 是共享的,那么每个拥有克隆的人(以及你,如果你有同一个 repo 的多个克隆)都需要扔掉他们的本地版本,并用新的、重写的历史记录替换它。
评论
0赞
Cyberninja
10/8/2023
哇,你付出了很多努力。我会试一试,让你知道。谢谢
评论