简体   繁体   English

仅将最近的历史推送到新的 Git 回购

[英]Pushing Only Recent History to a New Git Repo

I have a legacy repository that has been corrupted through some misdirection of LFS and files that are no longer accessible.我有一个遗留存储库,该存储库已因 LFS 的误导和不再可访问的文件而损坏。 The result is that it requires access to files that no longer exist to push to a new repository or major surgery on the history that seems too difficult/risky that this time.结果是它需要访问不再存在的文件以推送到新的存储库或对历史进行重大手术,这一次似乎太困难/太冒险了。 I would like to attempt to create a new repository and push only recent history to avoid the old and bad commits.我想尝试创建一个新的存储库并仅推送最近的历史记录以避免旧的和错误的提交。 Say, all commits since a certain date or from a certain commit.比如说,自某个日期或某个提交以来的所有提交。 Is this possible?这可能吗?

在此处输入图像描述

Why not just delete everything before the first "good" commit?为什么不在第一次“好”提交之前删除所有内容?

One easy way:一种简单的方法:

git rebase -i --root

You are shown a list of all commits in reverse order from the start, eg从一开始就以相反的顺序向您显示所有提交的列表,例如

pick aa48e9c start
pick e10b625 middle
pick 6691860 finished up

Let's say we want to get rid of "start" and "middle" but keep "finished up".假设我们想去掉“start”和“middle”,但保留“finished up”。 So just delete those two lines and save.所以只需删除这两行并保存。

pick 6691860 finished up

An artificial empty first commit will be created, but the bad commits will be gone.将创建一个人为的空第一次提交,但错误的提交将消失。 There might be some conflicts to work out but it shouldn't be difficult.可能会有一些冲突需要解决,但应该不难。

Perhaps you could try git rebase --onto <sha1> <sha2> <sha3> (see this link ) functionality where也许您可以尝试git rebase --onto <sha1> <sha2> <sha3> (请参阅此链接)功能,其中

  • sha1: The new base sha1:新基地
  • sha2: The parent of the first commit that will be copied (sha2 will not be copied itself) sha2:将被复制的第一个提交的父级(sha2 本身不会被复制)
  • sha3: The last, most recent commit which will be copied sha3:将被复制的最后一个最近的提交

So all in all something like this:所以总的来说是这样的:

  1. Setup a new local repository named newRepo设置一个名为newRepo的新本地存储库
mkdir newRepo
cd newRepo
git init
  1. Create an initial commit in newRepo just to initialize master branchnewRepo中创建一个初始提交只是为了初始化 master 分支
touch temp_file_for_initial_commit
git add .
git commit -m "Initial commit of new corruption-free repository"
  1. Get all the history from the oldRepooldRepo获取所有历史记录
git add origin "/path/to/oldRepo" # notice it could just be a path to the local repo
git fetch origin
  1. Use git rebase --onto to copy old range of valid commits to new repo使用git rebase --onto将旧范围的有效提交复制到新的 repo
git rebase --onto master <sha of parent of first commit> <sha of latest commit>
  1. Make a branch for new history为新的历史创建一个分支
git checkout -b latest-history-on-new-repo

  1. Clean up reference to old repo清理对旧回购的引用
git remote remove origin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM