如何在单独的提交中添加跟踪文件的新初始版本?

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

注意:我在发布之前已经解决了我的问题。从那以后我为自己写了一本指南。由于问题和答案已经写好了,而且我没有找到任何关于处理这个特定问题的帖子,我认为发布它并分享解决方案不会花费太多,也许可以帮助一些人:)

问题

我所拥有的上下文是我将配置文件保存在系统上的 git 存储库中。不久前,我在存储库中添加了

someconf.conf
文件,从那时起,我在多次提交中修改了它。后来我发现这个文件一开始是系统自动生成的,然后我就开始修改它。因此,第一个提交
someconf.conf
(我们称其为
A1
)将具有 {初始系统配置} + {modif1},后续提交将分别具有 {modif N}(我们称其为
AN
)。

所以现在我希望能够创建一个提交

B1
,这将是最初在我的存储库中添加
someconf.conf
的新提交,并且仅包含{初始系统配置},然后
A1
将转换为
A1'
B1
相比,现在只包括 {modif1} ,因此新的历史记录将是
...,B1,A1',...,A2',...A3'

git git-filter-branch
1个回答
0
投票

我成功了

# Add a new initial version of a file in the past

# <file> Path of file in git worktree
# <file_b1> New initial version of file
# <file_a1> Original initial version of file

# Keep <file_b1> in an untracked location so that it won't be overwritten by git
cp <file_b1> <file>
git add <file>
git commit -m 'b1' # this is <commit_b1>
# Now keep a safe untracked copy of <file_a1> with the following
git checkout <commit_a1>
git cp <file> <file_a1>
# Resume Operations
git ckeckout master # or your other branch
git rebase -i HEAD~<count_to_commit_a1>
-> pick <commit_b1> # Pick this line from the bottom and add it to the top before the next line
-> pick <commit_a1>
# Resolve merge conflict of inserting <commit_b1> by forcing state b1
cp <file_b1> <file>
git add <file>
git rebase --continue
# Resolve merge conflict of adding <commit_a1> after <commit_b1> by forcing state a1
cp <file_a1> <file>
git add <file>
git rebase --continue

# vim: ft=sh
© www.soinside.com 2019 - 2024. All rights reserved.