为什么Git子树合并不会删除文件?

3
我有一个分支(“other”)作为子树附加到另一个分支(“master”)。当我从“other”合并到“master”时,它不会删除在“other”中已删除的文件。
在干净的存储库上重现步骤:
$ touch master.txt
$ git add master.txt
$ git commit -m 'Initial master'
[master (root-commit) e2f5ffd] Initial master
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master.txt
$ git checkout --orphan other
Switched to a new branch 'other'
$ touch other.txt
$ git add other.txt
$ git status
On branch other

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   master.txt
        new file:   other.txt
$ git commit -m 'Initial other'
[other (root-commit) 408ee95] Initial other
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master.txt
 create mode 100644 other.txt
$ git checkout master
Switched to branch 'master'
$ git read-tree --prefix=other/ -u other
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   other/master.txt
        new file:   other/other.txt
$ git commit -m 'Other subtreed'
[master f9ba0db] Other subtreed
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 other/master.txt
 create mode 100644 other/other.txt
$ git checkout other
Switched to branch 'other'
$ git rm master.txt
rm 'master.txt'
$ git commit -m 'master.txt removed'
[other 1feef18] master.txt removed
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 master.txt
$ git checkout master
Switched to branch 'master'
$ git merge --squash -s subtree --no-commit other
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested
$ git status
On branch master
nothing to commit, working directory clean

这段文本的意思是:“所以,在合并已删除的文件后,就没有什么可提交的了。这是正确的行为吗?如何合并已删除的文件?”
1个回答

1
正确的合并子树以使所有内容都被合并的方法是:
git merge -s recursive -Xsubtree=other --no-commit other

所以,它没有使用 --squash 并且形式有些不同(来自 git v2)。

这对我不起作用,我有一些文件是通过子树合并添加的,稍后删除时您的命令无法将它们删除。它确实正确地引入了其余的更改。 - LovesTha
@Glaz,你解决了这个问题吗?我也遇到了同样的情况。 - Richa Dua

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接