Git忽略合并时删除的文件 [英] Git ignores deleted file on merge
问题描述
我有两个存储库。有时,我想将其他
的内容合并到 main
中。但是,合并忽略已删除的文件。让我通过一个例子来解释它:
mkdir -p test / main test / other
cd测试/其他/
git init
触摸一个两三
git add。
git commit -m加一,二和三。
cd ../main/
git init
触摸四个
git add。
git commit -m加四。
将其他
添加到 main
as remote。
git remote add other ../other/
git取其他
合并其内容。
git merge --squash other / master
git commit -m合并其他。
正确添加文件。现在,移除其他
中的文件。
cd ../other /
git rm two
git commit -m删除两个。
将变更合并为 main
。
cd ../main/
git fetch other
git merge --squash other / master
合并后 git status
说:
#在分支master上
没有提交(工作目录干净)
我希望合并删除 two
,因为它已在其他 code>。我在做什么错了?
这里的问题是您使用压扁提交。
当您执行 merge --squash
时,您将放弃分支的所有历史记录。你还没有真正合并分支 - 你只是应用了其历史的简明表述。因此,如果您稍后执行 merge --squash
,git将重新应用分支历史记录中的所有提交(因为两个分支没有共同的祖先)。
执行第一个 当你执行第二个 您应该使用真实合并或樱桃采摘而不是压扁如果你想保持一个回购与远程的最新。 I have two repositories. From time to time, I want to merge the content of Add Merge its content. It adds the files correctly. Now, remove a file in Merge changes into After the merge I would expect the merge to delete The issue here is your use of squash commits. When you do a When you perform the first When you do the second You should use "real" merges or cherry-picking instead of squashing if you want to keep a repo up-to-date with a remote. A 这篇关于Git忽略合并时删除的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!合并--squash
时,您创建一个提交包含创建一个,两个和三个的 main
>。因此, main
的历史首先是创建四个,然后是创建一个,两个和三个。
merge --squash
时,添加一个提交,其中包含(实际上)创建一个,两个和三个以及删除两个。这两个提交在一起(压扁!)的网络是创建一个和三个。所以git自动合并创建一个和三个提交与您当前的回购状态 - 留下四个文件存在。内容合并会自动成功,因为文件one和three在双方都是相同的。
合并--squash
与通过远程存储库中的所有新提交进行播放并将其集成到此处不同。other
into main
. However the merge ignores deleted files. Let me explain it through an example:mkdir -p test/main test/other
cd test/other/
git init
touch one two three
git add .
git commit -m "Add one, two and three."
cd ../main/
git init
touch four
git add .
git commit -m "Add four."
other
to main
as remote.git remote add other ../other/
git fetch other
git merge --squash other/master
git commit -m "Merge other."
other
.cd ../other/
git rm two
git commit -m "Remove two."
main
.cd ../main/
git fetch other
git merge --squash other/master
git status
says:# On branch master
nothing to commit (working directory clean)
two
, as it was deleted in other
. What am I doing wrong?merge --squash
, you abandon all of the history of a branch. You haven't really "merged the branch" - you've just applied a condensed representation of its history. So if you do a later merge --squash
, git will reapply all the commits in the branch's history (since the two branches have no common ancestor).merge --squash
, you create a commit on main
which contains "Create one, two, and three". So the history of main
is first "create four", then "create one, two, and three".merge --squash
, you add a commit which consists of (in effect) "Create one, two, and three" plus "Remove two". The net of those two commits together (squashed!) is "Create one and three". So git auto-merges the "Create one and three" commit with your current repo state - leaving you with four files present. The content merge succeeds automatically because the files "one" and "three" are identical on both sides.merge --squash
is not the same thing as "play through all the new commits in a remote repository and integrate them here".