繁体   English   中英

git:如何首先插入[non empty]提交,然后将所有其他提交转移?

[英]git: how to insert a [non empty] commit as the first, shifting all the others?

我想在整个历史之前在我的master分支上插入一个提交。 我已经更改了该软件堆栈,并将更改后的版本提交为初始提交。 当我尝试查看最初更改时,我意识到我的错误方法已经很晚了,并注意到我没有首先提交原始堆栈。 所以我想先榨干它...

我发现了这个非常相似的问题,并认为仅添加某些文件的原始版本并提交就很简单。

但是,重新部署开始抱怨每个文件都带有:

CONFLICT (add/add): Merge conflict in XXXX

当我将文件合并到并尝试重新设置基础时,继续,它仍然失败:

Applying: first version
No changes - did you forget to use 'git add'?
If there is nothing left to stage, chances are that something else
already introduced the same changes; you might want to skip this patch.

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

第一个版本是我的master分支上的第一个提交。

我要做什么甚至有可能吗?

如果您不打算删除所做的所有更改,则可以使用以下命令返回项目的第一个提交版本

git revert $id

$ id是您可以使用git log命令找到的提交的ID

然后您可以使用所需的版本进行修改

但是,请谨慎使用,然后再三思

问题是它要应用的第一个提交是添加文件而不是更改文件,因为它通常会存储一个提交,因此您的初始提交会添加一堆文件,而您尝试执行的提交也是如此依靠。

我不是100%肯定这是最好的方法,但是应该可以

使孤立分支进行一次提交,以使您的代码库变得与您的初始提交相同,将您的第二个提交重新部署到该新提交上。

因为您的第二个提交实际上包含更改,所以不应冲突。

如果那不能满足我的要求,那么我认为我无法正确理解您的问题。

明智的命令应该是:

# first you need a new empty branch; let's call it `newroot`
git checkout --orphan newroot
git rm -rf .
#add all files you want
git commit -am 'initial commit'
git checkout 1st_commit
git merge -s ours newroot
git rebase --onto newroot --root 2nd commit
git branch -d newroot

您可以预料到“原始堆栈”和“已更改版本”之间的冲突。 也就是说,当您(在某个分支上)第一次提交“原始堆栈”,然后合并到堆栈的“更改版本”中时,所有内容都会发生冲突。 但是,您知道的是“变更版本”是正确的版本, 因此您不必一一解决每个冲突 相反,您将使用:

git checkout --ours -- <files that conflict>       # see note below
git add -- <files that conflict>
git commit -m 'Resolved with --ours'

注意:有时-我们的是正确的; 有时-它们是正确的,这取决于您是合并还是重新定基。 选择一个,使用它,检查是否获得了正确的文件,如果没有,则使用另一个。

暂无
暂无

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

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