简体   繁体   English

在Git中,如何创建最早提交之前的提交?

[英]In Git, how do I create a commit that's before the oldest commit?

I have a Git repository whose oldest commit is version 6.8.2 (it is tagged "v6.8.2"). 我有一个Git存储库,其最早的提交是版本6.8.2(标记为“v6.8.2”)。 I have version 6.8.1 of the package that I would like to commit to the repository as the parent commit of the oldest commit. 我有包的版本6.8.1,我想提交到存储库作为最早提交的父提交。 Is this possible? 这可能吗? If so, how? 如果是这样,怎么样?

You could rebase your current branch on top of the oldest commit that you want (v6.8.1). 您可以在所需的最早提交之上重新定义当前分支(v6.8.1)。 It will rewrite your history, though, but it is possible. 但它会重写你的历史,但它是可能的。

Edited to add more 编辑添加更多

Assuming your code is on the branch master 假设您的代码在分支master

Clear out the existing code and create a new branch. 清除现有代码并创建新分支。 Your old code is not lost - it still exists on the master branch. 您的旧代码不会丢失 - 它仍然存在于主分支上。

git symbolic-ref HEAD refs/heads/newbase
rm .git/index
git clean -dxf

Now you are on the branch newbase which is an empty directory. 现在你在分支newbase这是一个空目录。 Add the files that you want to be the new base to here. 将要作为新基础的文件添加到此处。

<add the files to the directory>
git add .
git commit -a -m "New base commit v6.8.1"

New you have two branches master which has your original code and newbase which has only your new base. 新的你有两个分支master ,它有你的原始代码和newbase只有你的新基地。

Now checkout the master branch and rebase it upon the newbase branch 现在签出master分支并在newbase分支上newbase

git checkout master
git rebase newbase

And then All you need to do is fix the conflicts, and you have a new master branch that starts at the earlier state of the code. 然后,您需要做的就是修复冲突,并且您有一个新的master分支,它从代码的早期状态开始。

This is all a bit of a mess though. 这有点乱。 If you just want to add the code to the repository, just do the first bit, which creates a new branch. 如果您只想将代码添加到存储库,只需执行第一位,即创建新分支。 Tag it so that you know what it is, there is no need to rebase the master branch on to it because if you get to the base of master, and want to go further back in time - you can just see what is in the other branch. 标记它以便你知道它是什么,没有必要将master分支重新设置为它,因为如果你到达master的基础,并希望进一步回到过去 - 你可以看到另一个是什么科。

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

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