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

问题描述 投票:0回答:1

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

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

示例文件

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

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

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

git github history
1个回答
0
投票

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

首先,创建一个新的空 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)"

此时,当您运行

git log
gitk
时,您应该会看到嫁接的历史记录,看起来您的脚本始终是存储库的一部分。

最终,您希望通过

git filter-branch
保留嫁接的历史。请注意,这将更改现有存储库的所有提交

的提交哈希值
git filter-branch --tag-name-filter cat -- --all

最后,将重写的历史包推送到现有的存储库/上游或获取重写的历史记录。如果此存储库被共享,则拥有克隆的每个人(以及您,如果您有同一存储库的多个克隆)都需要丢弃其本地版本,并将其替换为新的、重写的历史记录。

© www.soinside.com 2019 - 2024. All rights reserved.