Git:如何在不更改以后提交的文件/文件夹的情况下在旧提交之间进行提交?

时间:2014-08-29 11:36:26

标签: git commit rebase

我有一系列提交的存储库。让我们说两个旧的提交是A并直接在它之后C:

A--C

在提交CI中,在旧文件的新版本中选中,但事实上全新文件具有相同的名称。

这使得提交C的差异看起来很乱 - 实际上这个差异没有意义,因为文件完全不同。

我想要的是在A和C之间添加一个提交(然后我们称之为B):

A--B--C

在此提交B中,我想删除已用C更改的文件和文件夹。

我知道通过以下内容,我可以在A和C之间添加新的提交:

git rebase --interactive A^

然后在提交A上进行“编辑”

然后

git rm -rf file folder/
git commit -m "B" # New commit with name B

我现在如何获得与原始提交C之后相同的文件和文件夹,这意味着在 B到位之前

换句话说:我怎样才能再次获得C,但没有我在B中删除?

2 个答案:

答案 0 :(得分:1)

您可以使用git-commit-tree

git checkout A
git rm -rf file folder/
git commit -m 'B'
echo 'new C commit' | git commit-tree C -p HEAD
# create a new branch/update branch with the commit id printed by commit-tree

这将:

  • 结帐A(分离HEAD)
  • 删除文件和文件夹,创建新的提交B
  • 使用与原始提交C
  • 相同的文件/状态集创建新提交
  • 然后你必须更新你的旧分支以指向新的提交(目前仍然是一个独立的HEAD)

另一种方法可能是使用移植物然后使用filter-branch使其永久化:

git checkout A
git rm -rf file folder/
git commit -m B
echo `git rev-parse C` `git rev-parse HEAD` >> .git/info/grafts
git filter-branch A^..C

答案 1 :(得分:0)

以下是我为解决问题所采取的措施:

git checkout A
git rm -rf file folder/
# Create a new commit with name B on top of A:
git commit -m "B"
# On top of B create a new commit with what was C:
echo 'new C commit' | git commit-tree C^{tree} -p HEAD 

这将创建一个新的C提交并返回其SHA哈希。此时,如果您签出原始 C提交,则 new C提交的签出确切包含您将获得的内容。

git checkout new-C-commit # No, this did not happen with git commit-tree
git rebase --onto HEAD C master

这会重新分支分支(在我的情况下是它的主),从第一次提交之后新C提交的原始C提交开始。这将毫无问题地应用,因为两个提交都有效地创建了相同的文件和文件夹。